如何使用MySQLi在PHP中获取数据? [英] How to fetch data in PHP with MySQLi?

查看:73
本文介绍了如何使用MySQLi在PHP中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几次,但未能成功获取正确的语法(根据PHP 5.5.12),无法从数据库中获取单行或多行.

I tried several times but cannot succeed in getting the right syntax—according to PHP 5.5.12 —to fetch single or multiple rows from my database.

session_start();
$con=mysqli_connect("localhost","root","","doortolearn");
if (!$con) {
    echo "Could not connect to DBMS";       
}
    $query="select * from teacher where tremail='$_POST[email]' and trpasssword='$_POST[password]'";
    $result=mysqli_query($con,$query);
    $flag=FALSE;
    while ($row=mysqli_fetch_array($result,MYSQLI_BOTH)) {
        $_SESSION['email']=$row['email'];
        $flag=TRUE;
    }

推荐答案

首先,在$_POST[password]周围没有单引号':

First, you have no single quotes ' around $_POST[password]:

$query = "SELECT * FROM teacher WHERE tremail='". $_POST['email'] ."' and trpasssword='" . $_POST['password'] . "'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$flag = FALSE;
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    $_SESSION['email'] = $row['email'];
    $flag = TRUE;
}

但是,除此之外,您是否还在此处设置了MySQL数据库连接?我看到了$con,但这真的有用吗?

But past that, do you even have a MySQL database connection set here? I see $con but is that really working?

此外,通过在mysqli_query($con, $query)行中添加or die(mysql_error($con))来检查是否存在错误.

Also, check if there are errors by adding or die(mysql_error($con)) to your mysqli_query($con, $query) line.

此外,您有一个$_SESSION值,但是您甚至在脚本的开头都设置了session_start吗?

Also, you have a $_SESSION value, but do you even set session_start at the beginning of your script?

但是我也建议您使用mysqli_stmt_bind_param作为您的值,如果您不打算进行基本验证,至少可以转义它们:

But I also recommend you use mysqli_stmt_bind_param for your values to at least escape them if you are not going to do basic validation:

$query = "SELECT * FROM teacher WHERE tremail=? and trpasssword=?";
mysqli_stmt_bind_param($query, 'ss', $_POST['email'], $_POST['password']);
$result = mysqli_query($con, $query) or die(mysqli_error($con));
$flag = FALSE;
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
    $_SESSION['email'] = $row['email'];
    $flag = TRUE;
}

这篇关于如何使用MySQLi在PHP中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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