Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:19:32+00:00 2026-06-18T02:19:32+00:00

I am trying to use mySQLi for the first time. I have done it

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:19:33+00:00Added an answer on June 18, 2026 at 2:19 am

    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:

        //starting from PHP 8.1
        $value = $result->fetch_column();
        // for older versions:
        $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:

    PHP >= 8.2

    // get a single row
    $sql = "SELECT fullname, email FROM users WHERE id=?";
    $row = $conn->execute_query($query, [$id])->fetch_assoc();
    
    // just a single value
    $sql = "SELECT count(*) FROM users WHERE id=?";
    $count = $conn->execute_query($query, [$id])->fetch_column();
    

    Legacy PHP versions:

    // get a single row
    $query = "SELECT fullname, email FROM users WHERE id=?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    
    // 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();
    $count = $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

    When no variables to be used in the query, you can use the query() method:

    // array
    $user = $conn->query("SELECT * FROM users LIMIT 1")->fetch_assoc();
    // value
    $count = $conn->query("SELECT count(*) FROM users")->fetch_column();
    // value < 8.1
    $count = $conn->query("SELECT count(*) FROM users")->fetch_row()[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use mysqli for the first time because i have some
First time I am trying to use jquery ajax in my php code. I
I'm trying for the first time to create a simple Stored Procedure. I have
I am trying to use AJAX for the first time, and I am having
i'm trying to create a cursor for the first time. I have looked at
I'm just trying to use aspx for the first time and I'm getting a
I'm trying to use ORM for the first time, I've defined the models like
I am trying to use mysql_num_rows to then split the results of my query
I'm trying to use a MySQL database to save player information, but whenever there
I'm trying to use the MySQL CASE statements to select products from a products

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.