如何使用 MySQLi 在 PHP 中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24028697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fetch data in PHP with MySQLi?
提问by Rahul Goel
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.
我尝试了几次,但无法成功获得正确的语法——根据 PHP 5.5.12——从我的数据库中获取单行或多行。
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;
}
回答by JakeGould
First, you have no single quotes '
around $_POST[password]
:
首先,你'
周围没有单引号$_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;
}
But past that, do you even have a MySQL database connection set here? I see $con
but is that really working?
但除此之外,您是否甚至在此处设置了 MySQL 数据库连接?我明白了,$con
但这真的有效吗?
Also, check if there are errors by adding or die(mysql_error($con))
to your mysqli_query($con, $query)
line.
另外,通过添加or die(mysql_error($con))
到您的mysqli_query($con, $query)
行来检查是否有错误。
Also, you have a $_SESSION
value, but do you even set session_start
at the beginning of your script?
另外,您有一个$_SESSION
值,但是您是否甚至session_start
在脚本的开头进行设置?
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:
但我也建议您使用mysqli_stmt_bind_param
for 您的值,如果您不打算进行基本验证,至少可以转义它们:
$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;
}
回答by vivek raj
can You try this code
你可以试试这个代码吗
<?php $query=mysqli_query($connection, "SELECT * FROM user");
while($rows=mysqli_fetch_array($query)){ ?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['age']; ?></td>
<td><?php echo $rows['mobile']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php } ?>
回答by Dewesh Sinha
$r =$mysqli->query("select * from users");
while ( $row = $r->fetch_assoc() )
{
?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['pwd']; ?></td>
</tr>
<?php
}
?>