A PHP Array of MYSQL results makes everything null?

  • Thread starter Thread starter ckcook17
  • Start date Start date
C

ckcook17

Guest
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++;
}
I know that the MYSQL commands work perfectly. The thing you are seeing isn't single quotes, it is the "quote thing" on the same button as ~.

Thanks for the advice.
Wow that typo was the entirety of my problem. I stated at it for sooooo long and it was a typo. Thank you very much.
 
You made a spelling error when you initialised $resultarray.

Another thing to watch out for is single quotes in sql queries.. they can't be used on table names or table field names. Only use them on PHP variables or expressions.. e.g.

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

should be:

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

--------

You also need to change the names of the tables so that they don't contain any spaces. This is because spaces will break your PHP queries:

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

should be:
$result1 = mysql_query("SELECT * FROM shelf_1 WHERE Item REGEXP '.*$item.*'");
// Notice the underscore to replace the space.

-----

mysql_numrows

should be:
mysql_num_rows

-----

edit:
// Instead of:
$num=mysql_num_rows($resultarray[$j]);
$i=0;
while($i < $num)
{
$printitem=mysql_result($resultarray[$j]… $i, "Item");
$printamount=mysql_result($resultarray[$… $i, "Amount");
$printcategory=mysql_result($resultarray… $i, "Category");
echo "Item: $printitem, Amount: $printamount, Category: $printcategory<br>";
$i++;
}
// This works but there is a better way..

// Try this:
while ($row = mysql_fetch_assoc
($resultarray[$j])) {
$printitem = $row['Item'];
$printamount = $row['Amount'];
$printcategory = $row['Category'];
echo "Item: $printitem, Amount: $printamount, Category: $printcategory<br>";
}

---
I'm glad it helped! I learnt something also.

One more thing I forgot to mention.. there is a quicker way to initialise an array. There is no need to create lots of variables and then merge them into an array.. instead you can simply do this:

$resultarray = array();
$resultarray[] = mysql_query("SELECT * FROM `shelf 1` WHERE Item REGEXP '.*$item.*'");
$resultarray[] = mysql_query("SELECT * FROM `shelf 2` WHERE Item REGEXP '.*$item.*'");
$resultarray[] = mysql_query("SELECT * FROM `shelf 3` WHERE Item REGEXP '.*$item.*'");
// and so on..
 
Back
Top