travis Posted April 7, 2007 Share Posted April 7, 2007 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 More sharing options...
phoenixx5 Posted April 7, 2007 Share Posted April 7, 2007 $x = 4; $y = 9; if ($x>$y) { $searchvar = 1; } else $searchvar = 2; Link to comment https://forums.phpfreaks.com/topic/46026-solved-small-variable-question/#findComment-223664 Share on other sites More sharing options...
redking Posted April 7, 2007 Share Posted April 7, 2007 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. Link to comment https://forums.phpfreaks.com/topic/46026-solved-small-variable-question/#findComment-223669 Share on other sites More sharing options...
travis Posted April 7, 2007 Author Share Posted April 7, 2007 awesome, thanks dude. Im gonna use the 2nd method you post as im not only printing that var, i will be doing like 50-100 lines from that point on, the print was just to get my point across. thanks soo much Link to comment https://forums.phpfreaks.com/topic/46026-solved-small-variable-question/#findComment-223675 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.