Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single result from database using mysqli

Tags:

loops

php

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.

like image 568
Hiroshi Rana Avatar asked Jan 31 '13 11:01

Hiroshi Rana


People also ask

How can you retrieve a particular row of data from a set of MySQLi results?

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

What is MySQLi -> Real_connect?

Definition and Usage. The real_connect() / mysqli_real_connect() function opens a new connection to the MySQL server. This function differs from connect() in the following ways: real_connect() requires a valid object created by init() real_connect() can be used with options() to set different options for the connection.


2 Answers

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

like image 178
Your Common Sense Avatar answered Sep 23 '22 04:09

Your Common Sense


Use mysqli_fetch_row(). Try this,

$query = "SELECT ssfullname, ssemail FROM userss WHERE user_id = ".$user_id; $result = mysqli_query($conn, $query); $row   = mysqli_fetch_row($result);  $ssfullname = $row['ssfullname']; $ssemail    = $row['ssemail']; 
like image 22
Edwin Alex Avatar answered Sep 23 '22 04:09

Edwin Alex