marcin_koss Posted November 19, 2009 Share Posted November 19, 2009 Hello everybody. I am starting learning OO PHP and after reading many articles about it I am trying to start writing my first class which would be a class that handles mysql connections and performs queries. Below is the code I wrote so far but I'm already stuck with this error: "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\www\mysql_connect_oop.php on line 36" I would appreciete if somebody could take a look a the code and tell me where could be the problem. <?php // class mysql_con class mysql_con{ var $name; var $pass; var $host; var $db; var $query; var $result; function __construct($n, $p, $h, $d) { $this->name = $n; $this->pass = $p; $this->host = $h; $this->db = $d; } function connect() { mysql_connect($this->host, $this->name, $this->pass); mysql_select_db($this->db); } function query($q) { $this->query = $q; } function result() { $this->result = mysql_query($this->query) or trigger_error("Query: $this->query\n<br />MySQL Error: " . mysql_error()); } } // script that opens connection and performs query $db = new mysql_con('root','martek123','localhost','world'); $db->query('SELECT name FROM city WHERE name = "bialystok"'); while ($row = mysql_fetch_array($db->result)) { echo $row['name']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/182097-oop-mysql-class/ Share on other sites More sharing options...
premiso Posted November 19, 2009 Share Posted November 19, 2009 You never call the connect function before you run the query. Either put it in the constructor to call it automatically on the class instantiation or call it before you call the query function. Quote Link to comment https://forums.phpfreaks.com/topic/182097-oop-mysql-class/#findComment-960637 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.