Reputation: 94
I have a table like
I want to write a query in php so that I just get the value of companycolumn and not the column name along.
I have written the following code :
$connect = mysqli_connect("localhost","root", "","nets") or die("Couldn't connect to database");
$query4 = mysqli_query($connect,"SELECT company from new_table where Name='Abc';")
while($row123 = mysqli_fetch_assoc($query4)){
foreach($row123 as $key => $value){
echo $value;
echo $row123[$key];
}
}
The echo gives me the column name and not the value of the column. How to print only the column value in Php.
Output :
company
Required output:
lenovo
Upvotes: 0
Views: 508
Reputation: 5617
As discussed in the chat associated to this question. The SQL was actually part of a larger block of PHP code which was dynamically generating SQL that looked like this:
$query = "select '$varname' from thetable";
This resulted in selecting the column name as a string rather than a column. For example, if $varname = "country";
the output was country
. Using the backquotes fixed it:
$query = "select `$varname` from thetable"
This ensured that the SQL column was selected.
Upvotes: 1
Reputation: 29614
No need for nested loops. The inner foreach
you are trying to traverse a single row not an array.
Try
while($row123 = mysqli_fetch_assoc($query4)){
echo $row123['country'];
}
Upvotes: 0