Skip to content
Advertisement

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

Ok so I have found a number of tutorials online and have followed each of them step by step. My problem is that I know javascript/jQuery much better than I know PHP and I cannot figure out how to even debug what is going wrong in that section. Basically I have a bunch of buttons and a from and when a button is pressed it determines what the default values are in the form.

jQuery Side

$(document).ready(function(){
// CSPC and ADDUPDATE TOGGLE ARE DEFINED GLOBALLY
    $('ul#parts').on('click', 'button', function(){
        ADDUPDATETOGGLE = "ADD";
        CSPC = $(this).attr("data-cspc");
        var   form = $('div.sidebar form'),
              sr = 0;
        form.find("#cspc").val(CSPC);
        $.ajax({
            type: "GET",
            url: "getRate.php",
            data: "pid=A5843",
            dataType: "json",
            success: function(data){
                sr = data;
            }
        });
        form.find("#strokeRate").val(sr);
        showForm();
    });
});

PHP side

<?php

$host = "localhost";
$user = "username";
$pass = "password";
$databaseName = "movedb";
$tableName = "part parameters";

$con = mysql_connect($host, $user, $pass);
$dbs = mysql_select_db($databaseName, $con);
//get the parameter from URL
$pid=$_GET["pid"];
if (empty($pid)){
    echo "1"; //default rate
}
else{
    $db=mysql_pconnect("localhost");//connect to local database
    mysql_select_db("movedb", $db);//select the database you want to use
    if (!$db){
        echo ("error connecting to database");              
    }
    else{
        //connection successful
        $sql = "SELECT 'Processing Rate (ppm)' FROM 'part parameters' WHERE 'Part Number' LIKE '" . $pid . "'";//sql string command
          $result=mysql_query($sql);//execute SQL string command
          //result contains rows
          $rows = mysql_fetch_row($result)
          echo json_encode($rows["Processing Rate (ppm)"]); 
    }

}

?>

Any ideas why sr is not getting set?

Am I way off base?

I will also shamelessly note that I do not know what $user and $pass should be set to. I cannot find that explained anywhere

Thanks in advance!

EDIT: I followed most of the directions below and now when I run

http://localhost/getRate.php?pid=A5843 

it says “No database selected.” Also, I dont have access to our original MS Access file now (one of my team members has it) but once I get it I will make all the headers into one word headers. This is our first job with web programming/database management so we are constantly learning.

Advertisement

Answer

$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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement