SQL Newbie Question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SL-Cowsrule
Forum Newbie
Posts: 13
Joined: Sat Oct 12, 2002 5:08 pm

SQL Newbie Question

Post by SL-Cowsrule »

I am using the exact syntax from a book, and the code is not working. all the variables have the right caps, and they are all valid field names. I dont know what the problems are, the errors are:
Warning: Supplied argument is not a valid MySQL result resource in /hsphere/local/home/twatters/gwatters.com/ta/Login Pages/Login.php on line 32

Warning: Cannot add header information - headers already sent by (output started at /hsphere/local/home/twatters/gwatters.com/ta/Login Pages/Login.php:9 in /hsphere/local/home/twatters/gwatters.com/ta/Login Pages/Login.php on line 36

Warning: Cannot add header information - headers already sent by (output started at /hsphere/local/home/twatters/gwatters.com/ta/Login Pages/Login.php:9) in /hsphere/local/home/twatters/gwatters.com/ta/Login Pages/Login.php on line 37

::

the code is:

Code: Select all

<?php
	$Host = "******";
	$User = "******";
	$Pass = "******";
	$DBName = "******";
	$TableName = "******";

	$Query = "SELECT * FROM $TableName WHERE Username = $Username";
	$Link = mysql_connect("$Host", "$User", "$Pass");
	mysql_select_db("$DBName");

	if (!$_POSTї'Username']) {
		echo ("Please fill out your username.<BR>");
	}
	if (!$_POSTї'Password']) {
		echo ("Please fill out your password.<BR>");
	}
	If (mysql_connect("$Host", "$User", "$Pass")) {
		print ("Yahoo!!!<BR>\n");
	}
	
if ($Username && $Password) { 
    $res = mysql_query("SELECT Username,Password FROM $TableName WHERE Username='$Username' AND Password='$Password'"); 
	$num = mysql_num_rows($res);
	if ($num != '0') { 
        $verified_user = $Username; 
        $verified_userpw = $Password;
		setcookie("Username", "$verified_user", time()+ "10000000");
		setcookie("Password", "$verified_userpw", time()+ "10000000");
		print ("You have sucessfully logged in.\n");
	} else {
		print ("Bad Username or Password.\n");
		mysql_error();
	}
}

mysql_close($Link);
?>
Thanks for any help you can give me.

This is going to hopefully be a user login system, but if any of you have ideas that would work better for this, that would be great.

Thanks,
CoW
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$Query = "SELECT * FROM $TableName WHERE Username = $Username"; 
$Link = mysql_connect($Host, $User, $Pass) or die(mysql_error()); 
mysql_select_db($DBName) or die(mysql_error());

you are connecting the database twice

Code: Select all

$Link = mysql_connect("$Host", "$User", "$Pass");
...
If (mysql_connect("$Host", "$User", "$Pass")) {
why?

and please read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Put "ob_start()" and "ob_flush()" at the very start and the end of the script!
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

you cannot have ANY output before a header("Location:") tag...
that means all echos and all html tags (including <html>)
so you will need to remove the echos at the top (maybe make them if not then redirect to login statements) cos even if you fix the sql it wont work
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:oops: narrow minded me.
It was in 'Databases' so I gave the standard database-answer :lol:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

An error message would be counted as output though wouldn't it? So it could still be a problem with the query.

SL-Cowsrule -> have you read this: viewtopic.php?t=511

Mac
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

yah twig but the point is that even if the query worked theres output before the header :P
Post Reply