0

I am getting a php warring

Warning: Illegal string offset 'item_id' in

trying excute this code

<?php 
error_reporting(0);
include ('includes/db.inc.php');
include ('includes/items.inc.php');

include ('header.php');
 $user_id=$_SESSION['user_id'];


   $sqll= "SELECT 'transcription.item_id' FROM transcription left JOIN items ON 'items.item_id' = 'transcription.item_tra_id';" ;
   $resultt= mysqli_query($conn,$sqll);
   $resultts= mysqli_num_rows($resultt);
   //echo $results; 
   $rers = mysqli_fetch_assoc($resultt);

   foreach ($rers as $rerss) {
    echo $rerss['item_id'] ;
   } 
   ?>

I was searching an found out that I need to to wrap the array key in quotes and that exactly what I have did but still the same issue, also I don't know if my SQL statements is correct.

Mimo_dz
  • 3
  • 2
  • @RiggsFolly This is not a duplicate, there are two issues at hand, the OP has incorrectly attempted to fix the problem by using quotes instead of backticks. The root problem here is a failure to iterate the result set correctly, – Geoffrey Feb 24 '18 at 22:33
  • @Geoffrey the dupe link is fitting for correcting the issue described in the question. You are right there are multiple issues to correct. There are other duplicates available for _how to iterate a resultset_. Opening this question, only to instantly reclose with another dupe is a waste of time. If someone wants to add another dupe link to the list, that's fine but I personally don't support reopening. – mickmackusa Feb 25 '18 at 00:48
  • If this is not longer a duplicate then you need to explain why it is different – New Alexandria Feb 25 '18 at 03:34

1 Answers1

1

You have two problems:

Wrapping the column in single quotes turns it into a string, not a column name, you need to wrap it in backticks, ie:

$sqll= "SELECT `transcription.item_id` FROM transcription left JOIN items ON `items.item_id` = `transcription.item_tra_id`;" ;

You are not iterating the result set correctly.

while($rerss = mysqli_fetch_assoc($resultt))
{
  print_r($rerss);
}

mysqli_fetch_assoc fetches ONE record and returns it as an array of columns, not an array of all records, it has to be called for each record that is in the result set.

See: http://php.net/manual/en/mysqli-result.fetch-assoc.php

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • Maybe an alias on field could help too, but good answer. Why not `while ($rerss = mysqli_fetch_assoc($resultt)) {`? – Syscall Feb 24 '18 at 22:34
  • Now I am receiving another error : Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given – Mimo_dz Feb 24 '18 at 22:37
  • @Mimo_dz Please try the fixed code, I made a mistake myself too :) – Geoffrey Feb 24 '18 at 22:38
  • I have fixed the code and now I am having the same first problem, when I do the num rows and print it I have a result which mean the query is working fine, now I need to print the whole row element using the
  • tag, but once I use the $ress['item_id'] I receive an error : Warning: Illegal string offset 'item_id'
  • – Mimo_dz Feb 24 '18 at 22:42
  • I have solved it, the problem was with the variable "item_id" where I was using it in one of the pages I included as $item_id – Mimo_dz Feb 24 '18 at 22:43
  • Please read up on `print_r` and `var_dump`, these two functions will help you enormously while developing, especially with errors like this. Please don't forget to mark this question as answered also. – Geoffrey Feb 24 '18 at 22:45