eridanus Posted December 22, 2008 Share Posted December 22, 2008 Hi all, I'm creating some login code (just for some fun during my holidays, i recently finished school (onto uni, XD)) and i want to define the result of a query (only one result) as a variable (so it can be used throughout a site, as a function maybe?). Is there anyway i can do that? Here's my current code for this: <?php $username=$_POST['username']; $password=$_POST['password']; $errors=0; if (!trim($username))//checks if the username variable is blank and prints an error message { echo "<BR><B>Username</B> Required<BR>"; $errors++; } if (!trim($password))//checks if the password variable is blank and prints an error message { echo "<BR><B>Password</B> Required<BR>"; $errors++; } if ($errors>0) { echo"<BR>Please insert or modify the required field by <a href='login.php'>resubmitting the login</a>"; } else { echo "$username";// just echoed these variables to make sure it ran right echo "<BR>$password<BR>"; include 'dblogin.php'; $result = mysql_query("SELECT login_ID, username, password FROM snet.members WHERE password='$password' AND username='$username'", $db); if ($myrow = mysql_fetch_array($result)) { echo "logged in as<BR> "; printf ("%s", $myrow["username"]); echo "<BR>ID<BR>"; printf ("%4d", $myrow["login_ID"]); while ($myrow = mysql_fetch_array($result)); } else { echo "Invalid Username or password"; } mysql_close($db); } ?> i simply want to define the result as a variable instead of echoing it as whats shown in the code. I tried putting stuff like: $user="printf ("%s", $myrow["username"])"; but that just comes up with syntaxes or just echoes the text as is any and all help appreciated, eri PS. yeah, i recently started mysql and php, so i don't know all the ropes just yet XD Link to comment https://forums.phpfreaks.com/topic/137971-mysql-help/ Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 $user="printf ("%s", $myrow["username"])"; This is all wrong. Syntax isn't correct, sould be: $user = sprintf ("%s", $myrow["username"]); But there's no point at all in using sprintf here, just use: $user = $myrow["username"]; A Link to comment https://forums.phpfreaks.com/topic/137971-mysql-help/#findComment-721282 Share on other sites More sharing options...
eridanus Posted December 23, 2008 Author Share Posted December 23, 2008 sweet, thanks mate, i thought you had to declare the data type when requesting it, guess i was wrong XD eri Link to comment https://forums.phpfreaks.com/topic/137971-mysql-help/#findComment-721885 Share on other sites More sharing options...
ngreenwood6 Posted December 23, 2008 Share Posted December 23, 2008 You also may want to look into sessions. When a user logs in you can set their username in the session and call it at anytime without doing a query or passing it from another page Link to comment https://forums.phpfreaks.com/topic/137971-mysql-help/#findComment-721890 Share on other sites More sharing options...
eridanus Posted December 23, 2008 Author Share Posted December 23, 2008 You also may want to look into sessions. When a user logs in you can set their username in the session and call it at anytime without doing a query or passing it from another page okies, thanks, i got a bit of session code off the net, so I'll be incorporating that further on eri Link to comment https://forums.phpfreaks.com/topic/137971-mysql-help/#findComment-721911 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.