0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem with this is that I would constantly be opening and closing my connections for each query (there is a lot of AJAX). Is there no way to open it once, and leave it open for all files? Surely I can leave the connection open and continue with multiple queries. It takes so long to make new connections, that I would rather not have to do that for each query. I was told the connection would terminate itself at the end as long as it is not persistent.
well, I'll mark this answer correct because its been 4 hours, I'm not getting any other responses, and it does work creating a new connection for each query. But, I know there is a better way to do it, so I'll change this answer if a better one comes along.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.