0

Right now I have working a DB connection to mysql. The html -> PHP -> query -> data reception works. I show the relevant code:

From the html file matters:

d3.json("http://path/file.php",   function(error, data) {
    console.log (data); 
});

file.php:

    <?php

    $username = "myusername"; 
    $password = "mypassword";   
    $host = "myhost";
    $database="myDB";

    $server = mysql_connect($host, $username, $password);
    $connection = mysql_select_db($database, $server);

    $myquery = "select * from `mytable`";
    $query = mysql_query($myquery);

    if ( ! $query ) {
        echo mysql_error();
        die;
    }

    $data = array();

    for ($x = 0; $x < mysql_num_rows($query); $x++) {
       $data[] = mysql_fetch_assoc($query);
    }

    echo json_encode($data);     

    mysql_close($server);
    ?>

What I want is to have only 1 .php file instead of 1 php file for every query. That means I need to send from the html a variable inputquery to the php file. I've tried several things such as changing:

`$myquery = "select * from `mytable`; into `$myquery = inputquery`;

And I think that the wrong point is the definition of the function that requests the data from the DB. What I tried (wrong, the following code does not work as expected):

var inputquery = "select * from `mytable`"
d3.json("http://serverhost/path/file.php",  function(error, data) {
    console.log (data); 
});

Maybe this is not working because I am not telling the function I want as an input to the .php file the variable inputquery. I tried to put it inside the function, but got "data is not defined" errors, so I think it is not worth it to show the wrong code.

How can I input that var inputquery to the .php file? It could not be the way I planned it.

Thank you

  • Please don't use `mysql_*` functions to write new code. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Instead you should learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) and use either [PDO](http://www.php.net/pdo) or [MySQLi](http://www.php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. Also see http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Marijke Luttekes Mar 28 '14 at 12:20

3 Answers3

0

You have to send the inputquery variable with the http request as POST data, then in you php file you can do :

$myquery = $_POST['inputquery'];

You surely will find some documentation about sending post data with the request you're sending.

Brovoker
  • 896
  • 5
  • 16
0

This is a very bad idea, since you become very vulnerable to SQL Injection, even so I will try to help you

I assume you have JQuery if you have so

you can do the following html.file

var inputquery = "select * from `mytable`";

$.post("relative/path/to/file.php", 
{query : inputquery},
function (data) {
     alert(data); // See output
},'json);

file.php

<?php

$username = "myusername"; 
$password = "mypassword";   
$host = "myhost";
$database="myDB";

$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);

$myquery = $_POST['query'];
$query = mysql_query($myquery);

if ( ! $query ) {
    echo mysql_error();
    die;
}

$data = array();

for ($x = 0; $x < mysql_num_rows($query); $x++) {
   $data[] = mysql_fetch_assoc($query);
}

echo json_encode($data);     

mysql_close($server);
?>
Ricardo Barros
  • 205
  • 1
  • 8
0

The simplest way is using get parameter in d3.json:

var yourparam = 'mytable';
d3.json("http://path/file.php?query=" + yourparam, function (error, json) {
...
});

You can retrieve the variable from the $_GET array. Finally don't put mysql commmands into your js, and don't use mysql library. It's very dangerous.

socksnake
  • 44
  • 2
  • I said that I'm using MySQL because WAMPserver uses phpMyAdmin that uses it. Does it mean I'm using that library? – PleaseTeachMeHowToDoIt Mar 28 '14 at 12:08
  • The mysql library is the part of the php, which managaes your mysql queries in the php code: http://www.php.net/manual/en/book.mysql.php. This is deprecated, because it makes your code very vulnerable. Use rather [PDO](http://www.php.net/manual/en/book.pdo.php) or [mysqli](http://hu1.php.net/mysqli). The phpMyAdmin only an administration tool for the database server MySQL. – socksnake Mar 28 '14 at 13:08
  • Now I understand more. I'm changing my code to PDO. – PleaseTeachMeHowToDoIt Mar 31 '14 at 14:21