Create a Javascript Array with PHP from a MySQL Database Table

87

Wrote this function to check email addresses already in a database, so I dont have to change pages, and check with javascript. Problem: it exposes your whole email list to the public in the source code, and the source code can become a monster length. So it isnt practical, but it works. (I’m not using it to check emails)

The script proves useful to do other things with javascript and spawn arrays to manipulate data… etc.

function jarray(){

$sql = "SELECT * FROM <strong>table_name</strong> LIMIT 0, 100000000";
$jarray = "var <strong>your_array</strong> = new Array();\n\t\t";
$i = 0;
$q = mysql_query($sql);
while($object = mysql_fetch_object($q)){
$jarray = $jarray."<strong>your_array</strong>[{$i}] = \"{$object-&gt;<strong>field_name</strong>}\";\n\t\t";
$i++;
}

print $jarray."\n";
}

Put your own info where the Text is bold

Article written by developer and designer


1 comment so far ↓

If you found this post useful click the share this button. Contribute below by adding a comment, no registration is required.

  • roronoa zolo says on at 4:25 am comment #

    are you sure wtihout echo “javascript” inside?

Leave a Comment