php myAdmin gives different results than php code in query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

php myAdmin gives different results than php code in query

Post by chopWood »

Here is a script that I typed into php MyAdmin:

SELECT
M.media_name FROM media as M
INNER JOIN member_media as J on J.mediaID = M.mediaID
WHERE J.maaMemberId = 209

The number of rows it returns is 3

Here is the php version:

$q = "SELECT
M.media_name FROM media as M
INNER JOIN member_media as J on J.mediaID = M.mediaID
WHERE J.maaMemberId = 209";

$r = @mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
$row = mysqli_fetch_array ($r, MYSQLI_NUM);
echo $row[0];
echo $row[1];
echo $row[2];
exit();

The only result is $row[0] which is a correct result (matches first row in first example)
The other 2 rows give "Undefined offset"
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php myAdmin gives different results than php code in que

Post by Celauran »

It's not giving different results, you're just handling the results incorrectly. mysql_fetch_array() returns one array per row. Wrap it in a while loop and you'll get the results you expect.
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: php myAdmin gives different results than php code in que

Post by chopWood »

I thought the array that was produced (in this case) was that of $row[0],$row[1],$row[2] where each of these three held a single row in the results of the query?

What you're saying is that:

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
echo "One medium is ".$row[0]."<br />";
}

will print the entire result array within $row[0]?

and... I see that every time I have the line $row = mysqli_fetch_array($r, MYSQLI_NUM))
it eliminates one element within the array ( because it has already been "fetched")

so that if I had (even though it doesn't make sense)...
$row = mysqli_fetch_array($r, MYSQLI_NUM)
$row = mysqli_fetch_array($r, MYSQLI_NUM)
$row = mysqli_fetch_array($r, MYSQLI_NUM)

then, if I go:
while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
echo "One medium is ".$row[0]."<br />";
}

There will be nothing left to print out in the array within $row[0]!!
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: php myAdmin gives different results than php code in que

Post by litebearer »

test this

Code: Select all

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
echo $row[0]. "<br />" . $row[1] . "<br />" . $row[2] . "<hr>";
}
Post Reply