PDA

View Full Version : PHP: Getting info from a database


mikeyp
02-06-2003, 09:01 PM
Ok heres my problem. Im trying to get some info from a database, and check it to see if it is correct. I get a warning that I have no idea what it means.
"Warning: Supplied argument is not a valid MySQL result resource in c:\apache\htdocs\medieval\login.php on line 16"

The Code:
<html>
<head>
<title>Medieval- Lose Yourself</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$db = mysql_connect("localhost", "root");
$poop="mike";
mysql_select_db("medieval",$db) or die("Could not Connect: " . mysql_error());


if ($submit) {
$sql = "SELECT * FROM $poop WHERE login=$login";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$login = $myrow["login"];
$pw2 = $myrow["pw"];
if ($pw2 == $pw) {
echo "Login Succesful!";
}
else{
echo "Wrong password or username";
}
}
?>

<form method="post" action="<?php echo $PHP_SELF?>">

Login<input type="Text" name="login" value="<?php echo $login ?>"><br>
Password:<input type="password" name="pw" value="<?php echo $pw ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>

<br>
</body>
</html>

jollyfactory
02-06-2003, 11:21 PM
It might be that you haven't specified a password for mysql in

$db = mysql_connect("localhost", "root");

but assuming you have a blank password for login name "root" then there should be no problem.

I am sure the problem is with your $login and $pw variables. Assuming register_globals is on, there should be no problem. You should be setting $login = $_POST['login']; (that also goes for you $pw post item).

I've created a php file with your code, created the necessary tables in mysql to test your code out and it works fine once I changed the $login variables.

scoutt
02-07-2003, 01:49 AM
$sql = "SELECT * FROM $poop WHERE login=$login";

put ' around $login

$sql = "SELECT * FROM $poop WHERE login='$login'";

mikeyp
02-07-2003, 08:58 AM
ok thanks im going to try those out now.

mikeyp
02-07-2003, 09:08 AM
ok now it works perfectly, because of you two. Now, since people are logining in? do they need to logout?

Also how would I have it stop loading the login page again, and start loading a different page? include"page.html" ? would that work?

scoutt
02-07-2003, 09:12 AM
no, that is where you make the choice. instead of logging out they can just close the browser, how do you know they did, good question and a whole other topic. :)

mikeyp
02-07-2003, 09:27 AM
And what about sessions? how do I make it so that they have to log in. So that they cant just go to a certain page and view it?

scoutt
02-07-2003, 09:31 AM
yup sessions will work in a way. if they keep there browser open and come back to your page they can view anything thye have rights to, but if you don't look for sessions or cookies then thye have to login again

mikeyp
02-07-2003, 09:38 AM
scoutt, on the login file, I have it include the file that I want them to go to after they login, but the login file still comes op. Is there any way to stop the login file from loading, after they have successfully logged in?

karinne
02-07-2003, 10:03 AM
Here's what you can do:


if ($user = $userid && $pass = $password)
{
$url = "Location: /directory/file.php";
header($url);
} else {
$url = "Location: back-to-login.php";
header($url);
}

HTH :cool:

mikeyp
02-07-2003, 10:08 AM
i get this error:
Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\medieval\login.php:7) in c:\apache\htdocs\medieval\login.php on line 20

karinne
02-07-2003, 10:13 AM
can we see the source of your login file?

scoutt
02-07-2003, 10:13 AM
to add to what karinne said yu can do this.

in your script you should have it connecting to the db and then it looks for what the user typed in (username and password) once the db returns this you assign them to a session

$_SESSION["user"] = $user_form_database
$_SESSION["password"] = $password_from_database

then do this

if ($_SESSION["user"] && $_SESSION["password"]){
header("Location: /directory/file.php");
} else {
header("Location: back-to-login.php");
}

that way they have to have a session setup and initialized

scoutt
02-07-2003, 10:14 AM
Originally posted by mikeyp
i get this error:
Warning: Cannot add header information - headers already sent by (output started at c:\apache\htdocs\medieval\login.php:7) in c:\apache\htdocs\medieval\login.php on line 20
the part that karinne and I showed you has to be at the very top of the page.

karinne
02-07-2003, 10:19 AM
Originally posted by scoutt
the part that karinne and I showed you has to be at the very top of the page.

That or you cannot have your login page that has the <html><head></head><body></body></html> stuff in it!

mikeyp
02-07-2003, 10:34 AM
alright, now im getting no errors but, nothing is happening, it just keeps on loading the login page:

<?php
if ($_SESSION["user"] && $_SESSION["password"]){
header("Location: main.php");
}
else{
?>
<html>
<head>
<title>Medieval- Lose Yourself</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$db = mysql_connect("localhost", "root");
$poop="mike";
mysql_select_db("medieval",$db) or die("Could not Connect: " . mysql_error());


if ($submit) {
$sql = "SELECT * FROM $poop WHERE login='$login'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$login = $myrow["login"];
$pw2 = $myrow["pw"];
if ($pw2 === $pw) {
$_SESSION["user"]=$login;
$_SESSION["password"]=$pw;
}
else{
echo "Wrong password or username";
}
}
?>

<form method="post" action="<?php echo $PHP_SELF?>">

Login<input type="Text" name="login" value="<?php echo $login=$_POST['login'];?>"><br>
Password:<input type="password" name="pw" value="<?php echo $pw=$_POST['pw']; ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>

<br>
</body>
</html>
<?php } ?>

scoutt
02-07-2003, 10:40 AM
you need to add

session_start();

right after <?php
at the very top of EVERY page you want to use sessions in.


<?php
session_start();

if ($_SESSION["user"] && $_SESSION["password"]){
header("Location: main.php");
}
else{
?>
<html>

karinne
02-07-2003, 10:45 AM
Your best bet would be to put your form in another file (well... that's how I did it!)

So.... here's your form file

index.php

<html>

<head>
<title>Medieval- Lose Yourself</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form method="post" action="login">
Login: <input type="Text" name="login" value=""><br>

Password: <input type="password" name="pw" value=""><br>

<input type="Submit" name="submit" value="Enter information">
</form>

</body>
</html>


and here's your login.php

$sql = "SELECT * FROM $poop WHERE login='$login'";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

$login = $myrow["login"];

$pw2 = $myrow["pw"];

if ($_SESSION["user"] && $_SESSION["password"]){
session_start();
header("Location: main.php");

}

else {

echo "Wrong password or username";
header("Location: index.php");

}


Something like that!

mikeyp
02-07-2003, 10:51 AM
thanks Scoutt! thanks Karianne! Now it works! I just need to put the little session stuff on every page! Many thanks.

karinne
02-07-2003, 10:55 AM
Originally posted by mikeyp
thanks Scoutt! thanks Karianne! Now it works! I just need to put the little session stuff on every page! Many thanks.

Actually NO!!!

You put it in your login script and then you link like this

page.php?sessionid=".$PHPSESSID."

scoutt
02-07-2003, 10:59 AM
Originally posted by karinne
Actually NO!!!

You put it in your login script and then you link like this

page.php?sessionid=".$PHPSESSID."
karinne, you don't have to put it in the url. it is loaded automatically when you add sessin_start() on every page.

karinne
02-07-2003, 11:02 AM
but do you have to put session_start() on every page?

I also have session_register("somename")... do I need that too on every page?!

scoutt
02-07-2003, 11:07 AM
if you use sessions in your page you have to have session_start() no matter what. doesn't matter if it is in the url or not. if you don't use the sesion variable then you don't have to have it. but you will lose the session if you don't have it an dgo back to a page that does.

if (depending on your version of php) you use session_register() you don't need to have that on everypage. that is just doing the samething as $_SESSION['name'] = "whatever";

your way, karinne, is the old way and shouldn't be used.

haing it in the url is the old way as some servers didn't carry the session over or they are setup different. but every browser send the session_id to php so no worries.

karinne
02-07-2003, 11:38 AM
Thanks scoutt!

mikeyp
02-07-2003, 12:34 PM
Okay im making an MMORPG for me and like 10 of my friends. And I want it so that every hour the server updates info. Each user gets more money, peasants etc. How do I get the server to do this?

If that isnt possible, I sorta have an idea in my head. When a user logs in, it goes and checks a file and sees when the last login. If it was more than an hour ago, it updates all of the players info that many hours and then logs the player in. Is that possible?

karinne
02-07-2003, 12:37 PM
You might want to start another thread for that one!?

scoutt
02-07-2003, 01:59 PM
Originally posted by mikeyp
Okay im making an MMORPG for me and like 10 of my friends. And I want it so that every hour the server updates info. Each user gets more money, peasants etc. How do I get the server to do this?

If that isnt possible, I sorta have an idea in my head. When a user logs in, it goes and checks a file and sees when the last login. If it was more than an hour ago, it updates all of the players info that many hours and then logs the player in. Is that possible?
well thsi will be another thread but i see possiblities.

1) you can store the time the user logged in in the db and then each time the script runs it checks the db to see that time and compares it to a hour or so and if an hour has been reached then do the updates to the money and so forth.

2) you can, if you have a server that will let you) use cronjobs which will run a script every hour and that will update everything. now this way is automatic as you program the cronjob to run at a certain time and it takes over from there. not a lot of servers (especially free ones) will let you do this.

mikeyp
02-07-2003, 02:20 PM
im thinking that lycos.co.uk doesnt support cronjobs, so I am going to try and think up a script to do #1.

mikeyp
02-07-2003, 07:36 PM
ugg, im trying to do the time thing now. If there was no ampm it would be so much easier.

mikeyp
02-07-2003, 07:41 PM
Yahoo! *whacks himself on head* wait, im getting smarter, i can just do it in military time!


Ok here is a little scripty thingy that does what I want. Can someone check this to see if it will work right? I took into account leap years and such.


<?php
$db = mysql_connect("localhost", "root");
$poop="time";
mysql_select_db("medieval",$db) or die("Could not Connect: " . mysql_error());

$sql = "SELECT * FROM $poop WHERE id=1";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$lasthour = $myrow["hour"];
$lastday = $myrow["day"];

$currentday = date("z");
$currenthour = date("h");
if ($currentday == $lastday)
{
$numhours = $currenthour - $lasthour;
echo $numhours;
}
elseif ($currentday > $lastday)
{
$temp = 24*($currentday-$lastday);
$numhours=($temp-$lasthour)+$currenthour;
echo $numhours;
}
elseif ($currentday < $lastday)
{
$leapyear=date("L");
if ($leapyear == 1)
{
$temp = (366 - $lastday) + currentday;
$numhours=($temp-$lasthour)+$currenthour;
echo $numhours;
}
else
{
$temp = (365 - $lastday) + currentday;
$numhours=($temp-$lasthour)+$currenthour;
echo $numhours;
}
}
?>

scoutt
02-07-2003, 09:07 PM
I wouldn't do it with dates, you will have a better time to do it with the time() not dates. time() is the total seconds and is a lot easier to wok with.

mikeyp
02-07-2003, 09:09 PM
now that i have thought about it, it would be easier.

mikeyp
02-07-2003, 09:39 PM
alright, is this better scoutt?


<?php
$db = mysql_connect("localhost", "root");
$poop="time";
mysql_select_db("medieval",$db) or die("Could not Connect: " . mysql_error());
$sql = "SELECT * FROM $poop WHERE id=1";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$lasttime = $myrow["time"];
$time=(time()/60/60);
$diff = $time-$lasttime
if ($diff >= 1)
{
//execute stuff in here
$sql = "UPDATE $poop SET time='$time' WHERE id=$id";
}
?>

scoutt
02-07-2003, 10:13 PM
huh?

is it an hour that you want then like this, also change your field name to something different than "time". time is a reserved word and it will get confusing later on :)

<?php
$db = mysql_connect("localhost", "root");
$poop="time";
mysql_select_db("medieval",$db) or die("Could not Connect: " . mysql_error());
$sql = "SELECT * FROM $poop WHERE id=1";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$lasttime = $myrow["time"];
$diff = time()-$lasttime;
if ($diff >= 3600) // 3600 = 1hour = 60 * 60
{
//execute stuff in here
$sql = "UPDATE $poop SET time='$time' WHERE id=$id";
}
?>