tecmeister Posted May 27, 2009 Share Posted May 27, 2009 Hi everyone, I'm getting this error and I don't know why. Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\users\include\database.php on line 16 I am proberly missing something. Here is the code that I'm referring it from $first = $_POST['first']; $last = $_POST['last']; $email = $_POST['email']; $user = $_POST['user']; $pass = $_POST['pass']; $sql = "INSERT INTO users (firstname, lastname, email, username, password)"; $sql .= " VALUES ('$first', '$last', '$email', '$user', '$pass')"; $result = $database->insert_user($sql); This is what I'm sending it to: function query($sql){ $result = mysql_query($sql,$this->connection); $this->confirm_query($result); return $result; } private function confirm_query($result){ if(!$result){ die("Database query failed: " . mysql_error()); } } Thanks for your help, tecmeister. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/ Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 This is what I'm sending it to: No it's not, you're sending it to 'insert_user'. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842818 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 Sorry, that was beacuse I pressed ctrl + z before I copied it. When I send it to query I get the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842821 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 Add this: $result = mysql_query($sql,$this->connection) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842824 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 I had that originally, but I change it to the tutorial way and I still have the same warning. Is there something wrong with the $sql variable? Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842826 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 I had that originally, but I change it to the tutorial way and I still have the same warning. Is there something wrong with the $sql variable? Well what's the warning say? The SQL syntax looks valid to me... Maybe you should try to echo out '$sql' to see the string that's actually being passed to your function. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842828 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 I have also tried that and print. This is what I get. INSERT INTO users (firstname, lastname, email, username, password) VALUES ('Johnny', 'McCaffery', 'notanotherperson@hotmail.com', 'tecmeister', 'password'). Can you see and error? Thanks for your help, much appreciated temeister. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842829 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 Looks fine to me. You said earlier that you got a warning? Please share. :'( Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842833 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 Here is the warning: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\users\include\database.php on line 16 That line is this: $result = mysql_query($sql,$this->connection) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842836 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 Oh jeez... Isn't connection supposed to be a variable? Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842837 Share on other sites More sharing options...
lonewolf217 Posted May 27, 2009 Share Posted May 27, 2009 it should be an optional parameter right? What happens when you take out $this->connection entirely Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842838 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 When I take away $this->connection. It cant connect to the database. Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\users\include\database.php on line 17 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\users\include\database.php on line 17 Access denied for user 'ODBC'@'localhost' (using password: NO) Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842843 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 Please post your entire database class. In your constructor you have your connection string similar to? $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842846 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 <?php class mysqldatabase{ private $connection; function __content(){ $this->open_database; } function open_database(){ $this->connection = mysql_connect("localhost","teci","********")or die("Connect Error".mysql_error()); $this->connection = mysql_select_db("users")or die("Select Database Error".mysql_error()); } function query($sql){ $result = mysql_query($sql,$this->connection) or die(mysql_error()); return $result; } } $database = new mysqldatabase; return $database; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842847 Share on other sites More sharing options...
tecmeister Posted May 27, 2009 Author Share Posted May 27, 2009 I think that I have found the error $this->open_database(); Sorry still got that warning. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842849 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 Where is the private function confirm_query()? You need to post consistent code. Try using this class: class mysqldatabase{ private $connection; /* Class constructor */ function __construct(){ $this->connection = mysql_connect("localhost","teci","********")or die("Connect Error".mysql_error()); $this->connection = mysql_select_db("users")or die("Select Database Error".mysql_error()); } function query($sql){ $result = mysql_query($sql,$this->connection) or die(mysql_error()); return $result; } } $database = new mysqldatabase; return $database; ?> Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842858 Share on other sites More sharing options...
Ken2k7 Posted May 27, 2009 Share Posted May 27, 2009 Maq - typo in your last line in the constructor. Although, I'm not sure where you're returning $database to either. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842881 Share on other sites More sharing options...
anupamsaha Posted May 27, 2009 Share Posted May 27, 2009 Try this: $sql = "INSERT INTO `users` (`firstname`, `lastname`, `email`, `username`, `password`)"; I think there is a conflict between the field password and MySQL function PASSWORD. So, its better to enclose all the MySQL table name and field names with backquote (`) character in order to avoid the conflict. Hope this will help. Quote Link to comment https://forums.phpfreaks.com/topic/159796-warning-mysql_query-supplied-argument-is-not-a-valid-mysql-link/#findComment-842957 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.