Jump to content

Grab informatin from database.


sean14592

Recommended Posts

Hi, I have a login system and I once the user is logged in they are taken to there user Cpanel.

 

What I want to do is to be able to say "Welcome [Full Name], Enjoy your stay!", When the users register they have to save there full name in the database, so I just need to know how to extract this and print it to screen.

 

Cheers

Sean Preston

Link to comment
https://forums.phpfreaks.com/topic/98403-grab-informatin-from-database/
Share on other sites

that is a pretty basic query of the database but it sounds like you might need to brush up on some of the basics of PHP and MySQL. php.net has all the information you will need about PHP. You will need to use PHP to query your database, with the mysql_query() function. Inside of the parenthesis will go the instructions for MySQL that will tell it to find you the username of this particular user. The MySQL query will be formed something like: SELECT username FROM members WHERE userid = $_SESSION['userid']

    Basically you will need to learn a little bit to do it. But, it is one of the easiest possible scripts to write.

$connect = mysql_connect($host,$username,$password) or die("Error connecting to Database!". mysql_error());
mysql_select_db($database,$connect) or die("cannot select database!".mysql_error());

//Check if username exists

$fetchname = mysql_query("SELECT name FROM users WHERE username=$_SESSION['username']") or die(mysql_error());
     $a_record = mysql_fetch_array($fetchname);
     $namereturn = $a_record['name'];

echo("Welcome".$namereturn. ",hi");

 

the database info is included in a config.php which is included in this file at the very top!

 

Cheers

Sean

correction:

 

$fetchname = mysql_query("SELECT name FROM users WHERE username='{$_SESSION['username']}'") or die(mysql_error());

 

explanation: $_SESSION is an array, so like any array you need to add curly brackets around it when used as you have in a double-quoted string. you should also always single-quote values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.