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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.