PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How to return specific column from database results?

 October 11, 2022     database, mysql, php, phpmyadmin     No comments   

Issue

I have a column named "id" in MySQL I have to show the username of that id how to show the "username" column of that id in PHP?

it is database code:

<?php
$conn = mysqli_connect('localhost', 'root', '123456', 'profile');
if (!$conn) {
echo mysqli_connect_errno($conn);
echo mysqli_connect_error($conn);
}

and it is display code :

$get_data = "SELECT * FROM info";
$get_data_query = mysqli_query($conn, $get_data);
$array_data = $get_data_query->fetch_assoc();

$name = $array_data['name'];

Solution

Your database query needs to have a 'where' in it if you are searching for a specific ID.

$get_data = "SELECT * FROM info WHERE id = '1'"; or what ever the id you are looking for. (bear in mind you need to use prepared statements if not hardcoding the query)

In all cases, $array_data will be an array of data (even if it has only one result), so you need to either loop over it to return the values of the columns for each item that is returned.

foreach ($array_data as row){
 echo $row['name']
}

or return the first item from the results

echo current($array_data)['name'];


Answered By - Clint
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing