Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

suppose i have an javascript array "userName".I want to load it from a database table named "user".

Can anyone help with idea or sample code?

Thanks

share|improve this question
1  
Do you mean the content of your array is stored in database, or you want to get the code to create the array from the database ? – Guillaume Lebourgeois Jul 20 '10 at 9:42
    
Usually Javascript cannot talk directly to a database, you'll need something in between. How are you talking to the database? – deceze Jul 20 '10 at 9:43
    
@Guillaume:I want to get code to create array from database @deceze:i use php.. – Rony Jul 20 '10 at 9:46
up vote 5 down vote accepted

Use ajax and php as an intermediate :

  1. Define an empty JS array :

    var myarray = new Array();

  2. use ajax to call your php script, javascript will eval the output by PHP (note this uses prototype library):

     new Ajax.Request('myfile.php', {
    
        onSuccess : function(xmlHTTP) {
    
            eval(mlHTTP.responseText);
        }
    
    });
    
  3. Write your PHP code to grab data and print JS code (it must be valid javscript !) :

    $i=0; $q=mysql_query('select ..');
    
    while($row=mysql_fetch_array($q)){               
    
        echo "myarray[".$i."]='".$row['data']."';";
    
        $i++;  
    }
    
  4. You can check that your Js array contains data :

    alert(myarray.length); 
    

hope this helps.

share|improve this answer

This creates a global user variable in your page.

<?php
    $user = /* get information from database the way you usually do */;
    // $user == array('id' => 1, 'name' => 'foo', ...);
?>

<script type="text/javascript" charset="utf-8">
    var user = <?php echo json_encode($user); ?>;
</script>

If you're looking for a dynamic AJAXy solution, follow some tutorials about AJAX.

share|improve this answer
    
i actually don't want mix of javascript php code to do this...i have separate javascript and php script and from javascript i want to call php script to get database data.... – Rony Jul 20 '10 at 9:58
1  
@Rony In that case you'll have to brush up on AJAX, which you can use to load some information from the server. If the user variable doesn't change though, this method is better and saves an extra roundtrip to the server. – deceze Jul 20 '10 at 10:01

You'll have 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 the fields name and id, SELECT name, id FROM user). Then you can fetch all infos from the db with mysql_fetch_assoc() or any other mysql fetch function. Now you need to echo your data into the javascript on your website, formatted as an array. This is complicated to get right, but you can get help from json_encode.

To fill your array with the user names, you'd 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>
share|improve this answer

The best way to do that simply is to get your data in your php code, and then "write" the javascript code to generate the array in php.

A short example would be :

echo "a = new Array();";
foreach($row in $results)
{
  echo "a[$row[0]] = $row[1];";
}

My code could be quite incorrect, it's juste to give you a quick example.

You could also do that in a more elegant way, using json.

share|improve this answer
    
i actually don't want mix of javascript php code to do this...i have separate javascript and php script and from javascript i want to call php script to get database data.... – Rony Jul 20 '10 at 9:58
    
You can then make an ajax request to a piece of php code, which would give you the data in json, xml, or anything you like. – Guillaume Lebourgeois Jul 20 '10 at 10:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.