mysqli和AJAX [英] mysqli and AJAX

查看:82
本文介绍了mysqli和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从MySQL切换到MySQLi时,我感到非常困惑,我只是在尝试一个非常基本的示例,以查看是否可以使它正常工作.请原谅使用.post而不是.ajax且现在不返回JSON的糟糕做法,我只是在使用一些旧代码来查看是否可以使查询正常工作.

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.

当前,在index.php上,我创建了一个新连接:

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

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

然后,在我的scripts.js文件上使用jquery,我收集我的变量并将其发送到处理文件:

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);
                }
            );
        }
    });

现在,在我的处理文件loginProcess.php中,进行查询:

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 />';
}

这将导致致命错误:在第8行上的/home/guysandd/public_html/test/v3/ajax/loginProcess.php中的非对象上调用成员函数prepare().我认为这意味着它无法识别我的连接,但是我不确定如何实现该连接.另外,我想知道是否因为参数是独立的,所以是否有必要使用mysqli_real_escape_string.

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:只是为了澄清一点,我正在工作的网站有两个登录帐户:父母和学生.查询返回信息后,我检查他们是父母还是学生,这将更改内容.

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.

推荐答案

您需要编写此部分

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

再次loginProcess.php中,因为没有活动的数据库连接

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

在这种情况下,您将必须创建一个持久连接. 摘自PHP手册

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

与mysql扩展不同,mysqli不提供单独的功能来打开持久连接.要打开持久连接,连接时必须在主机名前加p:.

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.

这篇关于mysqli和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆