s_ainley87 Posted May 23, 2008 Share Posted May 23, 2008 hello, I am currently trying to tackle a php CMS using OO. My problem is I straight away get an err on the index page Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Outlane\occcms\includes\DbConnector.php on line 50 Now the databse exsists and there is a record in it so I dont see what the problem is dbconneccter code <?php //////////////////////////////////////////////////////////////////////////////////////// // Class: DbConnector // Purpose: Connect to a database, MySQL version /////////////////////////////////////////////////////////////////////////////////////// require_once 'SystemComponent.php'; class DbConnector extends SystemComponent { var $theQuery; var $link; //*** Function: DbConnector, Purpose: Connect to the database *** function DbConnector(){ // Load settings from parent class $settings = SystemComponent::getSettings(); // Get the main settings from the array we just loaded $host = $settings['localhost']; $db = $settings['outlane']; $user = $settings['*********']; $pass = $settings['*********']; // Connect to the database $this->link = mysql_connect($host, $user, $pass); mysql_select_db($db); register_shutdown_function(array(&$this, 'close')); } //*** Function: query, Purpose: Execute a database query *** function query($query) { $this->theQuery = $query; return mysql_query($query, $this->link); } //*** Function: getQuery, Purpose: Returns the last database query, for debugging *** function getQuery() { return $this->theQuery; } //*** Function: getNumRows, Purpose: Return row count, MySQL version *** function getNumRows($result){ return mysql_num_rows($result); } //*** Function: fetchArray, Purpose: Get array of query results *** function fetchArray($result) { return mysql_fetch_array($result); } //*** Function: close, Purpose: Close the connection *** function close() { mysql_close($this->link); } } ?> index code <b> WHAT'S NEW: </b><br> <?php // Require the database class require_once('includes/DbConnector.php'); // Create an object (instance) of the DbConnector $connector = new DbConnector(); // Execute the query to retrieve articles $result = $connector->query('SELECT ID,title FROM cmsarticles ORDER BY ID DESC LIMIT 0,5'); // Get an array containing the results. // Loop for each item in that array while ($row = $connector->fetchArray($result)){ echo '<p> <a href="viewArticle.php?id='.$row['ID'].'">'; echo $row['title']; echo '</a> </p>'; } ?> Quote Link to comment 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.