Jump to content

[SOLVED] Small Variable Question


travis

Recommended Posts

What i need to do is have to sql vars open (if thats where there called) and i have another conditional var that if its this need it accesses the first sql var and vice versa

 

eg.

$sql = mysql_query("SELECT * FROM own WHERE ID='$P1'");
$P1O = mysql_fetch_assoc($sql);

$sql = mysql_query("SELECT * FROM own WHERE ID='$P2'");
$P2O = mysql_fetch_assoc($sql);
if ($x>$y) $searchvar = 1; else $searchvar = 2;

 

now what i do not know how to do is make it so it picks either or sql var. I thought it would go like this

 

print $P".$searchvar."O['id'];

 

but im an idiot and cannot get it to work. Any help is appriciated.

-Travis

Link to comment
https://forums.phpfreaks.com/topic/46026-solved-small-variable-question/
Share on other sites

I see what you are trying to do, and it won't work.  There are two ways of doing what you are trying to do.

 

the first would be to replace your print line with this:

eval('$value = $p' . $searchvar . '0[\'id\'];');
print $value;

 

or a cleaner, cooler, and all around better method would be to use an array like so:

 

$sql = mysql_query("SELECT * FROM own WHERE ID='$P1'");
$P[0] = mysql_fetch_assoc($sql);

$sql = mysql_query("SELECT * FROM own WHERE ID='$P2'");
$P[2] = mysql_fetch_assoc($sql);

if ($x>$y) 
     $searchvar = 0; 
else 
     $searchvar = 1;

print $p[$searchvar]['id'];

 

or the best, fastest and cleanest way of all:

 

 

if ($x>$y) 
     $sql = "SELECT * FROM own WHERE ID='$P1'"; 
else 
     $sql = "SELECT * FROM own WHERE ID='$P2'";

$result = mysql_query($sql);
$p = mysql_fetch_assoc($result);
print $p['id'];

 

you should probably use the bottom one, unless you have a compelling reason not to.

 

Archived

This topic is now archived and is closed to further replies.

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