berry05 Posted December 12, 2008 Share Posted December 12, 2008 i have a index page and on that index page it welcomes a user but its always user tyler...here's the php code block that shows the user name...i don't know why it does that either.. <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("textgame") or die(mysql_error()); $query = "SELECT * FROM users"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result) or die(mysql_error()); echo $row['username']; ?> Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/ Share on other sites More sharing options...
Caesar Posted December 12, 2008 Share Posted December 12, 2008 You're not specifying any criteria in your MYSQL query. You're selecting ALL users...since you're not looping through results, it's likely showing the username alphabetically, in descending order. Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714036 Share on other sites More sharing options...
ted_chou12 Posted December 12, 2008 Share Posted December 12, 2008 yeah like this while ($row = mysql_fetch_array($result)) { echo $row['username'];} Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714044 Share on other sites More sharing options...
berry05 Posted December 12, 2008 Author Share Posted December 12, 2008 yeah like this while ($row = mysql_fetch_array($result)) { echo $row['username'];} i tried that and it shows all the users in the database...lol...and how do i loop if i have to loop it? Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714051 Share on other sites More sharing options...
timecatcher Posted December 12, 2008 Share Posted December 12, 2008 I think you need cookies for this to work mate, you can't just expect the database to remeber whos connecting. Try setting a cookie for the username and checking it against the database, then echoing it out in the welcome message. Hope thanks helps. Timecatcher. Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714063 Share on other sites More sharing options...
Caesar Posted December 12, 2008 Share Posted December 12, 2008 Where's your login script? In it, do you set a cookie or session variable that contains the user's id or username? Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714066 Share on other sites More sharing options...
berry05 Posted December 12, 2008 Author Share Posted December 12, 2008 Where's your login script? In it, do you set a cookie or session variable that contains the user's id or username? i use sessions...here's the login script... <?php ob_start(); $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="textgame"; // Database name $tbl_name="users"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE; $password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE; $password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE; if(!$username) die('username not set'); if(!$password1) die('Password1 not set'); else $password1 = md5($password1); if(!$password2) die('Password2 not set'); else $password2 = md5($password2); $query = "SELECT * FROM users WHERE username='$username' AND password1='$password1' AND password2='$password2'"; $result = mysql_query($query) or die( mysql_error()); $count=mysql_num_rows($result); if($count==1){ session_register("username"); $_SESSION['username'] = $username; header("location:index2.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714070 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2008 Share Posted December 12, 2008 so if it is in the session all you have to do is echo out he username echo "Welcome " . $_SESSION['username']; but on your index page you will have to start the session. in order to do that you will need to put this right below the <?php: session_start(); Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714106 Share on other sites More sharing options...
berry05 Posted December 12, 2008 Author Share Posted December 12, 2008 thxs man! a small code like that got it working !! Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714110 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2008 Share Posted December 12, 2008 no problem. I would read up on sessions a little bit more if I was you. check out tizag.com they have some good tutorials. Link to comment https://forums.phpfreaks.com/topic/136729-solved-on-main-page-is-welcomes-users-but-its-the-wrong-user/#findComment-714113 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.