Get single record in mysqli php

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;
    

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

Variables to be used in the query

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

No variables in the 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;

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].

❮ PHP MySQLi Reference

Example - Object Oriented style

Fetch rows from a result-set:


Look at example of procedural style at the bottom.

Definition and Usage

The fetch_row[] / mysqli_fetch_row[] function fetches one row from a result-set and returns it as an enumerated array.

Syntax

Object oriented style:

$mysqli_result -> fetch_row[]

Procedural style:

Parameter Values

ParameterDescription
result Required. Specifies a result set identifier returned by mysqli_query[], mysqli_store_result[] or mysqli_use_result[]

Technical Details

Return Value:PHP Version:
Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result set
5+

Example - Procedural style

Fetch rows from a result-set:


❮ PHP MySQLi Reference


We have seen how to display records of a mysql table here. Now we will learn how to display one record in a single page. This is required where full details of a record are to be shown in a page. Usually the records unique id is used to collect the details from the table. So same php page is used and the value of unique id of the record is taken as a variable. Before that links to different records are displayed and on click the full details are shown. So we will fist start with displaying a group of records with link to individual records.

Connecting database and executing Query

To manage data we have to connect to MySQL database and execute query to get our date. Here there are two ways to use PHP drivers to connect to MySQL and execute the functions for getting records.

One is using Portable Data Object [ PDO ]
Second one is MySQLI [ MysQL Improved ]

We will learn both here. We will first use PDO and at the end we will use MySQLI. You can use any one for your script.

Displaying Links to individual records

We will use our student table where we have added two more columns [ fields ] to store address and image. As we have more records so to restrict number of records we will display records of a particular class. The query is here.
select * from student where class='Four' order  by  id
Using the above query we will display the records by keeping them inside an html table. At the top of the script we will connect to MySQL database.
Here is the code.






Chủ Đề