it seems i can't output my username value from my database. All it gives was also a value of "username". So if i'm echoing my variable $users which is holding the rows of my username from my database, it strangely outputs "Welcome, username". I tried also with my password but also giving the same text. I can't figure out what's my problem here... Hmmm

<?php

$error = 'Could not connect';
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';
$mysql_db ='test';
@mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($error);
@mysql_select_db($mysql_db) or die($error);


$query = "SELECT 'username', 'password', 'firstname' FROM users";
if ($query_run = mysql_query($query)){

while($row = mysql_fetch_assoc($query_run)){
$users = $row['username'];

echo 'Welcome, '.$users;

}
}
else
{
echo mysql_error();
}



?>

Try the following -

$query = "SELECT 'username', 'password', 'firstname' FROM users";
$query_run = mysql_query($query)
while($row = mysql_fetch_assoc($query_run)){
$users = $row['username'];
echo 'Welcome, '.$users;
}
else
{
echo mysql_error();
}

Thank you but i got a syntax error, unexpected T_WHILE string. Hmmm..

The single quotes are messing it up. Remove them or use backticks.

The example from AndreRet misses a semi-colon on line 2.

that it does, my bad sorry. ;)

I tried putting a semi-colon on line 2, changing from single quotes to backticks but no luck. Unexpected T_ELSE error.

Your if is missing.

Thank you guys! I finally solved my problem. The trick was to use backticks around my fieldnames and the tablename instead of single quotes. Honestly, i didn't know about backticks until pritaeas mentioned it. I searched it on google and there i was educated. Thank you again :)

Glad you got a solution. Please mark as solved, thanx.

@Pritaeas, any idea on my post about 45 minutes ago? Would love your input, please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.