使用mySQLi从数据库获得单个结果 [英] Single Result from Database by using mySQLi
问题描述
我正在尝试首次使用mySQLi.我已经完成了以防万一.循环结果正在显示,但是当我尝试显示单个记录时我被卡住了.这是有效的循环代码.
I am trying to use mySQLi for the first time. I have done it in case of loop. Loop results are showing but i am stuck when i try to show 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)
or die("Could not connect to server " . mysql_error());
mysqli_select_db($conn, $database)
or die("Error: Could not connect to the database: " . mysql_error());
/*Check for Connection*/
if(mysqli_connect_errno()){
// Display Error message if fails
echo 'Error, could not connect to the database please try again again.';
exit();
}
?>
<?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);
?>
<?php // echo 'Name' .$row['ssfullname'] . 'email' . $row['ssemail'] . "\n"; ?>
Name: <?php print $row['ssfullname']; ?>
<br />
Email: <?php print $row['ssemail']; ?>
<br /><br />
<?php
// end loop
}
?>
上面的代码在循环的情况下很好. 现在如何显示第一行或任何记录中的任何记录,任何记录,名称或电子邮件,仅显示单个记录,我将如何处理? 在单个记录的情况下,请考虑删除所有上述循环部分,并显示没有循环的任何单个记录.
Above code is fine in case of loop. Now how do i show single record, any record, name or email, from first row or whatever, just single record, how would i do that? In single record case, consider all above loop part removed and lets show any single record without 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];
以下是针对不同用例的完整示例
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();
// in case you need an array
$row = $result->fetch_assoc();
// OR in case you need just a single value
$row = $result->fetch_row();
$value = $row[0];
可以在我的文章中找到上述过程的详细说明.关于为什么必须遵循此问题的解释,请参见此著名的问题
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);
// in case you need an array
$row = $result->fetch_assoc();
// OR in case you need just a single value
$row = $result->fetch_row();
$value = $row[0];
顺便说一句,尽管在学习没问题的情况下使用原始API,但将来还是可以考虑使用一些数据库抽象库.
它将全部您的数据库代码分成三行:
By the way, although using raw API while learning is okay, consider using some database abstraction library in the future.
It will turn all your database code into 3 lines:
include 'database.class.php';
$db = new DB();
$row = $db->getRow("SELECT ssfullname, ssemail FROM userss ORDER BY ssid LIMIT 1");
这篇关于使用mySQLi从数据库获得单个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!