Not a valid MySQL resource...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Not a valid MySQL resource...

Post by Jim »

Warning: Supplied argument is not a valid MySQL result resource in /home/maxxxtor/public_html/clanmanager/login.php on line 23

Here's the code:

Code: Select all

<FORM METHOD=POST ACTION=login.php>
<TABLE>
<TR>
<TD>Username</TD><TD><input type=text name=name></TD>
<TR>
<TD>Pass</TD><TD><input type=text name=pass></TD>
<TR>
<TD><input type=submit value=submit></TD>
</TR>
</TABLE>
</FORM>



<?
if($name)&#123;

ob_start();
include("config.php");

$sql = "SELECT from cm_admin id WHERE name='.$name.' and pwd='.$pass.'";
$query = mysql_query($sql);
$num = mysql_numrows($query);

if($num == 1) &#123;

echo 'You are logged in!!!';

setcookie("prodigy_name" , '.$name.');
setcookie("prodigy_pass" , '.$pass.');

&#125; else &#123;

print("Your Username and/or Password are incorrect.  If you feel you have reached this message in error, please contact the <a href='mailto:jim@strategyplanet.com'>webmaster</a>.");

&#125;

&#125;

?>
Thanks for the help!
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

I'm not sure, Jim but shouldn't:

Code: Select all

$sql = "SELECT from cm_admin id WHERE name='.$name.' and pwd='.$pass.'";
actually be

Code: Select all

$mysql = "SELECT from cm_admin id WHERE name='.$name.' and pwd='.$pass.'";
The $mysql = "SELECT...
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

opps, forgot

Post by gotDNS »

oops, forgot something:

Shouldn't you have:

Code: Select all

SELECT * from ......
rather than:

Code: Select all

SELECT from ...
I usually have the * there, but it could just specify one portion of the DB.
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

yeah, its got to have a * in there or a list of fields that you wish to select
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Not a valid MySQL resource...

Post by twigletmac »

Jim wrote:Warning: Supplied argument is not a valid MySQL result resource in /home/maxxxtor/public_html/clanmanager/login.php on line 23
This means that your query has a problem with it, or you were unable to connect to the mysql server or database.

Try putting:

Code: Select all

echo mysql_error();
after the query line and it should give you more information.
gotDNS wrote:The $mysql = "SELECT...
FWIW $sql was just a variable, changing it to $mysql would have made no difference whatsoever (just would have meant a new variable name) :).

Mac
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

It is not going to give you an error because that is a valid SQL statement. The problem you have is in the WHERE clause.

Code: Select all

WHERE name='.$name.' and pwd='.$pass.'";
You have it searching for name equaling .$name. and pass equaling .$pass. You have to add "s in there if your going to step out of the srting and add another string.

Code: Select all

WHERE name='".$name."' and pwd='".$pass."'";
Cheers Sam
prasadharischandra
Forum Commoner
Posts: 57
Joined: Thu May 09, 2002 2:19 am
Location: sri lanka
Contact:

sql

Post by prasadharischandra »

the sql start fro
SELECT *
you missed * part
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

sam wrote:It is not going to give you an error because that is a valid SQL statement.
It is going to give an error because nothing was specified between the SELECT keyword and the FROM keyword. The error that it would show is:

Code: Select all

You have an error in your SQL syntax near 'FROM users id WHERE username='.$name.' and password='.$pass.' LIMIT 0, 500' at line 1
or something similar.

The mistake with the WHERE clause would have just caused no rows to be returned because it wouldn't have matched anything in the database. Then what would have been displayed is Jim's predefined error message about incorrect username or password.

Mac

PS. If you're having problems with a MySQL query in PHP first try echoing the SQL statement to the screen so you can spot errors like the one's with .$name. and .$pass.. If that seems fine try running it through MySQL by the command line or a GUI like phpMyAdmin to see if it is a problem with the SQL statement or your PHP code.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Re: Not a valid MySQL resource...

Post by mikeq »

Jim wrote:Warning: Supplied argument is not a valid MySQL result resource in /home/maxxxtor/public_html/clanmanager/login.php on line 23

Here's the code:

Code: Select all

$sql = "SELECT * from cm_admin id WHERE name='.$name.' and pwd='.$pass.'";
The problem is the .'s in your SQL statement, you don't need to concatenate the variables in there, try this

Code: Select all

$sql = "SELECT * from cm_admin id WHERE name='$name' and pwd='$pass'";
Because you are in " then PHP will put in the values of $name and $pass without any further work.

Mike
Post Reply