Jump to content

Archived

This topic is now archived and is closed to further replies.

jimmyp3016

Quick Question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[quote]I have a page that loads a persons data from a sql database and puts it into the page[/quote]
Which persons data? How does it know who to load?

Share this post


Link to post
Share on other sites
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')

Share this post


Link to post
Share on other sites
here is my code:

[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");
}

[/code]

Share this post


Link to post
Share on other sites
[code=php:0]
$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.
}
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.