radleybobins
radleybobins

Reputation: 981

mysqli and AJAX

I am getting pretty confused trying to switch from MySQL to MySQLi, and I'm just trying a very basic example to see if I can get it to work. Please forgive the poor practice of using .post instead of .ajax and not returning JSON for now, I'm just using some old code to see if I can get queries to work.

Currently, on index.php, I create a new connection:

$connection = new mysqli($host, $username, $password, $database);
if (mysqli_connect_errno()) die(mysqli_connect_error());

Then, using jquery on my scripts.js file, I collect my variables and send them to a processing file:

    $("#toolbarTitle-Login").click(function() {
        var loginEmail = $("#toolbarTextbox-Email").val();
        var loginPassword = $("#toolbarTextbox-Password").val();
        if (loginEmail != "") {
            $.post(
                'ajax/loginProcess.php', 
                {
                    'email': loginEmail,
                    'password': loginPassword
                },
                function (response) {
                    $(".testingBox").html(response);
                }
            );
        }
    });

Now, in my processing file, loginProcess.php, I make a query:

$passwordMd5 = md5($_POST["password"]);
$email = $connection->$_POST["email"];
$password = $connection->$passwordM5;

$sql = $connection->prepare("SELECT clientId, studentEmail, parentEmail, school
                                                    FROM clients
                                                    WHERE (studentEmail = ? AND studentPassword = ?) OR (parentEmail = ? AND parentPassword = ?);");

$statement->bind_param("ssss",$email,$password,$email,$password);

$row["id"] = 0;
$row["studentEmail"] = '';
$row["parentEmail"] = '';
$row["school"] = '';

$statement->bind_result($row["id"], $row["studentEmail"], $row["parentEmail"], $row["school"]);
$statement->execute();

while($statement->fetch()) {
    print  $row["id"].' '.$row["studentEmail"].' '.$row["parentEmail"].' '.$row["school"].'<br />';
}

This results in "Fatal Error: Call to a member function prepare() on a non-object in "/home/guysandd/public_html/test/v3/ajax/loginProcess.php on line 8". I assume this means that it is not recognizing my connection, but I'm not sure how I can get it to do that. Also, I was wondering if mysqli_real_escape_string is necessary because the params are separate.

edit: Just to clarify a little, the site I am working on has accounts with two logins: parent and student. After the query returns information I check to see if they are a parent or student and this changes the content.

Upvotes: 0

Views: 2321

Answers (1)

Atif
Atif

Reputation: 10880

You need to write this part

$connection = new mysqli($host, $username, $password, $database);
if (mysqli_connect_errno()) die(mysqli_connect_error());

in loginProcess.php again as there is no active DB connection

Edit

In that case you will have to create a persistent connection. From PHP Manual

Unlike the mysql extension, mysqli does not provide a separate function for opening persistent connections. To open a persistent connection you must prepend p: to the hostname when connecting.

Upvotes: 1

Related Questions