How can i change my code from var_dump
to echo
only?
Code:
$link = mysqli_connect("$myHost", "$myUser", "$myPass", "$myDB");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$username = mysqli_real_escape_string($link, $_SESSION['username']);
var_dump($username);
$sql = "SELECT * FROM exampletable where username = $username";
if ($result = $link->query($sql)) {
while ($row = $result->fetch_row()) {
var_dump($row);
}
}
Because if i var_dump($row);
i get always this:
'test@gmail.com' (length=16)
But i want echo only the username:) i am new with php sorry.
Thanks
Your query is failing because you forgot to put quotes around $username
in the query. It should be:
$sql = "SELECT * FROM exampletable where username = '$username'";
Then in your loop you can do:
while ($row = $result->fetch_assoc($result)) {
echo $row['somecolumnname'];
}
to see the contents of that column in every row that matches the username.
sarahn22! I see you're a new contributor, so welcome to StackOverflow. :)
According to PHP MySQLi fetch_row()
function documentation, whenever you use it, you receive an array with each column as an index or null if no rows were found.
In that case, considering your username column would be the first column, you could get it by typing:
echo $row[0];
So, for better understanding, imagine the database table as:
username | first_name | last_name
---------------------------------
user01 | John | Snow
This would be the results:
echo $row[0]; // Shows "user01";
echo $row[1]; // Shows "John";
echo $row[2]; // Shows "Snow";
Good luck on your new path in PHP learning. :)