我尝试了多次,但无法成功获得正确的语法——根据 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?
此外,通过将 或 die(mysql_error($con))
添加到您的 mysqli_query($con, $query)
行来检查是否存在错误.
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 中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!