Page 1 of 1

Fetch Array Error :( [Resolved]

Posted: Mon Oct 07, 2013 9:07 am
by Epiales
I can't figure out this error either. I've searched google for an hour, but nothing explains why I get the error, so I can't fix it :x :x

Code: Select all

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login3\use_item.php on line 176
Line 176

Code: Select all

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) { 
use_item.php

Code: Select all

<?php
if (isset($_GET['randid'])) {
$randid=$_GET['randid'];
$sql = "SELECT * FROM inventory WHERE randid='$randid'";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

if ($row['name']) {

} else

$name = $row['name'];
$stats1 = $row['stats'];
$statadd = $row['statadd'];
$type = $row['type'];
$randid=$row['randid'];
$id = $_SESSION['userid'];
    if ($type == "healing") {
    
    $newhp = $statadd + $stats_hpoints;
    if ($newhp > $stats_maxhp) {
    $newhp = $stats_maxhp;
    }
    
$sql = "UPDATE stats set hpoints='$newhp' WHERE id='$id'";
$user_query = mysqli_query($db_conx, $sql);
    
    
$sql = "DELETE from inventory where id='$id' AND randid='$randid' limit 1";
$user_query = mysqli_query($db_conx, $sql);

   
   $stats_hpoints = $newhp;
   $name = $row['name'];
 
  
  echo "<br><br><span id='errormess'><big><center><font color='lime'>     Used " . $name . " and recovered " . $statadd . " health" ."!</center><br></span></big></font>";
 }

if ($type == "revive") {
    
    $newturns = $statadd + $stats_turns;
    if ($newturns > $stats_maxturns) {
    $newturns = $stats_maxturns;
    }

    $sql = "UPDATE stats set turns='$newturns' WHERE id='$id'";
$user_query = mysqli_query($db_conx, $sql);

$sql = "DELETE from inventory where id='$id' AND randid='$randid' limit 1";
$user_query = mysqli_query($db_conx, $sql);

   $name = $row['name'];
   $stats_turns = $newturns;
  
  echo "<br><br><span id='errormess'><big><center><font color='lime'>     Used " . $name . " and recovered " . $statadd . " stamina" ."!</center><br></span></big></font>";
}

?>
</div>
 
 <?php } } ?>

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 9:11 am
by vitinho444
That error appears when your query turns false because it failed.

What i usually try to do is go to phpmyadmin, go to the table you want to query and click on "SQL" tab. Paste there your query and see if it returns anything. It will tell you the mistake :)

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 9:15 am
by Epiales
vitinho444 wrote:That error appears when your query turns false because it failed.

What i usually try to do is go to phpmyadmin, go to the table you want to query and click on "SQL" tab. Paste there your query and see if it returns anything. It will tell you the mistake :)
Okay, so I went to the inventory table... I copied and pasted below:

Code: Select all

$user_query = mysqli_query($db_conx, $sql);
This is all it says:

Code: Select all

 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$user_query = mysqli_query($db_conx, $sql)' at line 1

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 9:18 am
by Epiales
If I run this:

SELECT * FROM inventory WHERE randid='$randid'

It displays no results, which don't know why. All the items have randid...

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 9:31 am
by Epiales
The item is used properly, but I still get that error. Again, not sure why. I tried adding an error " or die " message, but it didn't display anything but the same error in the original post. But it dies delete the item and uses it correctly. Just not sure why the error :x :x

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 9:56 am
by vitinho444
Nope, you need to copy this:
SELECT * FROM inventory WHERE randid='$randid'
But give some value to randid because phpmyadmin doesn't recognize your variable.

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 9:59 am
by Epiales
vitinho444 wrote:Nope, you need to copy this:
SELECT * FROM inventory WHERE randid='$randid'
But give some value to randid because phpmyadmin doesn't recognize your variable.
Yeah, if I put one of the randid numbers that are in the inventory item, it works:

Code: Select all

SELECT * FROM `inventory` WHERE `randid`=399384145
That picks the item that has that randid number assigned to it.

So I still have no clue what's wrong :(

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 10:52 am
by vitinho444
Hum... then it must be something about the way you are querying .. I never used mysqli like that sorry :(

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 11:27 am
by Epiales
Grr, frustrating. The "revive" type works perfectly. The "healing" type is the one giving the error. Why? Grrrrrrrr!!!!!!!

use_item.php

Code: Select all

<?php
    
$randid=$_GET['randid'];

$sql = "SELECT * FROM inventory WHERE randid='$randid'";
$user_query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($user_query);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

if (!$row['name']) {

} else {

$name = $row['name'];
$stats1 = $row['stats'];
$statadd = $row['statadd'];
$type = $row['type'];
$randid=$row['randid'];
$id = $_SESSION['userid'];
    if ($type == "healing") {
    
    $newhp = $statadd + $stats_hpoints;
    if ($newhp > $stats_maxhp) {
    $newhp = $stats_maxhp;
    }
    
$sql = "UPDATE stats set hpoints='$newhp' WHERE id='$id'";
$user_query = mysqli_query($db_conx, $sql);
        
$sql = "DELETE from inventory where id='$id' AND randid='$randid' limit 1";
$user_query = mysqli_query($db_conx, $sql);

   $stats_hpoints = $newhp;
   $name = $row['name'];
 
  echo "<br><br><span id='errormess'><big><center><font color='lime'>     Used " . $name . " and recovered " . $statadd . " health" ."!</center><br></span></big></font>";
 }
}
}

if ($type == "revive") {
    
    $newturns = $statadd + $stats_turns;
    if ($newturns > $stats_maxturns) {
    $newturns = $stats_maxturns;
    }

    $sql = "UPDATE stats set turns='$newturns' WHERE id='$id'";
$user_query = mysqli_query($db_conx, $sql);

$sql = "DELETE from inventory where id='$id' AND randid='$randid' limit 1";
$user_query = mysqli_query($db_conx, $sql);

   $name = $row['name'];
   $stats_turns = $newturns;
  
  echo "<br><br><span id='errormess'><big><center><font color='lime'>     Used " . $name . " and recovered " . $statadd . " stamina" ."!</center><br></span></big></font>";
}

?>
So I'm still getting this error...

Code: Select all

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login3\use_item.php on line 176

Re: Fetch Array Error :(

Posted: Mon Oct 07, 2013 12:49 pm
by vitinho444
I don't know the exact mistake but i see your queries are not perfectly written.

what i do and phpmyadmin does is:

Code: Select all

SELECT * FROM `table` WHERE `field`='$field' ORDER BY `field2` DESC
So you can see that i use ` to indicate the fields and single quotes for php variables. Of course this is if for the whole query you use double quotes. You can't use just one in both query and variables.

Try to format your query like that and see if there's still an error.