0

hi i managed to change my site from mysql to mysqli with all your help

although i have one line thats causing me problems in inbox.php can someone check this out and see whats wrong the error im getting is
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/jktempla/public_html/inbox.php on line 202
heres code for that section

<?php 
      // Fetch user random for display on bottom
      $topuid = array();
      $idtopuid     = implode(',', $topuid);
      $userquery    = mysqli_query($conn, "select * from user where user_id != '".$_SESSION['userid']."' and user_id NOT IN (".$idtopuid.") order by rand() limit 0,5");
      while($fetch_query=mysqli_fetch_array($userquery)) //this is the line concerned
      {
          $show_memtype  = $fetch_query['user_memtype'];
          $show_id      = $fetch_query['user_id']; 
          $fetch_img2   = mysqli_fetch_array(mysqli_query($conn, "select user_image from user_images where user_id = '".$show_id."' and main_image = '1' "));
          $show_img     = $fetch_img2['user_image'];
          ?>

so after reading through the forum on here i so a post where add error die to it so i put the following in

<?php 
      // Fetch user random for display on bottom
      $topuid = array();
      $idtopuid     = implode(',', $topuid);
      $userquery    = mysqli_query($conn, "select * from user where user_id != '".$_SESSION['userid']."' and user_id NOT IN (".$idtopuid.") order by rand() limit 0,5") or die( mysqli_error($conn) );  //line changed
      while($fetch_query=mysqli_fetch_array($userquery))
      {
          $show_memtype  = $fetch_query['user_memtype'];
          $show_id      = $fetch_query['user_id']; 
          $fetch_img2   = mysqli_fetch_array(mysqli_query($conn, "select user_image from user_images where user_id = '".$show_id."' and main_image = '1' "));
          $show_img     = $fetch_img2['user_image'];
          ?>

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 ') order by rand() limit 0,5' at line 1

Thankyou for all your help so far any help is much appreciated

2
Contributors
2
Replies
13
Views
1 Year
Discussion Span
Last Post by janicemurby
1

The problem is generated in the IN() statement: what happens if $topuid is empty, like in your code?

$topuid   = array();
$idtopuid = implode(',', $topuid);

The result of $idtopuid will be an empty string, example:

var_dump(implode(',', []));
string(0) ""

This will generate the syntax error you got: syntax to use near ') order by rand() limit 0,5' at line 1 because the IN() statement cannot be empty.

Also, keep in mind that you are setting something that will display as a single CSV string, for example:

$topuid   = ['a', 'b', 'c'];
$idtopuid = implode(',', $topuid);

# in query
... IN(".$idtopuid.") ...

Will generate:

IN(a,b,c)

without quotes, which will work fine if these will be integers [1,2,3], but it will fail if using alphanumeric IDs, because it will be like testing table columns and it could generate an error like:

ERROR 1054 (42S22): Unknown column 'a' in 'where clause'

to avoid it, in case of alphanumeric IDs, the IN() statement values should be set with quotes around each element and commas to separate them, so:

IN('a', 'b', 'c')

Bye!

Edited by cereal

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.