Jump to content


Photo

Quick Question


  • Please log in to reply
6 replies to this topic

#1 jimmyp3016

jimmyp3016
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 20 July 2006 - 03:30 PM

I have a page that loads a persons data from a sql database and puts it into the page. I need to know how to check the database, and if the user is not in it then redirect to a different page. This is prob easy to do, im just new at php and sql. any help would be great.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 July 2006 - 03:32 PM

I have a page that loads a persons data from a sql database and puts it into the page

Which persons data? How does it know who to load?

#3 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 20 July 2006 - 03:32 PM

I'd need to know your table info to write a script but here is an outline (Look in the FAQ for database help):
Connect to DB
Selct DB
Look in users for 'user'
If no results  header('location: redirected page')

#4 jimmyp3016

jimmyp3016
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 20 July 2006 - 03:33 PM

here is my code:


$user	=	$_GET['usr'];
$db = @mysql_connect("$localhost", "$databaseuser", "$databasepasswd");
@mysql_select_db("$databasename",$db);
$sql	=	"SELECT * FROM table WHERE user='$user'";
$result = mysql_query($sql,$db);
if (mysql_num_rows($result) != 0) {

	$firstname	= mysql_result($result, 0, "firstname");
	$lastname = mysql_result($result, 0, "lastname");
}



#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 20 July 2006 - 03:34 PM

$user	=	$_GET['usr'];
$db = @mysql_connect("$localhost", "$databaseuser", "$databasepasswd");
@mysql_select_db("$databasename",$db);
$sql	=	"SELECT * FROM table WHERE user='$user'";
$result = mysql_query($sql,$db);
if (mysql_num_rows($result) != 0) {

	$firstname	= mysql_result($result, 0, "firstname");
	$lastname = mysql_result($result, 0, "lastname");
} else {
  header("Location: usernotfound.php"); // <--- put the page you wish to direct to here.
}


#6 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 20 July 2006 - 03:35 PM

It's not as simple as just throwing an answer at you.

1. You need to know what table to lookup.
2. How are users handles in your curent script? What are you using as criteria? Userid? Username?
3. You then need a conditional statement that executes your redirection, based on the results.
4. What method is your current code using? Is it using OOP? The database query might look diferent if that's the case.

You have any example code?

http://www.php.net <--Very helpful site. Use it.
PHP Ninja

#7 jimmyp3016

jimmyp3016
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 20 July 2006 - 03:39 PM

Thorpe,

You are truly a Guru! Thank you! It worked.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users