Mysqli fetch single row php
When just a single result is needed, then no loop should be used. Just fetch the row right away. Show
The last example will return the first column from the first returned row, or
Below are complete examples for different use cases Variables to be used in the queryWhen variables are to be used in the query, then a prepared statement must be used. For example, given we have a variable
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 No variables in the queryIn your case, where no variables to be used in the query, you can use the
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:
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). ❮ PHP MySQLi Reference Example - Object Oriented styleFetch rows from a result-set: $mysqli = new mysqli("localhost","my_user","my_password","my_db"); if ($mysqli -> connect_errno) { $sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname"; if ($result = $mysqli -> query($sql)) { $mysqli -> close(); Look at example of procedural style at the bottom. Definition and UsageThe fetch_row() / mysqli_fetch_row() function fetches one row from a result-set and returns it as an enumerated array. SyntaxObject oriented style:$mysqli_result -> fetch_row() Procedural style:Parameter Values
Technical Details
Example - Procedural styleFetch rows from a result-set: $con = mysqli_connect("localhost","my_user","my_password","my_db"); if (mysqli_connect_errno()) { $sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname"; if ($result = mysqli_query($con, $sql)) { mysqli_close($con); ❮ PHP MySQLi Reference mysqli_fetch_row(PHP 5, PHP 7, PHP 8) mysqli_result::fetch_row -- mysqli_fetch_row — Fetch the next row of a result set as an enumerated array DescriptionObject-oriented style public mysqli_result::fetch_row(): array|null|false mysqli_fetch_row(mysqli_result
Return Values Returns an enumerated array representing the fetched row, ExamplesExample #1 mysqli_result::fetch_row() example Object-oriented style
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Procedural style
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); The above examples will output something similar to: Pueblo (USA) Arvada (USA) Cape Coral (USA) Green Bay (USA) Santa Clara (USA) See Also
Stephen ¶ 15 years ago
sainthyoga2003 at gmail dot com ¶ 8 years ago
evangun2001 at yahoo dot fr ¶ 15 years ago
maillist at pnpitalia.it ¶ 18 years ago
How can I get single record in PHP?Show activity on this post. $link = mysql_connect('localhost','root','yourPassword') mysql_select_db('database_name', $link); $sql = 'SELECT id FROM games LIMIT 1'; $result = mysql_query($sql, $link) or die(mysql_error()); $row = mysql_fetch_assoc($result); print_r($row);
How can we fetch data from database row by row in PHP?The fetch_row() / mysqli_fetch_row() function fetches one row from a result-set and returns it as an enumerated array.
How can I get one record details in MySQL database using PHP?Displaying records using MySQLI functions
php"; // MySQL connection string $count="SELECT name,id,class,mark,sex FROM student LIMIT 10"; if($stmt = $connection->query($count)){ echo "
Bài Viết Liên QuanQuảng CáoCó thể bạn quan tâmToplist được quan tâm#1
Top 4 uống nước chanh sả mật ong có tác dụng gì 20235 tháng trước#2
Top 10 bài tập làm văn số 5 lớp 7 de 4 20235 tháng trước#3
Top 3 vừa chơi đã có tài khoản vương giả chap 1 20235 tháng trước#4
Top 6 anh sẽ on thôi cover phạm nguyên ngọc lyrics 20235 tháng trước#6
Top 7 hãy ra khỏi người đó đi hợp âm 20235 tháng trước#7
Top 6 giáo án thơ về thăm nhà bác 20235 tháng trước#8
Top 8 giáo án ngữ văn 6 cánh diều 20235 tháng trước#9
Top 9 tinh bột tham gia phản ứng nào 20235 tháng trướcQuảng cáoXem NhiềuQuảng cáoChúng tôiTrợ giúpBản quyền © 2021 Hàng Hiệu Inc.
|