Neji Posted February 7, 2011 Share Posted February 7, 2011 I've just started to learn PHP and I'm having a little trouble. I'm trying to stick with OOP but I'm having trouble with it. I've coded a database connection and I'm trying to take data from a form and insert it into a database. I've managed to do it without OOP but I can't get it to work with. The code is below. Any help would be great. I have a file for the form, which should take the data and the php should insert it into the table - <?php // Get the PHP file containing the dbConnect class require('../../configuration.php'); // Get the PHP file containing the dbConnect class require('../../lib/db.class.php'); // Checks whether a form has been submitted. If so, carry on if ($_POST) { // Creates an instance of dbConnect $link = new dbConnect(); // Creates a SQL query $insertQuery = 'INSERT INTO content SET title = "' . $_POST['title'] . '", alias = "' . $_POST['alias'] . '", category = "' . $_POST['category'] . '", summary = "' . $_POST['summary'] . '", content = "' . $_POST['content'] . '"'; $result = $link->query($insertQuery, $link); } ?> <body> <form action="" method="post"> <div> <label for="title">Title:</label> <textarea id="title" name="title" rows="1" cols="30"> </textarea> </div> <div> <label for="alias">Alias:</label> <textarea id="alias" name="alias" rows="1" cols="30"> </textarea> <div> <label for="category">Category:</label> <textarea id="category" name="category" rows="1" cols="30"> </textarea> </div> <div> <label for="summary">Summary:</label> <textarea id="summary" name="summary" rows="6" cols="40"> </textarea> </div> <div> <label for="content">Content:</label> <textarea id="content" name="content" rows="12" cols="40"> </textarea> </div> <div> <input type="submit" value="Add Article" /> </div> </form> This is my class to connect to the db - class dbConnect extends siteConfig { var $theQuery; var $link; // Function to connect to the database public function dbConnect() { // Load configuration from parent class $config = siteConfig::getConfig(); // Get main config settings from the array that we just loaded $host = $config['hostname']; $user = $config['username']; $pass = $config['password']; $db = $config['database']; // Connect to the DB $link = mysql_connect('localhost', 'user', 'pass'); if (!$link) { $error = 'Unable to connect to the database server.'; echo $error; exit(); } } // Function to execute a database query public function query($link, $query) { $this->theQuery = $query; mysql_query($this->link, $query); } // Function to get array of query results public function getArray($result) { return mysql_fetch_array($result); } // Function to close the connection public function closeConnection() { mysql_close($this->link); } } I also have a config file. I'm not using it atm but I thought I'd show it anyway as it may help - class siteConfig { var $config; function getConfig() { $config['site_url'] = 'localhost/edencms'; $config['hostname'] = 'localhost'; $config['username'] = 'user'; $config['password'] = 'pass'; $config['database'] = 'edencms'; } } After filling out the form and sending it, I get the following error: Warning: mysql_query() expects parameter 2 to be resource, object given in C:\xampp\htdocs\EdenCMS\lib\db.class.php on line 39 It seems like $link isn't staying as a resource once the dbConnect is called. If I print it in the dbConnect function, it shows it's a resource but if I try to print it after, it shows as an object. I'm not sure why. As I said, I'm new, so go easy Quote Link to comment https://forums.phpfreaks.com/topic/226922-mysql_query-expects-parameter-2-to-be-resource/ Share on other sites More sharing options...
trq Posted February 7, 2011 Share Posted February 7, 2011 Firstly, this.... $link = mysql_connect('localhost', 'user', 'pass'); Should be.... $this->link = mysql_connect('localhost', 'user', 'pass'); Then, the arguments passed here are in the wrong order..... mysql_query($this->link, $query); Quote Link to comment https://forums.phpfreaks.com/topic/226922-mysql_query-expects-parameter-2-to-be-resource/#findComment-1170872 Share on other sites More sharing options...
trq Posted February 7, 2011 Share Posted February 7, 2011 Actually, another look at your code and there is stuff pretty much all over the place. Firstly, dbConnect should not extend siteConfig, they are in no way related. Secondly, your construct in dbConnect does not return a connection. If you follow the advice in my other reply it doesn't need to. Finally, getConfig() doesn't return anything. Quote Link to comment https://forums.phpfreaks.com/topic/226922-mysql_query-expects-parameter-2-to-be-resource/#findComment-1170873 Share on other sites More sharing options...
Neji Posted February 7, 2011 Author Share Posted February 7, 2011 Thanks for the help! I've got it working following your advice but still have a couple of questions. dbConnect extends siteConfig because I want to bring in settings from the configuration.php file. It wasn't setup but it should bring in the values which are used to connect to the db (hostname, username & password). To achieve that, I would I need getConfig to return $config? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/226922-mysql_query-expects-parameter-2-to-be-resource/#findComment-1170935 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.