Echo User Info

I have the following code…I want to be able to echo various variables from the user info in mysql, so I tested it with the username. Once I login with the test username and password, the variable is not echoed.

Can you please tell me what I am doing wrong?

[php]<?php

// Connects to your Database

mysql_connect(“mydbhost”, “mydbusername”, “mydbpassword”) or die(mysql_error());

mysql_select_db(“mydbtable”) or die(mysql_error());

//checks cookies to make sure they are logged in

if(isset($_COOKIE[‘ID_my_site’]))

{

$username = $_COOKIE['ID_my_site']; 

$pass = $_COOKIE['Key_my_site']; 

 	$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); 

while($info = mysql_fetch_array( $check )) 	 

	{ 

//if the cookie has the wrong password, they are taken to the login page

	if ($pass != $info['password']) 

		{ 			header("Location: login.php"); 

		} 

//otherwise they are shown the admin area

else 

		{ 

echo "Welcome, " . $_SESSION[‘username’] . " ";

echo “other Content

”;

echo “Logout”;

		} 

	} 

	} 

else

//if the cookie does not exist, they are taken to the login screen

{

header(“Location: login.php”);

}

?>[/php]

What’s there are examples of what to use. You need to fill in your information for each thing

mysql_connect(“mydbhost”, “mydbusername”, “mydbpassword”) or die(mysql_error());
mysql_select_db(“mydbtable”) or die(mysql_error());

Take the above code as an example, what’s between quotes need to replaced with your database information. And they aren’t valid variables, missing the $.

@richei, thank you for your reply. I am aware of how to connect to the DB and my working file has the correct connection variables. I simply put fake variables in to my forum post so that I would not compromise my database security.

I am not having an issue with connecting to the database, but rather calling the user variables that I want to echo (like name, address, etc) from the database.

ok, well…

Basically, the while loop creates an array called $info. So to get at it, you can do echo $info[‘name’], echo $info[‘address’], and so on. To format it, just use regular html/css.

@richei,
So, I added this per your suggestion.

[php]echo “$info[‘company’]”;[/php]

It results in the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in compliance.php on line 20

You shouldn’t wrap it in quotes.

@JimL…sweet. Got it to echo.

Now, if I had to remove the quotes, how do I make is say “Welcome, …” etc.

I tried these variations, but all result in an error:

[php]echo ‘Welcome’ $info[‘company’];[/php]

[php]echo Welcome $info[‘company’];[/php]

[php]echo “Welcome” $info[‘company’];[/php]

You can do it a few different ways

echo 'Welcome '.$info[‘company’];

echo “Welcome $info[company]”;

echo "Welcome ".$info[‘company’];

Sponsor our Newsletter | Privacy Policy | Terms of Service