Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm doing a website and now i need that a php class returns an array containing the result of a query in this mode with the key that is the name of the column on the db, and the value that is the value stored in db for that key. The result set contains only a row .

I'm trying in this mode .

$nomePG = $_POST['nomePG'];
$querys = mysqli_query($db_connect, "SELECT * FROM user WHERE nomeutente like '%$nomePG%'");
$queryRicerca = mysqli_fetch_array($querys);
$arrayReturn = array(); // This is the array to encode with JSON
$keys = array_keys($queryRicerca); 

        $i=0;
        $count=count($keys);
        while ($i < $count) {
            $arrayReturn[$i]=$keys[$i]->$queryRicerca[$keys[$i]];
        }              
          echo json_encode($arrayReturn);

... what am I doing wrong?

The SQL Query returns something like this :

Name : Mark Age : 13

And i want that in arrayReturn the key is Name and the value is Mark, for example ... and all must be in JSON format at the end ( with function json_encode ? ).

Thanks for your help!

share|improve this question
    
You should either escape your variables from POST, or use parameterisation - as it stands you have a SQL injection vulnerability in this code. –  halfer Apr 12 at 16:07
    
Also, I'm not sure what you are asking here. What you say the query returns, and what you want, seem to be the same thing. Can you add more detail, such as a print_r() of the output array as it stands? –  halfer Apr 12 at 16:09
    
When i try json_encode on the $queryRicerca, that is the array that already contains these info, the page that receives this associative array receives "empty" , so i need another array that will be converted in json format. The array must contain key and value, the key is the name of the db column, the value is the value of that column . –  user2854276 Apr 12 at 16:14
    
Please add that into your question, and then delete the comment. If you can add a print_r that really does help, though - still am not quite sure what you mean. Include a dump of what you get, and a dump of what you want. –  halfer Apr 12 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.