travelkind Posted October 27, 2009 Share Posted October 27, 2009 Hello everyone, Forgive my ignorance I am totally new to PHP and have come across the follow error while trying to run a user login script: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/do_authusers.php on line 21 Here is my code: <? if ((!$_POST[username]) || (!$_POST[password])) { header("Location: show_login.html"); exit; } //set up names of database and table to use $db_name = "blogtasticc"; $table_name = "logins"; //connect to server and select database $connection = mysql_connect("blogtasticc.xxxxxxxx", "xxxxxxx", "xxxxxx") or die(mysql_error()); $db = mysql_select_db($db_name, $connection) or die(mysql_error()); //build and issue the query $sql = "SELECT * FROM $table_name WHERE username = '$_POST(username)' AND password = password('_POST[password]')"; $result = mysql_query($sql,connection) or die(mysql_error()); //get the number of rows in the result set $num = mysql_num_rows($result); //print a message or redirect elsewhere, based on result if ($num !=0) { $msg = "<P>Congratulations, you're authorized!</p>"; } else { header("Location: show_login.html"); exit; } ?> <HTML> <HEAD> <TITLE>Secret Area</TITLE> </HEAD> <BODY> <? echo "$msg"; ?> </BODY> </HTML> I ran a syntax check on it and it came back fine. Any help would be much appreciated. Thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/179131-supplied-argument-is-not-a-valid-mysql-link-resource/ Share on other sites More sharing options...
xtopolis Posted October 27, 2009 Share Posted October 27, 2009 $sql = "SELECT * FROM $table_name WHERE username = '$_POST(username)' AND password = password('_POST[password]')"; I'm pretty sure that query fails. to fix it, it would be $_POST[username] with brackets. You should sanitize your inputs though. You're also missing a $ before the _POST[password] Should be closer to this iirc: $sql = "SELECT * FROM $table_name WHERE username = '{$_POST['username']}' AND password = password({$_POST['password']})"; Quote Link to comment https://forums.phpfreaks.com/topic/179131-supplied-argument-is-not-a-valid-mysql-link-resource/#findComment-945110 Share on other sites More sharing options...
travelkind Posted October 27, 2009 Author Share Posted October 27, 2009 I made the changes you recomended but I am still getting the same message. Do you have any other ideas? Thanks, Dave Quote Link to comment https://forums.phpfreaks.com/topic/179131-supplied-argument-is-not-a-valid-mysql-link-resource/#findComment-945355 Share on other sites More sharing options...
Mark Baker Posted October 27, 2009 Share Posted October 27, 2009 Start by echoing out $sql just before you execute the query. See what it actually looks like. That might help identify what the problem is and how to fix it Quote Link to comment https://forums.phpfreaks.com/topic/179131-supplied-argument-is-not-a-valid-mysql-link-resource/#findComment-945358 Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2009 Share Posted October 27, 2009 Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/do_authusers.php on line 21 At least look at the line where the error is being reported at - $result = mysql_query($sql,connection) or die(mysql_error()); The second parameter of the mysql_query() is the link resource. Your's is missing the $ in front of connection, making it a defined constant instead of a variable holding the link resource from your mysql_connect() statement. Quote Link to comment https://forums.phpfreaks.com/topic/179131-supplied-argument-is-not-a-valid-mysql-link-resource/#findComment-945364 Share on other sites More sharing options...
travelkind Posted October 27, 2009 Author Share Posted October 27, 2009 Okay everyone thanks for all the help! I am not getting any more error codes but now I am getting the following Message: Unknown column 'bacon' in 'where clause' 'bacon' is the password for this particular user. So instead of posting: $msg = "<P>Congratulations, you're authorized!</p>"; it is posting the users password? Quote Link to comment https://forums.phpfreaks.com/topic/179131-supplied-argument-is-not-a-valid-mysql-link-resource/#findComment-945402 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.