0

I'm trying to make javascript function returning mysql respond to make database querys faster, easier and with less code. This code responds with nothing, it just doesnt give anything with no error.

echo "console.log('$js_array');";

Can someone explain me why and how to fix that?

<?php
function myQuery($query)
{
    $server_name='myserver.com';
    $user_name='admin';
    $password ='admin';
    $db_name='myDBName';

    $db = mysql_connect($server_name,$user_name,$password);
    if($db)
    {
        mysql_select_db($db_name);
        $sql = mysql_query($query);
        $respond = array();
        while($row = mysql_fetch_assoc($sql)) $respond[]=$row;
        $js_array = json_encode($respond);
        echo "console.log('$js_array');";
    }
}
?>

var databaseQuery= <?php echo myQuery("SELECT * FROM myTable");?>
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 12 '16 at 17:18
  • 1
    Typically you would use AJAX to connect to PHP / MySQL to ask for and get data. Your code, as it stands, will never work. – Jay Blanchard Feb 12 '16 at 17:19
  • I didn't know about that, I'm new in php. I'm starting learning about prepraed statements now! Thanks. – stevenhawkingsbiggestfan Feb 12 '16 at 17:23
  • 1
    BTW, *I am Stephen Hawkings Biggest Fan!* But Feyman is my real favorite. ¯\\_(ツ)_/¯ – Jay Blanchard Feb 12 '16 at 17:24
  • Damn, I hope that it will not stop you from giving an answer for my next stupid question. : / – stevenhawkingsbiggestfan Feb 12 '16 at 17:27
  • Of course not! And your question is not stupid. – Jay Blanchard Feb 12 '16 at 17:28
  • And, *I'm Biggest Fan Of Fred-ii* `:D` – Nana Partykar Feb 12 '16 at 17:32
  • I tried using PDO and I got to the point when it returns empty array. file.php: http://pastebin.com/SwhUWSCa pdo_connect.php: http://pastebin.com/dFikHDRD Output: http://s29.postimg.org/iavuxbsgn/screenshot_1766.png – stevenhawkingsbiggestfan Feb 15 '16 at 15:57

0 Answers0