How to fetch data in PHP with MySQLi?(如何使用 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?
此外,通过将 或 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 中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 MySQLi 在 PHP 中获取数据?


基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01