Page 1 of 1

php myAdmin gives different results than php code in query

Posted: Sat Mar 17, 2012 11:17 am
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"

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

Posted: Sat Mar 17, 2012 12:05 pm
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.

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

Posted: Sat Mar 17, 2012 1:11 pm
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]!!

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

Posted: Sat Mar 17, 2012 6:20 pm
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>";
}