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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.