Forum Moderators: coopster

Message Too Old, No Replies

Different result from MySQL and PHP

php returns less rows than direct mysql query

         

phparion

6:05 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi

I am executing the following query from PHP code, using mysql_fetch_array(mysql_query()) functions,

$sql = "SELECT count(br.bname) as cc

FROM beers br, brewers bw
WHERE br.bid = bw.bwid
AND br.sid = '$style'";

echo $sql;
$brRes = mysql_query($sql) or die(mysql_error());

$row = mysql_fetch_array($brRes);
echo $row['cc'];

I get 2 rows from last echo...

however if i execute the very same query directly in phpmyadmin i get 15 rows.

i have tested this for many different values and always get less rows from PHP and more rows from phpmyadmin.

Can anybody shed some light please what's going wrong with this?

thank you very much.

dreamcatcher

8:52 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What about if you do..

echo mysql_num_rows($brRes);

or

echo count($row);

As you are reading the data into an array using myql_fetch_array, wouldn`t a while loop be more efficient? Or am I missing something?

dc

Habtom

9:04 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> AND br.sid = '$style'

I think that could be the part where it is making a difference.

phparion

11:15 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



to my amazement, i am doing the same as here

$brRes = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($brRes);
echo count($row);

echo $sql;

my echo count() line shows 18 as the result rows. however, i copy the query which i get from echo $sql and paste it in the phpmyadmin and get 364 results.

the AND section is not giving problem, because i am not changing anything in the query, i execute the very same query in phpmyadmin.

i am lost :( ....

phparion

11:19 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



btw i this code is a testing code that i am doing for error analysis. the actual code uses while loop after mysql_query() to display the records. but it displays less record. therefore i am printing the number of rows in Resource to check the problem.

Habtom

12:07 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The error could be from something that is not posted there. You might be querying the wrong database, or the code is connected to a different one.

Those kind of errors end up to be originating from something over looked, but very very small.

You need to 'zoom' out a little bit :)

phparion

5:22 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i am connecting to the correct database and query the correct tables. :-s

coopster

6:19 pm on Dec 27, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would start at the command line and run the query there. See which record count (your code versus phpmyadmin) is incorrect and work from there. Step-by-step through the code if it is your code that comes up incorrect, and if it is phpmyadmin, check the version and bugs, etc. to make certain it is current.

Habtom

10:03 am on Dec 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



phparion, solved this?

phparion

5:11 am on Dec 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi guys,

Finally I have fixed the problem. It had to do with my hosting server setup. I am not sure about the details but somehow the code was connecting to the wrong database. I had a sample and a working database. Sample database has less records for testing and production database is huge. Phpmyadmin was communicating with production database while the php code was working with sample database.

I saw a mysql server problem notice on the hosting company side, and as soon as they fixed it the code started to work fine.

Thank you all for your help.