简体   繁体   English

如何使用 MySQLi 在 PHP 中获取数据?

[英]How to fetch data in PHP with MySQLi?

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

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?但除此之外,您是否甚至在此处设置了 MySQL 数据库连接? I see $con but is that really working?我看到$con但这真的有效吗?

Also, check if there are errors by adding or die(mysql_error($con)) to your mysqli_query($con, $query) line.另外,通过在mysqli_query($con, $query)行中添加or die(mysql_error($con))来检查是否存在错误。

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作为您的值,如果您不打算进行基本验证,至少可以转义它们:

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

To successfully fetch data from MySQL using mysqli extension in PHP you need to perform more or less three actions: connect, execute prepared statement, fetch data.要使用 PHP 中的 mysqli 扩展成功从 MySQL 获取数据,您需要执行或多或少的三个操作:连接、执行准备好的语句、获取数据。

Connection:联系:

The connection is really simple.连接非常简单。 There should always be only 3 lines of code for opening a connection.打开连接应该总是只有 3 行代码。 You enable error reporting, create new instance of mysqli, and set the correct charset.您启用错误报告,创建 mysqli 的新实例,并设置正确的字符集。

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name');
$mysqli->set_charset('utf8mb4');

Prepared statement准备好的声明

This is the tricky part.这是棘手的部分。 You need to prepare SQL statement to be executed.您需要准备要执行的 SQL 语句。 Careful, never concatenate PHP variables into SQL directly .小心,切勿将 PHP 变量直接连接到 SQL 中 Bind the variables using placeholders.使用占位符绑定变量。 Once the statement is ready you can execute it on the server.一旦语句准备好,您就可以在服务器上执行它。

$stmt = $mysqli->prepare('SELECT * FROM teacher WHERE tremail=?');
$stmt->bind_param('s', $_POST['email']);
$stmt->execute();

Fetch the data获取数据

If your prepared statement should return some results, you need to fetch them and do something with the records.如果你准备好的语句应该返回一些结果,你需要获取它们并对记录做一些事情。 To fetch the result use get_result() .要获取结果,请使用get_result() This will give you an object that you can iterate on to fetch each row one by one.这将为您提供一个对象,您可以对其进行迭代以逐行获取。

$result = $stmt->get_result();
foreach ($result as $row) {
    echo $row['user_id'];
}

If you are only starting learning PHP, please consider learning PDO instead.如果您刚开始学习 PHP,请考虑学习PDO It is easier to use and offers more functionality.它更易于使用并提供更多功能。 Use mysqli only for legacy projects.仅将 mysqli 用于遗留项目。

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 } ?>
$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
  }
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM