...

View Full Version : MySQL statement within mysql_fetch_array



newmand2
10-07-2004, 02:31 PM
For some reason this script isn't working... Can anyone see why?


<?php

include_once 'conn.php'; # Include database connection details

$mlist = mysql_query("SELECT x,y,z,n FROM arbiter WHERE a='1'"); # Obtain x,y,z and name fields from table 'arbiter'

while ($ml = mysql_fetch_array($mlist)) { # Loop through all rows found where a = 1 in table 'arbiter'

$x = $ml['x'];
$y = $ml['y'];
$z = $ml['z'];
$n = $ml['n'];

$pl = mysql_query("SELECT * FROM dumps WHERE x='$x', y='$y', z='$z'"); # Using values from table 'arbiter', get all values from table 'dumps' which correspond to values being looped through.

$pl_n = $pl[0]; # N
$pl_p = $pl[1]; # P
$pl_r = $pl[2]; # R
$pl_s = $pl[3]; # S

echo "$pl_p $pl_n<br>";

echo "$pl_r $pl_s<br>";

}

?>

Thankyou very much everyone :)

sir pannels
10-07-2004, 02:41 PM
what happens when you run it? :p

Error message or bad results?

newmand2
10-07-2004, 02:44 PM
sorry, shoulda mentioned that, eh :)

i get a blank screen :(

sir pannels
10-07-2004, 02:53 PM
ok In that case I guess your not finding any results with your query.
Do you use a front end such as phpmyadmin? If do run your query in that and see what you get :)

newmand2
10-07-2004, 02:59 PM
Using PHPMyAdmin, I have 10 rows in the DB.

I then altered my code to this:


<?php

include_once 'conn.php'; # Include database connection details

$pl = mysql_query("SELECT * FROM dumps WHERE x='1', y='2', z='2'"); # Using values from table 'arbiter', get all values from table 'dumps' which correspond to values being looped through.

$pl_x = $pl['x']; # N
$pl_y = $pl['y']; # P
$pl_z = $pl['z']; # R


echo "$pl_x $pl_y<br>";

?>


I still get no results displayed, even though in my DB there is a record where x=1, y=2 and z=2... :(

So I am thinking that I have a problem with this query, and the rest is ok

4xz
10-07-2004, 03:09 PM
If I may make a side-comment. Dunno if u designed this datamodel yourself, but with columnnames like a,b,c etc. U will be finding yourself in a lot of trouble in 12 months.

newmand2
10-07-2004, 03:11 PM
Thanks, this is only a testing file, and I'll integrate it into the full design with proper filenames once I am done.

Nightfire
10-07-2004, 04:14 PM
Didn't think you could use comma's in the query. I've always used


$pl = mysql_query("SELECT * FROM dumps WHERE x='1' AND y='2' AND z='2'");

Try and stay away from the perl comments too, I think they're depreciated, try to use // or /* */
Also, try adding or die() at the end of your query


$pl = mysql_query("SELECT * FROM dumps WHERE x='1' AND y='2' AND z='2'") or die("Unable to complete query ".mysql_error());

mrjamin
10-08-2004, 03:15 PM
i think you're missing a mysql_fetch_assoc in there!!


<?php

include_once 'conn.php'; # Include database connection details

$pl = mysql_query("SELECT * FROM dumps WHERE x='1', y='2', z='2'"); # Using values from table 'arbiter', get all values from table 'dumps' which correspond to values being looped through.

while($pl = mysql_fetch_assoc($pl)){
$pl_x = $pl['x']; # N
$pl_y = $pl['y']; # P
$pl_z = $pl['z']; # R

echo "$pl_x $pl_y<br>";
}

?>


try that

Hawkmoon
10-08-2004, 04:01 PM
Yeah mrjamin's right. You could also use mysql_fetch_row:


<?php

include_once 'conn.php'; # Include database connection details

$result = mysql_query("SELECT * FROM dumps WHERE x='1', y='2', z='2'");
while($row = mysql_fetch_row($result)){
$pl_x = $row[0]; # N
$pl_y = $row[1]; # P
$pl_z = $row[2]; # R

echo "$pl_x $pl_y $pl_z<br>";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum