user2245512 user2245512 - 1 month ago 7
SQL Question

PHP Select fields from database where username equals X

im having problems in PHP with selecting Infomation from a database where username is equal to $myusername

I can get it to echo the username using sessions from the login page to the logged in page.

But I want to be able to select things like 'bio' and 'email' from that database and put them into variables called $bio and $email so i can echo them.

This is what the database looks like:

This is what the database looks like:

Any ideas?:/

Answer

You should connect to your database and then fetch the row like this:

// DATABASE INFORMATION
$server = 'localhost';
$database = 'DATABASE';
$dbuser = 'DATABASE_USERNAME';
$dbpassword = 'DATABASE_PASSWORD';

//CONNECT TO DATABASE
$connect = mysql_connect("$server", "$dbuser", "$dbpassword")
    OR die(mysql_error());
   mysql_select_db("$database", $connect);
//ALWAYS ESCAPE STRINGS IF YOU HAVE RECEIVED THEM FROM USERS
$safe_username = mysql_real_escape_string($X);
//FIND AND GET THE ROW
$getit = mysql_query("SELECT * FROM table_name WHERE username='$safe_username'", $connect);
$row = mysql_fetch_array($getit);

//YOUR NEEDED VALUES
$bio = $row['bio'];
$bio = $row['email'];

Note 1:

Dont Use Plain Text for Passwords, Always hash the passwords with a salt

Note 2:

I used MYSQL_QUERY for your code because i don't know PDO or Mysqli, Escaping in MYSQL is good enought but Consider Using PDO or Mysqli , as i don't know them i can't write the code with them for you