Jump to content

Is it Possible to Run Two SQL Selects?


kulmu

Recommended Posts

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 present
if (mysql_num_rows($sql_result) > 0)
{
// iterate through resultset
// print random
while ($row = mysql_fetch_array($sql_result)) {
  $random_rows[] = $row;
}

// iterate through resultset
// recover $_GET data
while ($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

[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 present
if (mysql_num_rows($sql_result) > 0)
{
// iterate through resultset
// print random
while ($row = mysql_fetch_array($sql_result)) {
  extract($row);
}

// iterate through resultset
// recover $_GET data
while ($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
[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]
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 products
WHERE 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>&nbsp;</td>
      </tr>
      <tr>
        <td><div align="center">

<!-- DISPLAY METAL -->
<!-- DISPLAY METAL -->

<?php

$result = mysql_query
("SELECT metal FROM products
WHERE 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 present
if (mysql_num_rows($sql_result) > 0)
{
// iterate through resultset
// print random
while ($row = mysql_fetch_array($sql_result)) {
  $random_rows[] = $row;
}

// iterate through resultset
// recover $_GET data
while ($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]

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.