使用 mysqli 来自数据库的单个结果 [英] Single result from database using mysqli

查看:25
本文介绍了使用 mysqli 来自数据库的单个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用 mySQLi.我已经在循环的情况下做到了.循环结果正在显示,但当我尝试显示单个记录时卡住了.这是有效的循环代码.

I am trying to use mySQLi for the first time. I have done it in the case of loop. Loop results are showing but I am stuck when I try to show a single record. Here is loop code that is working.

<?php
// Connect To DB
$hostname="localhost";
$database="mydbname";
$username="root";
$password="";

$conn = mysqli_connect($hostname, $username, $password, $database);
?>

<?php
$query = "SELECT ssfullname, ssemail FROM userss ORDER BY ssid";
$result = mysqli_query($conn, $query);
$num_results = mysqli_num_rows($result);
?>

<?php
/*Loop through each row and display records */
for($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc($result);
?>

Name: <?php print $row['ssfullname']; ?>
<br />
Email: <?php print $row['ssemail']; ?>
<br /><br />

<?php 
// end loop
} 
?>

如何从第一行或其他任何内容显示单个记录、任何记录、姓名或电子邮件,仅显示单个记录,我该怎么做?在单条记录的情况下,考虑删除上述所有循环部分,让我们显示没有循环的任何单条记录.

How do I show a single record, any record, name, or email, from the first row or whatever, just a single record, how would I do that? In a single record case, consider all the above loop part removed and let's show any single record without a loop.

推荐答案

当只需要一个结果时,不应使用循环.立即获取该行.

When just a single result is needed, then no loop should be used. Just fetch the row right away.

  • 如果您需要将整行提取到关联数组中:

  • In case you need to fetch the entire row into associative array:

  $row = $result->fetch_assoc();

  • 如果你只需要一个值

  • in case you need just a single value

      $row = $result->fetch_row();
      $value = $row[0] ?? false;
    

  • 最后一个示例将返回第一行返回的第一列,如果没有返回行,则返回 false.也可以缩短为一行,

    The last example will return the first column from the first returned row, or false if no row was returned. It can be also shortened to a single line,

    $value = $result->fetch_row()[0] ?? false;
    

    以下是不同用例的完整示例

    Below are complete examples for different use cases

    如果要在查询中使用变量,则必须使用准备好的语句.例如,假设我们有一个变量 $id:

    When variables are to be used in the query, then a prepared statement must be used. For example, given we have a variable $id:

    $query = "SELECT ssfullname, ssemail FROM userss WHERE id=?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    
    // in case you need just a single value
    $query = "SELECT count(*) FROM userss WHERE id=?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $value = $result->fetch_row()[0] ?? false;
    

    上述过程的详细解释可以在我的文章中找到.至于为什么你必须遵循它在这个著名的问题

    The detailed explanation of the above process can be found in my article. As to why you must follow it is explained in this famous question

    在您的情况下,查询中没有要使用的变量,您可以使用 query() 方法:

    In your case, where no variables to be used in the query, you can use the query() method:

    $query = "SELECT ssfullname, ssemail FROM userss ORDER BY ssid";
    $result = $conn->query($query);
    // in case you need an array
    $row = $result->fetch_assoc();
    // OR in case you need just a single value
    $value = $result->fetch_row()[0] ?? false;
    

    顺便说一句,虽然在学习时使用原始 API 是可以的,但将来考虑使用一些数据库抽象库或至少一个辅助函数:

    By the way, although using raw API while learning is okay, consider using some database abstraction library or at least a helper function in the future:

    // using a helper function
    $sql = "SELECT email FROM users WHERE id=?";
    $value = prepared_select($conn, $sql, [$id])->fetch_row[0] ?? false;
    
    // using a database helper class
    $email = $db->getCol("SELECT email FROM users WHERE id=?", [$id]);
    

    如您所见,虽然辅助函数可以减少代码量,但类的方法可以将所有重复的代码封装在里面,让您只编写有意义的部分——查询、输入参数和所需的结果格式(以方法名称的形式).

    As you can see, although a helper function can reduce the amount of code, a class' method could encapsulate all the repetitive code inside, making you to write only meaningful parts - the query, the input parameters and the desired result format (in the form of the method's name).

    这篇关于使用 mysqli 来自数据库的单个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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