Jump to content

Need help with mysql_query and mysql_fetch_array


Morrac

Recommended Posts

I am having some problems with a couple of MySQL lines of code on a website that I am working on.

 

$q = "SELECT COUNT(ID) FROM ".$tables['TableNameGoesHere']." WHERE AssocA=".$logObj->ID." AND (AssocB=10 OR AssocB=1)";
$r = mysql_query($q);
$ra = mysql_fetch_array($r);

 

The problem I am having is that $r is returning Resource id #17 and $ra is returning just Array (not an array just the work Array).

 

I don't know what the problem could be ... I am completely stumped.

If you echo an array, it will just give you the word array, try using print_r() instead.

 

You need to extract each row of data from the array using extract()

 

If you show us a bit more code then we might understand the problem better.

First, this should be under the PHP forum...

 

Code looks good, but you can't just do an echo/print on $ra. If you do a print_r it will show you the values.

 

<?php
$q = "SELECT COUNT(ID) FROM ".$tables['TableNameGoesHere']." WHERE AssocA=".$logObj->ID." AND (AssocB=10 OR AssocB=1)";
$r = mysql_query($q);
$ra = mysql_fetch_array($r); //Get the first row (there should only be one row)
echo $ra[0]; //Print the first column from that row
?>

Don't worry about extracting each row, because there will only be one row with a COUNT() in the SELECT. My code above should print out the value fine.

 

If you still aren't getting the results you expect, put an:

echo $q;

in there to make sure the query looks like you expect it to look

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.