Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Database Array to JSON
    primarykey
    data
    text
    <p>I'm currently searching a MySQL database for results based on an input value. Once the data has been pulled, it is included into an array and than encoded to JSON.</p> <p>At the moment the code works as expected, however it does not loop through every database result, it just pulls the first results that contains the input data, even if there are multiple results that match.</p> <p>For example. if i input the letter "e" into the front end search form, this should get all results that contain the letter "e", however it only gets the first result with the letter "e" rather than looping through the enter table (there are 3 other results that should be pulled).</p> <p>Is there any way to modify my current code to loop correctly and get all results that match the query?</p> <pre><code>$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']); $search_string = $search_db-&gt;real_escape_string($search_string); if (strlen($search_string) &gt;= 1 &amp;&amp; $search_string !== ' ') { $query = 'SELECT kbarticleid,subject FROM swkbarticles WHERE subject LIKE "%'.$search_string.'%"'; $initial = $search_db-&gt;query($query); while($results = $initial-&gt;fetch_array()) { $result_array['kbarticleid'] = $results['kbarticleid']; $result_array['subject'] = $results['subject']; } $match = $result_array['kbarticleid']; $queryf = "SELECT * FROM swkbarticlelinks WHERE kbarticleid = '$match'"; $final = $search_db-&gt;query($queryf); while($results2 = $final-&gt;fetch_array()) { $result_array['linktype'] = $results2['linktype']; } echo json_encode($result_array); } </code></pre> <p>There are 2 database queries as I need to run through 2 different tables.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload