Reputation: 97
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>";
Upvotes: 0
Views: 1092
Reputation: 694
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>";
?>
Upvotes: 1
Reputation: 924
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
Upvotes: 0