Reply
Help with query Count
Old 04-29-2008, 10:18 AM Help with query Count
Novice Talker

Posts: 5
good day all,

Have an issue with making a query count with php/mysql

What i'm trying to do is search my column within my table and return a count for the number of specified records found

This is what i'm coding so far but getting errors:

PHP Code:

<?php

$link 
mysql_connect("localhost""root""Password");
mysql_select_db("dt08_vnc"$link);

$result=mysql_query("SELECT COUNT(*) FROM bayside WHERE item ='Desktop'");
if (!
$result) {
    die(
'Invalid query: ' mysql_error());
}



?>
This is where im a little lost. TO echo my Value out. What am I looking for. I know its a fetch array but not sure which one with a Count Query.

I figure its somehting like:

PHP Code:
while ($row msql_fetch_row($result)) {
    echo 
$row"\n";

I just want it to count all the "desktop" text with in my Table "Bayside" and return with a number Value.

Any help would be great, thanks.
vipernet is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-29-2008, 10:40 AM Re: Help with query Count
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Almost there.
http://www.php.net/mysql_fetch_row
As the name don't suggest, the mysql_fetch_row() returns an array, so you should write it:
PHP Code:
while($row=mysql_fetch_array($result)){
  echo 
$row[0]."<br/>\n";

You have to give the number of the column of the row you want to fetch, even when there is only one.

But...
I recommand you to rather use the mysql_fetch_assoc() function, that allows you to use the column name, rather than a number.
http://www.php.net/manual/en/functio...etch-assoc.php
You can simplify the syntax by using an alias in the select:
PHP Code:
$q="select count(*) as cnt from bayside where item='desktop'";
$r=mysql_query($q);
while(
$row=mysql_fetch_assoc($r)){
     echo 
$row['cnt']."<br/>\n";

__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 04-29-2008 at 01:22 PM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 11:56 AM Re: Help with query Count
Novice Talker

Posts: 5
thanks for the help dude.

Although 1 problem with your code:
PHP Code:
$link mysql_connect("localhost""root""Password");
mysql_select_db("dt08_vnc"$link);
$q="select count(*) as cnt from bayside where item='desktop'";
$r=mysql_query($q);
while(
$row=mysql_fetch_assoc($result)){
     echo 
$row['cnt']."<br/>\n";

I figured out what was wrong, just something little.
WAS:
PHP Code:
while($row=mysql_fetch_assoc($result)){ 
Should be:
PHP Code:
while($row=mysql_fetch_assoc($r)){ 

here is the whole code for later issues with others can look back at this.

PHP Code:
<?php
$link 
mysql_connect("localhost""root""Password");
mysql_select_db("dt08_vnc"$link);
$q="select count(*) as cnt from bayside where item='desktop'";
$r=mysql_query($q);
while(
$row=mysql_fetch_assoc($r)){
     echo 
$row['cnt']."<br/>\n";
}  
?>
Again thanks allot for the tips and help Tripy
vipernet is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 01:22 PM Re: Help with query Count
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Sorry, copy/paste from work in a rush doing datawarehouse.
I did not noticed the typo.

Thanks to post it corrected.
I have updated my last post too, if ever someone copy it without reading further...
__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 04-29-2008 at 01:24 PM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 04-29-2008, 01:55 PM Re: Help with query Count
Novice Talker

Posts: 5
This is what i have made that code to do so far. Just wondering is it something u would do or am I missing a short way to write this:
PHP Code:
<table width="40%" border="1" align="center">
<tr>
<?php
$link 
mysql_connect("localhost""root""Password");
mysql_select_db("dt08_vnc"$link);
$q="select count(*) as dsk from bayside where item='desktop'";
$l="select count(*) as lcd from bayside where item like '%lcd%'";
$lap="select count(*) as lap from bayside where item ='laptop'";
$smt="select count(*) as smt from bayside where item ='smartboard'";
$r=mysql_query($q);
$r1=mysql_query($l);
$r2=mysql_query($lap);
$r3=mysql_query($smt);
while(
$row=mysql_fetch_assoc($r))
while(
$row1=mysql_fetch_assoc($r1))
while(
$row2=mysql_fetch_assoc($r2))
while(
$row3=mysql_fetch_assoc($r3)){
 
echo 
'<tr><td>'.$row['dsk'].'</td><td>Desktops</td></tr>';
echo 
'<tr><td>'.$row1['lcd'].'</td><td>LCD Projector</td></tr>';
echo 
'<tr><td>'.$row2['lap'].'</td><td>Laptops</td></tr>';
echo 
'<tr><td>'.$row3['smt'].'</td><td>SmartBoards</td></tr>';

?>
 
</tr>
</table>
vipernet is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with query Count
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13365 seconds with 12 queries