kulmu Posted January 9, 2007 Share Posted January 9, 2007 I am trying to run a SQL query that will return both 2 random selections and two defined variables. How can this be done? Right now my code looks like:Code:$sql = "SELECT id FROM users ORDER BY rand() LIMIT 2";$sql_result = mysql_query($sql);$query = "SELECT * FROM `users` WHERE (`users`.`id` = $foo) OR (`users`.`id` = $foo2)";$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());// if records presentif (mysql_num_rows($sql_result) > 0){// iterate through resultset// print randomwhile ($row = mysql_fetch_array($sql_result)) { $random_rows[] = $row;} // iterate through resultset// recover $_GET datawhile ($row = mysql_fetch_array($result)) { $rows[] = $row;}Trying to use $rows[1]['id'] to recover the data but it returns Array['id']Sorry if this is a newbish question I just started with PHP and am still getting the grasp of it. I really appreciate the help. Link to comment https://forums.phpfreaks.com/topic/33500-is-it-possible-to-run-two-sql-selects/ Share on other sites More sharing options...
Asheeown Posted January 9, 2007 Share Posted January 9, 2007 [code]<?php$sql = "SELECT id FROM users ORDER BY rand() LIMIT 2";$sql_result = mysql_query($sql);$query = "SELECT * FROM `users` WHERE (`users`.`id` = $foo) OR (`users`.`id` = $foo2)";$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());// if records presentif (mysql_num_rows($sql_result) > 0){// iterate through resultset// print randomwhile ($row = mysql_fetch_array($sql_result)) { extract($row);}// iterate through resultset// recover $_GET datawhile ($row = mysql_fetch_array($result)) { extract($row);}?>[/code]Take that and use it because when you use a while statement it automatically builds the array for you no need for $rows[] = $row; so to speak...what I put in there is a lot easier to use...the extract function it takes your array and puts it into simple one word variables, if you want the array variables you can take out the extract and use $rows[Something] or $random_rows[Something] inside your while statement Link to comment https://forums.phpfreaks.com/topic/33500-is-it-possible-to-run-two-sql-selects/#findComment-156787 Share on other sites More sharing options...
kulmu Posted January 9, 2007 Author Share Posted January 9, 2007 Using that code how would I call the data then? I tried to replace extract with $rows[] and get the error "Cannot use [] for reading" Link to comment https://forums.phpfreaks.com/topic/33500-is-it-possible-to-run-two-sql-selects/#findComment-156798 Share on other sites More sharing options...
kulmu Posted January 9, 2007 Author Share Posted January 9, 2007 I'm still stuck and after hours of changing various ideas I am still back where I started. I can't seem to take my second query and have it display anything other than Array[id] Link to comment https://forums.phpfreaks.com/topic/33500-is-it-possible-to-run-two-sql-selects/#findComment-156863 Share on other sites More sharing options...
trq Posted January 9, 2007 Share Posted January 9, 2007 [code]<?php $sql = "SELECT id FROM users ORDER BY rand() LIMIT 2"; if ($result = mysql_query($sql)) { if (myssql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $random_rows[] = $row; } } } $sql = "SELECT id FROM users WHERE id = '$foo' || id = '$foo2'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $rows[] = $row; } } } if (is_array($random_rows) && is_array($rows)) { echo "random<br />"; echo $random_rows[0]['id']."<br />"; echo $random_rows[1]['id']."<br />"; echo "chosen<br />"; echo $rows[0]['id']."<br />"; echo $rows[1]['id']."<br />"; }?>[/code] Link to comment https://forums.phpfreaks.com/topic/33500-is-it-possible-to-run-two-sql-selects/#findComment-156873 Share on other sites More sharing options...
dpabla Posted January 9, 2007 Share Posted January 9, 2007 You could do "if...while" statements or if you want mutiple selects this is what I have done as an example:<!-- DISPLAY COLLECTION PULL DOWN HERE --><?php$result = mysql_query ("SELECT product_name,subcategory_id FROM productsWHERE subcategory_id = {$_GET['sub_id']}");echo "<div align=center><select name=collection value=''><option value=''>Browse Collection</option><br>";while($collection=mysql_fetch_array($result)){$prodname = $collection["product_name"];echo "<option value='".$prodname."'>".$prodname."</option>";}echo "</select>";?></div></td> </tr> <tr> <td> </td> </tr> <tr> <td><div align="center"><!-- DISPLAY METAL --><!-- DISPLAY METAL --><?php$result = mysql_query ("SELECT metal FROM productsWHERE product_id = {$_GET['prod_id']}");echo "<select name=collection value=''><option value=''>Select Metal</option><br>";while($material=mysql_fetch_array($result)){$madeof = $material["metal"];[quote author=kulmu link=topic=121682.msg500753#msg500753 date=1168370711]I am trying to run a SQL query that will return both 2 random selections and two defined variables. How can this be done? Right now my code looks like:Code:$sql = "SELECT id FROM users ORDER BY rand() LIMIT 2";$sql_result = mysql_query($sql);$query = "SELECT * FROM `users` WHERE (`users`.`id` = $foo) OR (`users`.`id` = $foo2)";$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());// if records presentif (mysql_num_rows($sql_result) > 0){// iterate through resultset// print randomwhile ($row = mysql_fetch_array($sql_result)) { $random_rows[] = $row;} // iterate through resultset// recover $_GET datawhile ($row = mysql_fetch_array($result)) { $rows[] = $row;}Trying to use $rows[1]['id'] to recover the data but it returns Array['id']Sorry if this is a newbish question I just started with PHP and am still getting the grasp of it. I really appreciate the help.[/quote] Link to comment https://forums.phpfreaks.com/topic/33500-is-it-possible-to-run-two-sql-selects/#findComment-156894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.