2

I am looping through a column in my localhost MySql table. I am able to print all the values in this table. My goal is turn these values into JSON format so that my android app could read and populate a ListView with them. I seem to be having the problem in setting up the "key" values in my key/values pair required to format a JSON string. I don't know how to use the while loop in order to create these json_encode array with key/values pair in it.

Here's what I have tried so far:

It works but I want my while loop to assign the key/value pairs and create the JSON string so my app could upload it to a ListView.

//all connections to database have been established
$hold = "select email from info";
$h = mysqli_query($connection,$hold);
$a = array();
$d = 0;

while($row = mysqli_fetch_array($h)){
    echo $d.'<pre>'.$row['email'].'<pre>';//this works fine
    $a[d] = $row['email']; //values have been assigned to array that will be    
    //converted to JSON format, but how can I assign the "key" to each value
    //now inside here it says that 'd' is an undefined constant. What could be
    //wrong with the scope of 'd'?
    $d++;
}

Once the keys have been assigned, I just need to convert the array to json format with json_encode(). On my android app, I would retrieve these key/value pairs. So somehow, my app code needs to know what "key" was assigned inside the while loop. It could be keys that are alphabetized so I could follow an ordering that would help me retrieve the values.

If you have any thoughts or ideas please advise.

Thank you

0

3 Answers 3

1

You don't need to index the array yourself if you want keys from 0 to n.

$a[] = $row['email'];

Also d is an undefined constant because you haven't prefixed it with $ (which would make it a reference to a variable).

See PHP docs - Creating/modifying with square bracket syntax

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

Comments

1

You need to know how many key/value pairs are there in the json. You can use count method of php to determine how many are there and put it in the json.

To determine keys, you should use a sequence.

//all connections to database have been established
$hold = "select email from info";
$h = mysqli_query($connection,$hold);
$a = array();
$d = 0;

while($row = mysqli_fetch_array($h)){

$a["x".$d] = $row['email']; //Now you know, that keys will be like x0, x1, x2, x3... 
//converted to JSON format, but how can I assign the "key" to each value
//now inside here it says that 'd' is an undefined constant. What could be
//wrong with the scope of 'd'?
$d++;
}

$a["total"] = count($a); // this gives you total number of key/value pairs

Also, you should remove the 'echo' statement because it is likely to disturb the json output.

At the end, you can use json_encode.

1 Comment

Thank you @EresDev you gave a great idea. I already fixed my problem. Thank you so much.
1

As other comments/answers have said, $a[] = $row['email']; will accomplish this without using $d at all, but if you need to retrieve the values from your database later, using an incremented number like that will probably not work.

Your table should have some sort of unique identifier to reference later when you want to look up records (an autoincrement id would be my choice) that you can use as your key, rather than an unrelated integer generated by PHP. Once you have established this unique id in your database, you can modify your code to use it like this:

$hold = "select id, email from info";
$h = mysqli_query($connection,$hold);
$a = array();

while($row = mysqli_fetch_array($h)){
    $a[ $row['id'] ] = $row['email'];
    //    key             value
}

Just to clarify based on the comments here:

$a[d] = $row['email'];      // d is an undefined constant
$a[$d] = $row['email'];     // value of $d will be used as key (0, 1, 2, etc.)
$a['d'] = $row['email'];    // literal letter 'd' will be used as key (repeatedly)
$a[] = $row['email'];       // PHP automatically assigns incrementing numeric keys (0,1,etc.)

2 Comments

silly mistake of mine not putting single quotes around d. Thanks for the information. I will about putting a reference identifer on the column of this table and use it a key in order to generate the json encoded string. thanks @don'tpanic
Putting single quotes around d will not give you the output you expect, unless you only have one record. Doing that will use a literal 'd' as the key in your $a array, and overwrite it with each iteration of the while loop. If you want to use the $d variable, use $a[$d] instead of $a[d], but this will really have the same effect as just using $a[].

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.