Ads Posted November 5, 2007 Share Posted November 5, 2007 I am trying to acess the Databse to get a username and echo it out..But for some reason it gives that error (See title ) I have no idea what it means, Can any one help out i am pretty noobish at php so Forgive me Quote Link to comment Share on other sites More sharing options...
thisisnuts123 Posted November 5, 2007 Share Posted November 5, 2007 your information is going into an array and u are not outputting it properly paste your code so we can take a look Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 5, 2007 Share Posted November 5, 2007 code please Quote Link to comment Share on other sites More sharing options...
Zane Posted November 5, 2007 Share Posted November 5, 2007 you are echo-ing your query variable. you need to use it i.e. $sql = "SELECT * FROM sometable"; $query = mysql_query($sql); //echo $query while($row = mysql_num_rows($query)) { echo $row[1]; // That's you get data out } Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 <?php include "include/db.php"; ?> <html> <head> <title>Apples xD </title> <link href="style1.css" rel="stylesheet" type="text/css"> </head> <body> <?php $getuser="SELECT * FROM players WHERE id='6'"; $getuser2=mysql_query($getuser) or die("Could not get user info"); $getuser3=mysql_fetch_array($getuser2); echo $getuser2; Quote Link to comment Share on other sites More sharing options...
Zane Posted November 5, 2007 Share Posted November 5, 2007 try echo $getuser3[0]; and see what happens or you can replace 0 with the name of the username column in players Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 try echo $getuser3[0]; and see what happens or you can replace 0 with the name of the username column in players Cool thanx that works, Got another Question. I don;t accutly wont to Display the id Number, I want to Display the User name after the Person is logged in. EXAMPLE: Welcome: USERNAME GOES HERE but it wont let me do it it just comes up blank Heres the code: $username = $_POST['username']; $getuser="SELECT * FROM players WHERE id='6'"; $getuser2=mysql_query($getuser) or die("Could not get user info"); $getuser3=mysql_fetch_array($getuser2); echo $getuser3[1]; I don;t want the Number 6 there, it is only there becasue i have no idea how to do it Quote Link to comment Share on other sites More sharing options...
Zane Posted November 5, 2007 Share Posted November 5, 2007 echo $getuser3['username'] Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 5, 2007 Share Posted November 5, 2007 Change your call to mysql_fetch_array to mysql_fetch_assoc and save yourself the headache of dealing with numeric indexes that can change if the underlying table is altered. Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 echo $getuser3['username'] Hmm That works, but it doesn;t Display a different Username For a different..well username, if that makes sense. If i login with a different user name it doesn;t Change it at all Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 You have WHERE id='6' hardcoded in, therefore it's always going to select the record for user #6 PhREEEk Quote Link to comment Share on other sites More sharing options...
Zane Posted November 5, 2007 Share Posted November 5, 2007 here's something to work with $username = $_POST['username']; //This line is only good if you have a form beforehand $getuser="SELECT username, email, firstname FROM players'"; $getuser2=mysql_query($getuser) or die("Could not get user info"); while($row = mysql_fetch_assoc($getuser2)) { echo $row['username'] . " "; echo $row['email'] . " " echo $row['firstname'] . " " } Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 You have WHERE id='6' hardcoded in, therefore it's always going to select the record for user #6 PhREEEk I have taken out the Id 6 thing NEW CODE so far still not working $username = $_POST['username']; $getuser="SELECT * FROM players"; $getuser2=mysql_query($getuser) or die("Could not get user info"); $getuser3=mysql_fetch_array($getuser2); echo $getuser3['username']; Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 You need a criteria for which user you want to view the record for. Your newest code posted selects all player records, because there is no WHERE clause to specify a single record or group of records. Do you want ALL the records, or just one, or what? PhREEEk Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 You need a criteria for which user you want to view the record for. Your newest code posted selects all player records, because there is no WHERE clause to specify a single record or group of records. Do you want ALL the records, or just one, or what? PhREEEk I want the Username of the User. Hence they Jusy logged in Theere username displayed. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 Well, just insert the variable holding the username as the WHERE clause: $username = $_POST['username']; $getuser="SELECT * FROM players WHERE username = '$username'"; $getuser2=mysql_query($getuser) or die("Could not get user info"); $getuser3=mysql_fetch_array($getuser2); echo $getuser3['username']; But, you already have that variable in the POST... So, echo "Welcome $username!"; should work just as easily... PhREEEk Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 So, echo "Welcome $username!"; should work just as easily... PhREEEk Doesnt display $Username Just says Welcome Quote Link to comment Share on other sites More sharing options...
Zane Posted November 5, 2007 Share Posted November 5, 2007 http://www.phpfreaks.com/forums/index.php/topic,95443.0.html http://www.phpfreaks.com/forums/index.php/topic,95441.0.html Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 You have $username = $_POST['username']; in your script, so we're assuming someone has submitted a username through a form. If so, $_POST['username'] would be populated, and then converted to $username, and then Welcome $username would work... If nothing is posted, and $_POST['username'] is EMPTY, then Welcome $username! would just print Welcome ! PhREEEk Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 Seems to be one Problem after another with me Current Error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource $username = $_POST['username']; $sql = "SELECT username, FROM players WHERE username='$username'"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { // Retrieve data until no more echo $row['username'], '<br />'; } Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 5, 2007 Share Posted November 5, 2007 Remove the comma after SELECT username $sql = "SELECT username, FROM players WHERE username='$username'"; becomes $sql = "SELECT username FROM players WHERE username='$username'"; Use commas to separate more than one field request, last field (the one before FROM) should not have a comma (otherwise MySQL assumes you want to retrieve the field 'from'). PhREEEk Quote Link to comment Share on other sites More sharing options...
Zane Posted November 5, 2007 Share Posted November 5, 2007 error right here username, FROM players Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 Cool got rid of the error..Now it is a blamk screen. I dunno if this is Relevent or not, Proberly is, and if it is i am sorry I have a login Page(login.php) Once the user has put in there Email and password, it links to another page (loginck.php) This chekcs if the email and password are correct, It get the email and password from the Login.php which posts them, if the email and password is correct loginck.php redirects the page to main.php. On Main.php is where i want it to display the username the player Registerd With. But When the player Logs in the Username isn;t put into the field, they login with your email adress Quote Link to comment Share on other sites More sharing options...
redarrow Posted November 5, 2007 Share Posted November 5, 2007 <?php $username = $_POST['username']; $sql = "SELECT username, FROM players WHERE username='$username'"; $result = mysql_query($sql)or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { // Retrieve data until no more echo $row['username'], '<br />'; } ?> this code makes no sence at all what you doing...... example form with username just echo $username; dont even need the database lol......... Quote Link to comment Share on other sites More sharing options...
Ads Posted November 5, 2007 Author Share Posted November 5, 2007 <?php $username = $_POST['username']; $sql = "SELECT username, FROM players WHERE username='$username'"; $result = mysql_query($sql)or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { // Retrieve data until no more echo $row['username'], '<br />'; } ?> this code makes no sence at all what you doing...... example form with username just echo $username; dont even need the database lol......... I do need the Databse, Becasue i havent posted the username, as i just figured out, so i need to get the username from the Data base Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.