1

i m trying to build a JSON array from mysql. it does not get the information from mysql

<?php
$host="localhost";
$pwd="";
$user="root";
$db="mydb";

$con=mysqli_connect($host,$user,$pwd,$db) or die('Unable to connect'); 
if(mysqli_connect_error($con))
{
echo"failded to connect";
}
$query = mysqli_query($con,"select * from product");
if ($query)
{
    while($row = mysqli_fetch_array($query))
    {
        $flag[] = $row;

    }
  print(json_encode($flag));


}
mysqli_close($con);
 ?>

Notice: Array to string conversion in C:\wamp\www\new\count.php on line 19

What does this error mean and how do I fix it?

6
  • Which one is line 19? Commented May 15, 2017 at 13:26
  • print(json_encode($flag)); Commented May 15, 2017 at 13:28
  • It must be $flag = $row; instead of $flag[] = $row; as $row is already an array I reckon. Commented May 15, 2017 at 13:29
  • please specify line 19... where is line 19? Commented May 15, 2017 at 13:33
  • print_r($flag).. try this .. Commented May 15, 2017 at 13:34

3 Answers 3

2

Please initialize $flag something like this $flag = array()

hope it was helpful to you.

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

1 Comment

This won't solve the problem though. It will just remove the warning of uninitialized variable. Please update the answer as its not resolving the issue
1

change print to print_r(json_encode($flag)) you won't get any error

1 Comment

write print_r($row) inside loop and check if it shows something then also if not then check database that contains data or not
0

You are converting your array to a json array so you have to use var_dump or print_r().

3 Comments

the result must be Something like that [{"image":"http:\/\/192.168.1.104:81\/new\/image\/laVacheQuiRit.jpg","namee":"la vache qui rit","rank":"190","realName":"fromage vache ","createdBy":"danone","Appearance":"1900","powers":"190"}]
@HamzaAy What is the output if you replace print with var_dump?
Something like thatarray (size=4) 0 => array (size=6) 0 => string '192.168.100.93:81/new/categorie/boisson.jpg' (length=50) 'image' => string '192.168.100.93:81/new/categorie/boisson.jpg' (length=50) 1 => string 'Boisson' (length=7) 'categorie' => string 'Boisson' (length=7) 2 => string '2' (length=1) 'quantite' => string '2' (length=1)

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.