Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP To Javascript Array?
    primarykey
    data
    text
    <p>I have googled and looked at SO for a while now and can't seem to figure this out. The only requirement Im trying to meet is to return a properly formatted javascript array that contains the results of the sql statement. </p> <p>I.E. </p> <p>Given a query:</p> <pre><code>SELECT NUMBERS FROM TABLE </code></pre> <p>And results:</p> <pre><code>NUMBERS 1 2 3 </code></pre> <p>I would like to eventually get back an array like so</p> <pre><code>["1","2","3"] </code></pre> <p>Please help me understand where I am going wrong</p> <p>Here is my php code</p> <pre><code>&lt;?php $mysqli = new mysqli("local.host.com", "user", "pass", "db"); $sql = "SELECT DISTINCT NAME FROM table"; $result = $mysqli-&gt;query($sql); while($row = $result-&gt;fetch_array()) { $rows[] = $row['NAME']; } echo(json_encode($rows)); $result-&gt;close(); /* close connection */ $mysqli-&gt;close(); ?&gt; </code></pre> <p>Here is my javascript:</p> <pre><code> function GetCards() { var cardarray = new Array(); //alert('test'); $.getJSON('getcardlist.php', function(data) { for(var i=0;i&lt;data.length;i++) { cardarray.push(data[i]); } //return cardarray; }); return cardarray; } </code></pre> <p>EDIT:</p> <p>Little more information, Im trying to setup an autocomplete list for jquery ui, this is my setup for the autocomplete widget.</p> <pre><code>var list = GetCards(); $( "#name" ).autocomplete({ source: list, minLength: 2 </code></pre> <p>And this is the error Im getting from chrome console </p> <pre><code>Uncaught TypeError: Cannot read property 'label' of null </code></pre>
    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