jQuery calls a PHP file to get data from mysql database? jQuery calls a PHP file to get data from mysql database? database database

jQuery calls a PHP file to get data from mysql database?


$user and $pass should be set to your MySql User's username and password.

I'd use something like this:

JS

success: function(data){             if(data.status === 1){                 sr = data.rows;             }else{                 // db query failed, use data.message to get error message             }        }

PHP:

<?php    $host = "localhost";    $user = "username";    $pass = "password";    $databaseName = "movedb";    $tableName = "part parameters";    $con = mysql_pconnect($host, $user, $pass);    $dbs = mysql_select_db($databaseName, $con);    //get the parameter from URL    $pid = $_GET["pid"];    if(empty($pid)){        echo json_encode(array('status' => 0, 'message' => 'PID invalid.'));    } else{        if (!$dbs){            echo json_encode(array('status' => 0, 'message' => 'Couldn\'t connect to the db'));               }        else{            //connection successful            $sql = "SELECT `Processing Rate (ppm)` FROM `part parameters` WHERE `Part Number` LIKE `" . mysqli_real_escape_string($pid) . "`"; //sql string command            $result = mysql_query($sql) or die(mysql_error());//execute SQL string command            if(mysql_num_rows($result) > 0){                $rows = mysql_fetch_row($result);                echo json_encode(array('status' => 1, 'rows' => $rows["Processing Rate (ppm)"]);            }else{                echo json_encode(array('status' => 0, 'message' => 'Couldn\'t find processing rate for the give PID.'));               }        }    }?>

As another user said, you should try renaming your database fields without spaces so part parameters => part_parameters, Part Number => part_number.

If you're still having trouble then (as long as it's not a production server) put this at the top of your php file:

error_reporting(E_ALL);ini_set('display_errors', '1');

This will output any errors and should help you work out what's going wrong.


Your DB query code is incorrect:

    $sql = "SELECT 'Processing Rate (ppm)' FROM 'part parameters' WHERE 'Part Number' LIKE '" . $pid . "'";//sql string command

using ' to quote things in the query turns them into STRINGS, not field/table names. So your query is syntactically and logically wrong. Your code is simply assuming success, and never catches the errors that mysql will be spitting out.

The query should be:

SELECT `Processing Rate (ppm)`FROM `part parameters`WHERE `Part Number` = '$pid'

Note the use of backticks (`) on the field/table names, and the use of single quotes (') on the $pid value.

Then you execute the query with:

$result = mysql_query($sql) or die(mysql_error());

If this fails, you will get the error message that mysql returns.

And in the grand scheme of things, your code is vulnerable to SQL injection attacks. Better read up and learn how to prevent that before you go any farther with this code.


sr it's outside the success callback function. Start putting it into the success function and see what happens

$.ajax({        type: "GET",        url: "getRate.php",        data: "pid=A5843",        dataType: "json",        success: function(data){            sr = data;            form.find("#strokeRate").val(sr);        }    });

remember that, if data is expected to be a json, it will become a js object, so you will not be able to use data directly