0

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();
}



?>

Edited by Eggstraordinary

3
Contributors
11
Replies
12
Views
4 Years
Discussion Span
Last Post by Eggstraordinary
Featured Replies
  • 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 :) Read More

0

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();
}
0

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

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

0

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

1

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 :)

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.