Shorten this MySQL code?

From: Date: Mon, 19 Feb 2001 15:49:39 +0000
Subject: Shorten this MySQL code?
Groups: php.general 
Request: Send a blank email to php-general+get-40580@lists.php.net to get a copy of this message
Newbie with MySQL. I have a user auth form before this script which asks for username and password. First Name and Last name are also in each record in the database along with username and password. When the user successfully logs in, I have it say, "Welcome name_first, you're authorized! The only way I could figure out how to do this is make a separate sql call to retrieve the first name. Can I consolidate this into one call? Thanks. (Based on "PHP Fast and Easy" code.) <? if ((!$username) || (!$password)) { header("Location: blah"); exit; } $db_name = "notgonnadoit"; $table_name = "guess"; $connection = @mysql_connect("blah", "bleck", "bleep") or die("Couldn't connect."); $db = mysql_select_db($db_name, $connection) or die("Couldn't select database."); $sql = "SELECT * FROM $table_name WHERE username = \"$username\" AND password = password(\"$password\") "; $result = mysql_query($sql) or die ("Can't execute query."); $num = mysql_numrows($result); if ($num != 0) { $connection = @mysql_connect("blah", "bleck", "bleep") or die("Couldn't connect."); $db = mysql_select_db($db_name, $connection) or die("Couldn't select database."); $sql1 = "SELECT username, name_first FROM $table_name "; $result1 = @mysql_query($sql1,$connection) or die("Couldn't execute query."); while ($row = mysql_fetch_array($result1)) { $name_first = $row['name_first']; $username1 = $row['username']; if ($username1 == $username) { $name_first1 = $name_first; } } $msg = "<p>Welcome $name_first1, you're authorized!</p>"; } else { header("Location: http://www.webdesigns1.f2s.com/db/auth_login.html"); exit; } ?> <html> <head> <title>Welcome</title> </head> <body bgcolor="#000000" text="#00ff00"> <? echo "$msg"; ?> </body> </html>

« previous php.general (#40580) next »