CyberShot Posted November 21, 2015 Share Posted November 21, 2015 I am trying to create my first class. learning how to do this. I have a database set up called "Gas". It's to keep track of my gas receipts to see how much I spend on fuel. I have one table called "stats" and have populated one row of information. I can connect to my database with my class but I am now getting an error saying that "No database selected". I don't understand why it's saying that when I have the database name set in the class variables. class database { var $server = "private"; var $db_user = "private"; // Enter your username var $db_pass = "private"; // Enter your password var $db_name = "Gas"; // Enter your database name private $result = array(); private function tableExists($table) { $tablesInDb = mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"'); if($tablesInDb) { if(mysql_num_rows($tablesInDb)==1) { return true; } else { return false; } } } function connect(){ $mysqli = new MySQLi($server, $db_user, $db_pass, $db_name); if($mysqli->connect_errno){ echo "Failed to connect to database: (" . $mysqli->connect_errno . ")" . $mysqli->connect_error; return false; } return $mysqli; } public function disconnect(){ } public function select($table, $rows = '*', $where = null, $order = null){ $q = 'SELECT '.$rows.' FROM '.$table; if($where != null) $q .= ' WHERE '.$where; if($order != null) $q .= ' ORDER BY '.$order; $query = mysqli_query($q); var_dump($q); // while( $row = mysql_fetch_assoc($query) ){ // echo $row['miles'] . ' ' . $row['amount'] . ' ' . $row['gallons'] . ' ' . $row['date']; // } } public function insert(){ } public function delete(){ } public function update(){ } } $mysqli = new database; ?> then in my index file, I did this $conn = $mysqli->connect(); $query = "SELECT * FROM stats"; //var_dump($query); $result = mysqli_query($conn, $query); if($result === false){ echo mysqli_error($conn); } var_dump($result); while($row = mysql_fetch_assoc($result)){ echo $row['miles']; } I know that the query is failing. It's returning FALSE. I am selecting my database in the class. So where am I going wrong? Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted November 21, 2015 Solution Share Posted November 21, 2015 You're randomly mixing mysql_* functions with mysqli_* functions. You cannot do this. They belong to two entirely different PHP extensions which are not related to each other (besides the fact that they both deal with MySQL). Actually, the mysql_* functions are hopelessly outdated and will be removed with the release of PHP7. The “var” keyword you're using in your class is also obsolete. It dates back to PHP4 which died somewhere around 2008. So it looks like you really need to update your learning resources. Nowadays, we use access modifiers (private, protected, public) instead of just “var”. What's weird is that you already use those modifiers for your methods. So I guess the “var” stuff was copied and pasted? Your database code is also somewhat shaky (no security, no proper error handling). I don't know. Maybe you should write your first class for a problem that you really understand. This database wrapper stuff usually just ends in a truckload of SQL injection vulnerabilities. Quote Link to comment Share on other sites More sharing options...
CyberShot Posted November 24, 2015 Author Share Posted November 24, 2015 (edited) I found those issues you pointed out and fixed them. It's amazing how things tend to work when you don't mix things up. It is true that a lot of the online tutorials are extremely out dated. I have looked really hard in my neck of the woods for a college that offers a class in PHP. Unfortunately, the best they can do is no where good enough. I have altered my class to use private for the class variables. This database will only run locally on my machine using wamp. I don't plan on putting it live online. It's for my own personal use to try and learn this stuff. Edited November 24, 2015 by CyberShot 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.