Jump to content

An Array of MYSQL results makes everything null?


sootybuttercup

Recommended Posts

I'm trying to make a loop to print the results of 22 MYSQL queries. The MYSQL results are arrays. I know for a fact that the results are not null but when I put them in the array and call $resultarray[$j] it always says it is null.

 

My code is:

 

    $item = htmlspecialchars($_POST['item']);  //input from a form on a previous page

    $result1 = mysql_query("SELECT * FROM `shelf 1` WHERE Item REGEXP '.*$item.*'");

    $result2 = mysql_query("SELECT * FROM `shelf 2` WHERE Item REGEXP '.*$item.*'");

    $result3 = mysql_query("SELECT * FROM `shelf 3` WHERE Item REGEXP '.*$item.*'");

    $result4 = mysql_query("SELECT * FROM `shelf 4` WHERE Item REGEXP '.*$item.*'");

    $result5 = mysql_query("SELECT * FROM `shelf 5` WHERE Item REGEXP '.*$item.*'");

    $result6 = mysql_query("SELECT * FROM `shelf 6` WHERE Item REGEXP '.*$item.*'");

    $result7 = mysql_query("SELECT * FROM `shelf 7` WHERE Item REGEXP '.*$item.*'");

    $result8 = mysql_query("SELECT * FROM `shelf 8` WHERE Item REGEXP '.*$item.*'");

    $result9 = mysql_query("SELECT * FROM `shelf 9` WHERE Item REGEXP '.*$item.*'");

    $result10 = mysql_query("SELECT * FROM `shelf 10` WHERE Item REGEXP '.*$item.*'");

    $result11 = mysql_query("SELECT * FROM `shelf 11` WHERE Item REGEXP '.*$item.*'");

    $result12 = mysql_query("SELECT * FROM `shelf 12` WHERE Item REGEXP '.*$item.*'");

    $result13 = mysql_query("SELECT * FROM `shelf 13` WHERE Item REGEXP '.*$item.*'");

    $result14 = mysql_query("SELECT * FROM `shelf 14` WHERE Item REGEXP '.*$item.*'");

    $result15 = mysql_query("SELECT * FROM `shelf 15` WHERE Item REGEXP '.*$item.*'");

    $result16 = mysql_query("SELECT * FROM `shelf 16` WHERE Item REGEXP '.*$item.*'");

    $result17 = mysql_query("SELECT * FROM `shelf 17` WHERE Item REGEXP '.*$item.*'");

    $result18 = mysql_query("SELECT * FROM `shelf 18` WHERE Item REGEXP '.*$item.*'");

    $result19 = mysql_query("SELECT * FROM `shelf 19` WHERE Item REGEXP '.*$item.*'");

    $result20 = mysql_query("SELECT * FROM `shelf 20` WHERE Item REGEXP '.*$item.*'");

    $result21 = mysql_query("SELECT * FROM `shelf 21` WHERE Item REGEXP '.*$item.*'");

    $result22 = mysql_query("SELECT * FROM `shelf 22` WHERE Item REGEXP '.*$item.*'");

 

    $resultarrary = array($result1, $result2, $result3, $result4, $result5, $result6, $result7, $result8, $result9, $result10, $result11, $result12, $result13, $result14, $result15, $result16, $result17, $result18, $result19, $result20, $result21, $result22); 

 

    $j=0;

 

//all of result array is coming up null

 

    while($j <= 21)

    {

      if($resultarray[$j]==null)    //this currently is true 22 times. Yet if($result11==null) is false.

      {

        echo("Hai");

        $j++;

        continue;

      }

      echo("I'm in here");

      $num=mysql_numrows($resultarray[$j]);

      $i=0;

      while($i < $num)

      {

        $printitem=mysql_result($resultarray[$j], $i, "Item");

        $printamount=mysql_result($resultarray[$j], $i, "Amount");

        $printcategory=mysql_result($resultarray[$j], $i, "Category");

        echo "Item: $printitem,  Amount: $printamount,  Category: $printcategory<br>";

        $i++;

      }

      $j++;

    }

 

You should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your php.ini to get php to help you. There would have been an undefined error when you accessed the non-existent array that would have given you immediate feed back that would have pointed you in the right direction of finding the problem.

 

Your scheme of using multiple tables to store similar items is very bad from a design standpoint and results in a lot of unnecessary code to do any task that involves more than one table. If all your items were stored in a single table with a column to indicate the 'shelf', all that code you posted could be done with a single query to get just the rows you want in the order you want, then process the single result set.

<?php
$result22 = mysql_query("SELECT * FROM `shelf 22` WHERE Item REGEXP '.*$item.*'");

$sqls = array();
for( $i = 1; $i <= 22; $i++ ) {
    $sqls[] = "SELECT * FROM `shelf {$i}` WHERE `Item` REGEXP '.*{$item}.*'";
}
$sql = implode( PHP_EOL . 'UNION' . PHP_EOL, $sqls );
$result = mysql_query( $sql );
?>

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.