0

Hey i am getting Illegal string offset warning in php when i try to store the values from associate array into a table.

When i print the associate array i get this :

Array ( [productName] => iphone [productDesc] => A product from apple [price] => 5 [image] => test/Group/pic.png )

   $db1 = new mysqli('localhost',$user,$pass,$db) or die (" Connection failed  ");
   $query="Select productName,productDesc,price,image from product where productID=1";
   $query_first=mysqli_query($db1,$query) or die("Query not retrieved");

   $query_second=mysqli_fetch_assoc($query_first);
   //print_r($query_second);
   $rows=mysqli_num_rows($query_first);
   echo '<table border="1">';
    echo '<tr>

             <td> Product Name</td>
             <td> Product Description</td>
             <td> Price</td>
             <td> Image </td>
        </tr> ';

    foreach($query_second as $r)
    {


        echo '<tr><td>'.$r['productName'];
        echo '</td><td>'.$r['productDesc'];
        echo '</td><td>'.$r['price'];
        echo '</td><td>'.$r['image'];
        echo '</td></tr>';
    }//end foreach
   echo"</table>";
2
  • 1
    place your mysqli_fetch_assoc in a while rather than a foreach Commented Nov 10, 2015 at 21:24
  • the answer you accepted, is exactly what I meant by ^ Commented Nov 10, 2015 at 22:41

2 Answers 2

1

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

mysqli_result::fetch_assoc -- mysqli_fetch_assoc — Fetch a result row as an associative array

mysqli_mysqli_fetch_assoc returns ONE single row from the database. As you can see in the result of print_r().

The array contains:

Array ( [productName] => iphone [productDesc] => A product from apple [price] => 5 [image] => test/Group/pic.png )

So when you loop through the array in the first iteration $r will contain a string with value "iphone", second loop "A product from apple" and so on.

In your code you are basically saying "from the STRING iphone give me the character at the position 'productName' ", and as you can guess "productName" is not a valid char position in string.

So you need to iterate every row from the table.

<?php
$db1 = new mysqli('localhost',$user,$pass,$db) or die (" Connection failed  ");
$query="Select productName,productDesc,price,image from product where productID=1";
$query_first=mysqli_query($db1,$query) or die("Query not retrieved");

//print_r($query_second);

echo '<table border="1">';
echo '<tr>

     <td> Product Name</td>
     <td> Product Description</td>
     <td> Price</td>
     <td> Image </td>
</tr> ';


while($r=mysqli_fetch_assoc($query_first)) {

    echo '<tr><td>'.$r['productName'];
    echo '</td><td>'.$r['productDesc'];
    echo '</td><td>'.$r['price'];
    echo '</td><td>'.$r['image'];
    echo '</td></tr>';
}//end foreach
echo"</table>";

?>

If you just want 1 single row then you dont need interation

<?php
$db1 = new mysqli('localhost',$user,$pass,$db) or die (" Connection failed  ");
$query="Select productName,productDesc,price,image from product where productID=1";
$query_first=mysqli_query($db1,$query) or die("Query not retrieved");

//print_r($query_second);

echo '<table border="1">';
echo '<tr>

     <td> Product Name</td>
     <td> Product Description</td>
     <td> Price</td>
     <td> Image </td>
</tr> ';


$r=mysqli_fetch_assoc($query_first);

echo '<tr><td>'.$r['productName'];
echo '</td><td>'.$r['productDesc'];
echo '</td><td>'.$r['price'];
echo '</td><td>'.$r['image'];
echo '</td></tr>';

echo"</table>";

?>
Sign up to request clarification or add additional context in comments.

8 Comments

Edit: sorry bad copy paste :D
The image column in the table contains the location of the image file , how would i display that ?
echo "</td><td><imt src='" . $r['image'] . "' alt='' />";
Mark the question as answered, to let people know.
syntax is <img and not <imt
|
0

mysqli_fetch_assoc returns a row so you don't need to loop through it with:

foreach($query_second as $r)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.