Jump to content

Mysql help


eridanus

Recommended Posts

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.