You will need to use the mysql_connect() , mysql_select_db() functions in PHP to connect to your db. After that, use mysql_query() to select the fields in your user table (if your user table has a name and field identifier, SELECT name, id FROM user ). Then you can get all the information from db using mysql_fetch_assoc() or any other mysql extraction function. Now you need to echo your javascript data on your site, formatted as an array. This is hard to do right, but you can get help from json_encode .
To populate your array with usernames, you would do something like this.
<html> <head> <script type="text/javascript"> var userName = <?php // Connect to MySQL //mysql_connect(); //mysql_select_db(); $d = mysql_query( "SELECT name, id FROM user" ) or die( mysql_error() ); $usernames = array(); while( $r = mysql_fetch_assoc($d) ) { $usernames[] = $r['name']; } echo json_encode( $usernames ); ?>; // Do something with the userName array here </script> </head>
svens
source share