Page 1 of 1

newb to php

Posted: Tue Aug 11, 2009 6:07 pm
by jonnygrim
Hi All,

Im very new to php and learning some scripts that I have been given. When entering the following code it comes back with the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. Can you offer some suggestions to get round this.

Thanks

Code: Select all

<?php
 
    // Class: DBConnector
    // Purpose: Connect to to a database, MYSQL version
    
    require_once 'SystemComponent.php';
    
    class DBConnector extends SystemComponent {
        var $theQuery;
        var $link;
        
        // Function: DBConnector, Purpose: Connect to a Database
        function DBConnector(){
            
            // Load settings from parent class
            $settings = SystemComponent::getSettings();
            
            // Get the main settings from the array just loaded
            $host = $settings['dbhost'];
            $db = $settings['dbname'];
            $user = $settings['dbusername'];
            $pass = $settings['dbpassword'];
            
            // Connect to database
            
            $this->link = mysql_connect($host, $user, $pass);
            mysql_select_db($db);
            register_shutdown_function(array(&$this, 'close'));
        
        }
            // Function: Query, Purpose: Execut a Database Query
            function query($query) {
                
                $this->theQuery = $query;
                return mysql_query($query, $this->link);
            }
            
            // Fuction: 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);
            
            }
    }
    
?>
im using php 5.2.9, apache 2.2.11, mysql 5.1

Thanks in advance

Re: newb to php

Posted: Wed Aug 12, 2009 5:45 am
by robnet
It's probably the mysql_query() that's failing. Try tweaking it to give you some errors:
Instead of

Code: Select all

return mysql_query();
Try:

Code: Select all

$query_output = mysql_query($query, $this->link) or die(mysql_error());
return $query_output;
If it's not making sense it might be worth having a look at the actual query too

Code: Select all

echo $query;

Re: newb to php

Posted: Wed Aug 12, 2009 8:47 am
by jonnygrim
That worked like a treat, many thanks.