elliotpo Posted September 23, 2009 Share Posted September 23, 2009 Hello, I'm fairly new to this, having trouble with a basic query. I can run the query in Terminal, but when I write it into PHP I get the following error message: Warning: mysql_query() expects parameter 2 to be resource, null given in (etc.) and here is the problematic line as per the error reporting: mysql_query ( 'INSERT INTO users (id, username, password, first_name, last_name) VALUES (1, \'john\', \'password\', \'john\', \'thompson\')', NULL ) it's not clear to me where that NULL is coming from. here's what i'm writing in the page i'm trying to load: $sql = sprintf("INSERT INTO users (id, username, password, first_name, last_name) VALUES (1, 'john', 'password', 'john', 'thompson')"); $result = $db->query($sql); and here's how the method is written on the page that's being included: public function query($sql) { $result = mysql_query($sql, $this->connection); $this->confirm_query($result); return $result; } Again, I can just enter the row into MySql directly and no trouble, but I can't get it to work through PHP. Any advice? I feel like something simple and glaring is at fault here, but I can't see it. Thanks so much - Elliot Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/ Share on other sites More sharing options...
redarrow Posted September 23, 2009 Share Posted September 23, 2009 You sure it connecting correctly mate! Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/#findComment-923230 Share on other sites More sharing options...
elliotpo Posted September 23, 2009 Author Share Posted September 23, 2009 this is above the query. would this error message override the query method message if the connection was failing? or would the query message still the one to appear since that's what i called directly? sorry is these are silly questions - public function open_connection() { $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if (!$this->connection) { die("databse connection failed, bzzzt " . mysql_error()); } else { $db_select = mysql_select_db(DB_NAME, $this->connection); if (!db_select) { die("database selection failed: " . mysql_error()); } } } Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/#findComment-923244 Share on other sites More sharing options...
redarrow Posted September 23, 2009 Share Posted September 23, 2009 That should work? Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/#findComment-923254 Share on other sites More sharing options...
mikesta707 Posted September 23, 2009 Share Posted September 23, 2009 maybe it has to do with the sprintf() call you have. try getting rid of that Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/#findComment-923257 Share on other sites More sharing options...
elliotpo Posted September 23, 2009 Author Share Posted September 23, 2009 I only added the sprintf while trying to randomly trouble-shoot. But I got the same NULL error message before that. Earlier today I changed localhost to 127.0.0.1 in one of the mysql library files... I think, or one of the PHP files, I can't remember anymore; which was to address my inability to mysql to work. it fixed it-- but do i now have to change localhost in a range of other files as well? and on the page where the DB values are being given in this file structure here? does that seem like it could be relevant? thanks again Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/#findComment-923263 Share on other sites More sharing options...
elliotpo Posted September 23, 2009 Author Share Posted September 23, 2009 Ah I fixed it. It's embarrassing. I was trying to connect with a function __construct, and simply mistyped that method. Apologies. But thanks, since the replies were still instructive and are appreciated. Elliot Quote Link to comment https://forums.phpfreaks.com/topic/175170-mystery-null-mucking-me-up-newbie-question/#findComment-923627 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.