Jump to content

counting the number of rows in a mysql database

- - - - -

  • Please log in to reply
6 replies to this topic

#1
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
hi all,

okay this seems really basic mysql but I cannot seem to get it to work.
I am trying to count the number of rows in a database

My code goes like this:

$q = "SELECT COUNT(*) FROM `test` WHERE `Type` = \'1\' LIMIT 0, 30 "; 

$count_tst = mysqli_query($dbc, $q);

echo "$count_tst";


And I know this works because when I run it in the PHPMyAdmin utility it runs perfectly.

Am I missing something here?

Any help??

Many Thanks,
J

P.s i forgot to say that the problem is there is nothing being echoed out of the variable $count_tst and i am already connected to the database, i just haven't pasted the code (it defo connects right as i link to the connection page with a require_once(); which works correctly on other pages I've done.)

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Yes, you need to fetch the result too.
$q = "SELECT COUNT(*) FROM `test` WHERE `Type` = \'1\' LIMIT 0, 30 ";  

$count_tst = mysqli_query($dbc, $q);

$count = mysqli_fetch_array($count_tst);

echo $count[0];

but you shouldn't have a limit when you're counting rows...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I have tried your code butit dosent seem to work.

I get the error

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line 28

Line 28 is this line

$count = mysqli_fetch_array($count_tst); 

any ideas?

Thanks

#4
maximilianos

maximilianos

    Newbie

  • Members
  • Pip
  • 2 posts
can you send me the script on my email to give you a solution my email is maximilianos1985@hotmail.com

#5
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
sorry but i no longer need to, i finally got the code working.

Thanks for the help

The problem was the \ in the \'1\'. Not sure why it caused this problem but its fixed it :D

Thanks again

#6
maximilianos

maximilianos

    Newbie

  • Members
  • Pip
  • 2 posts
welcome welton122 anytime you need help you can email me

#7
welton122

welton122

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thanks maximilianos




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users