MoFish Posted January 12, 2014 Share Posted January 12, 2014 Hi, I'm getting the following error and im not entirely sure why - Call to a member function fetch_array() on a non-object When I execute the SQL query manually in phpmyadmin it displays 'total' a value of as 7000 which is correct - so the query appears correct. Could anyone point me in the right direction? Something simple im missing. public function get_staff_current_month($staff_id) { $db = new sql(); $sql = "SELECT SUM(amount) as total FROM $this->tablename WHERE staff_id = $staff_id AND YEAR(date_added) = YEAR(CURDATE()) AND MONTH(date_added) = MONTH(CURDATE()) ORDER BY id ASC"; $result = $db->query($sql); //var_dump($result); $row = $result->fetch_array(); return $row['total']; } Regards, MoFish. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 12, 2014 Share Posted January 12, 2014 without knowing the methods inside your sql class $db = new sql(); we would only be guessing Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted January 12, 2014 Share Posted January 12, 2014 you also should not be creating a new instance of your database class inside of another class method just to run each query. Quote Link to comment Share on other sites More sharing options...
MoFish Posted January 12, 2014 Author Share Posted January 12, 2014 without knowing the methods inside your sql class we would only be guessing <?php class sql { var $name; var $host; var $username; var $password; var $mysql_link; var $sql_query; var $sql_result; var $error; function sql() { $this->name=MYSQL_DB; $this->host=MYSQL_HOST; $this->username=MYSQL_USER; $this->password=MYSQL_PASS; $this->connection(); } function connection() { $this->mysql_link=mysql_connect($this->host,$this->username,$this->password) or die ('Error connecting to MySQL'); mysql_select_db($this->name, $this->mysql_link) or die('Database '.$db['db'].' does not exist!'); } function query($sql_query) { $this->sql_query=$sql_query; $this->sql_result=mysql_query($this->sql_query); $this->error=mysql_error(); if (!$this->sql_result) { echo "<b>" . $sql_query . "</b><br><br>"; $this->error=mysql_error(); echo $this->error; } echo mysql_error(); } function num_rows() { $mysql_rows = mysql_num_rows( $this->sql_result ); return $mysql_rows; } function fetch_array() { if ( $this->num_rows() > 0 ) { $mysql_array = mysql_fetch_array( $this->sql_result ); if (!is_array( $mysql_array )) { return FALSE; } return $mysql_array; } } function insert_id() { return mysql_insert_id(); } } ?> you also should not be creating a new instance of your database class inside of another class method just to run each query. Should I therfore be setting db one at the first instance? Thank you for your help. Quote Link to comment Share on other sites More sharing options...
MoFish Posted January 12, 2014 Author Share Posted January 12, 2014 I dont know if this is very pretty but it works? $row = $db->query($sql); if($db->num_rows()){ while($k = $db->fetch_array($row)) { return $k['total']; } } Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 12, 2014 Solution Share Posted January 12, 2014 try public function get_staff_current_month($staff_id) { $db = new sql(); $sql = "SELECT SUM(amount) as total FROM $this->tablename WHERE staff_id = $staff_id AND YEAR(date_added) = YEAR(CURDATE()) AND MONTH(date_added) = MONTH(CURDATE()) ORDER BY id ASC"; $db->query($sql); $row = $db->fetch_array(); return $row['total']; } Create the connection to the database once. Pass it as a parameter to the functions that need it Quote Link to comment Share on other sites More sharing options...
MoFish Posted January 12, 2014 Author Share Posted January 12, 2014 Thank you Barand. 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.