-1

I’m trying to fill a list with the output from a mysql query. I can deliver 1 item but not more.
PHP code: ('fetchBooks.php')

$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);   
$brand = $_GET['brand'];    
$res = mysqli_query($con, "CALL GetBooks('$brand')");   
$result = array();
while($row = mysqli_fetch_array($res)){
    array_push($result, 
            array('brand'=>$row[0]));
}   
echo json_encode(array('result'=>$result));

Javascript code:

$.getJSON(
    'fetchBooks.php',
    'brand='+ brand,    
    function(result){                       
        const olEle = document.createElement("ol");
        olEle.setAttribute("id", "tomes");
        document.body.appendChild(olEle);
        $.each(result.result, function(){   
            const liEle = document.createElement("li");
            const aEle = document.createElement('a');  
            aEle.innerText = this['brand'];         
                liEle.append(aEle);             
                olEle.append(liEle);
        });
    }       
);

All this works OK but if I try:

array_push($result, array('brand'=>$row[0]), $row[1]);   or
array_push($result, array('brand'=>$row[0]), 'brand'=> $row[1]);

I can’t ever seem to get more than one item in the output. I’ve kicked a few other ideas around but I need to have control over each of the elements of the output so I can position them separately on the webpage eg Title, Author, Description etc I'm aware that the '$.each' may only apply to the whole output of each record not 'each' element of the record. Any ideas or help welcome!

killeme
  • 1
  • 1
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Jul 31 '21 at 12:16
  • Not sure exactly what you expect as a result vs what you got instead since there's no concrete example in your question but wouldn't it just be easier to fetch the result as an associative array and send that directly as a result in your JSON response? It seems like you did a lot of work to reconstruct the array and didn't end up with the desired result. Can you inculde the actual vs desired result in a [reproducible minimal example](http://sscce.org/) – Sherif Jul 31 '21 at 12:53

1 Answers1

0

Can you work out like this

    $result = array();
    while($row = mysqli_fetch_array($res)){
        $result[] = array('brand'=>$row);
    }   
  • OK so I pack the array $result[] with the output of each line, then do I have to change the next line to: echo json_encode(array('result'=>$result[])); and also the start of the javascript will I need 'function(result[]){ '? – killeme Jul 31 '21 at 15:22
  • you can use echo json_encode(array('result'=>$result)) same like your code. in jquery each function $.each(result.result, function(data){ console.log(data); }). check the desired result is coming, after use $.each(result.result, function(data){ var data = $.parseJSON(data); console.log(data.brand); }). – Nantha kumar M Aug 02 '21 at 06:08
  • Thanks for your response Nantha. I used $.each(result.result, function(data){ var data = $.parseJSON(data); console.log(data.brand); }). as suggested and got a console error "GET 192.168.1.162/tests/simple24/fetchBooks.php?brand=/var/www/html/…" That looks pretty much what I would expect to see as the GET command so don't quite understand the error. [that line should read "/var/www/html/books/China" at the end]. If it was working I should expect to see a list of book titles in the category 'China'. – killeme Aug 02 '21 at 13:13
  • To recap: when I changed the php code to: $result = array(); while($row = mysqli_fetch_array($res)){ $result[] = array('brand'=>$row); } . Then I no longer got a list of book titles (but the program still commences. However, it still records the error. $.each(result.result, function(data){ var data = $.parseJSON(data); console.log(data.brand); }) doesnt do anything for me. – killeme Aug 02 '21 at 13:33