Jump to content

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean..


5kyy8lu3

Recommended Posts

Hi.  I keep getting this stupid error.  It's happened to me on several occasions and I'd like to get an answer on what I'm doing wrong so I can put an end to this annoying error.  Here's the code, it's for my pictures pagination:

 

$query = "SELECT * FROM " . $_SESSION['logname'] . "_data";
$result = mysqli_query($cxn, $query);
$num_rows = mysqli_num_rows($result);     //This checks to see total number of pics in the table

$curpic = explode(".", $_GET['pic']);   // pulls the number off of the filename so it can be compared
if ( $curpic['0'] > 1 )                      //Checks to see if it's first pic (if it is, it doesn't give a link for "previous picture")

{
	$query2 = 'SELECT filename FROM ' . $_SESSION['logname'] . '_data WHERE filename < ' . $_GET['pic'] . ' LIMIT 1';
	$result2 = mysqli_query($cxn, $query2);
	$nextpage = mysqli_fetch_assoc($result2);
	echo '<a href="pic.php?pic=' . $nextpage['filename'] . '">Previous Picture</a> - ';
}

else

{
	echo '<font color="gray"><i>Previous Picture</i></font> - ';
}

 

$_SESSION['logname'] is the username, that plus the _data is the name of the table the picture data is in

 

$_GET['pic'] is the filename of the current picture (example: pic.php?pic=12.jpg)

 

any ideas? thanks

 

oh and here's the full error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /hermes/bosweb/web191/b1913/ipw.kloudzco/public_html/pic.php on line 114

check for errors in your SQL by changing your code to:

 

$query = "SELECT * FROM " . $_SESSION['logname'] . "_data";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cnx));
$num_rows = mysqli_num_rows($result);     //This checks to see total number of pics in the table

$curpic = explode(".", $_GET['pic']);   // pulls the number off of the filename so it can be compared
if ( $curpic['0'] > 1 )                      //Checks to see if it's first pic (if it is, it doesn't give a link for "previous picture")

   {
      $query2 = 'SELECT filename FROM ' . $_SESSION['logname'] . '_data WHERE filename < ' . $_GET['pic'] . ' LIMIT 1';
      $result2 = mysqli_query($cxn, $query2) or die(mysqli_error($cnx));
      $nextpage = mysqli_fetch_assoc($result2);
      echo '<a href="pic.php?pic=' . $nextpage['filename'] . '">Previous Picture</a> - ';
   }

else

   {
      echo '<font color="gray"><i>Previous Picture</i></font> - ';
   }

check for errors in your SQL by changing your code to:

 

 

here's the error I get now when I added that

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /hermes/bosweb/web191/b1913/ipw.kloudzco/public_html/pic.php on line 92

 

 

 

oops...you use cxn not cnx:

 

$query = "SELECT * FROM " . $_SESSION['logname'] . "_data";
$result = mysqli_query($cxn, $query) or die(mysqli_error($cxn));
$num_rows = mysqli_num_rows($result);     //This checks to see total number of pics in the table

$curpic = explode(".", $_GET['pic']);   // pulls the number off of the filename so it can be compared
if ( $curpic['0'] > 1 )                      //Checks to see if it's first pic (if it is, it doesn't give a link for "previous picture")

   {
      $query2 = 'SELECT filename FROM ' . $_SESSION['logname'] . '_data WHERE filename < ' . $_GET['pic'] . ' LIMIT 1';
      $result2 = mysqli_query($cxn, $query2) or die(mysqli_error($cxn));
      $nextpage = mysqli_fetch_assoc($result2);
      echo '<a href="pic.php?pic=' . $nextpage['filename'] . '">Previous Picture</a> - ';
   }

else

   {
      echo '<font color="gray"><i>Previous Picture</i></font> - ';
   }

oops...you use cxn not cnx:

 

well it turns out I should've added quotes around the string i was comparing it to, so it was an sql error, appreciate the help

 

now my next problem, my pagination works fine moving up one picture but the link to go down one picture always shows 1.jpg for the link, any ideas? lol the code is the same as the one I posted in my original post only i had a less than sign for the comparison

 

i'm guessing it just finds the very first one in the table "1.jpg" being meeting the comparison criteria so it uses that, but I need the one just before the one i'm on, know how to do this? i'm lost lol

 

UPDATE: oh and if I'm at 1 and hit next, i goes to 10, and if i'm at 2 and hit next, it goes to 20 lol, i obviously need a better algorithm, i had it working fine until i changed stuff around to offset being able to delete a picture and having the pagination work fluidly around it

yeah...your SELECT statement is off, it should be something like:

      $query2 = 'SELECT filename FROM ' . $_SESSION['logname'] . '_data WHERE filename < ' . $_GET['pic'] . ' ORDER BY id DESC LIMIT 1';

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.