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

查看:42
本文介绍了使用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 ud=?";
    $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天全站免登陆