idire Posted November 18, 2008 Share Posted November 18, 2008 Sorry for making yet another thread. But I asked someone who works with object orientated php / mysql and they tell me that my code is all wrong. According to him, I should be using a predefined mysqli class. I'd guess it would be this: http://uk2.php.net/mysqli Does anyone know anything about how my code would need to be adapted to work with this class? Here is my current code: class_lib.php <?php class dbHandle { const HOST = 'localhost'; // Server const USERNAME = 'root'; // Username const PASSWORD = ''; // Password const DATABASE = 'db1'; // Database Name function connect() // Database Connection { $this->conn = mysql_connect(self::HOST, self::USERNAME, self::PASSWORD) or die("DB Connection Failure"); mysql_select_db(self::DATABASE); } function query($query) // Return generic query result { $result = mysql_query($query, $this->conn) or die("Query Failure: <br/> $query <br/>" . mysql_error()); return $result; } function fetchArray($query) // Return query result as an associative array { $result = $this->query($query); while($r = mysql_fetch_assoc($result)) { $output[] = $r; } return $output; } function numRows($query) // Return number of rows found by query. { $result = $this->query($query); $result = mysql_num_rows($result); return $result; } function disconnect() // Disconnect the connection. { mysql_close($this->conn); } } index.php <?php include("class_lib.php"); $conn = new dbHandle(); $conn->connect(); $result = $conn->fetchArray('SELECT * FROM `user`'); //print_r($result); for($i = 0; $i < count($result); $i++) { echo $result[$i]['username']; echo $result[$i]['password']; } Thanks in advance, the reason for this is I'm trying to get it working the correct way instead of having to go back later and learn a different way of coding. Quote Link to comment https://forums.phpfreaks.com/topic/133228-mysqli-class/ Share on other sites More sharing options...
premiso Posted November 18, 2008 Share Posted November 18, 2008 Honestly you can do it how you want to. mysqli class, if you can use it, does offer alot of easy to use functions and provides more functionality, but it is also limited to PHP 5 I believe. So if the code you are writing needs to be on a multitude of systems, than this is not the way to go. If it is only used on your current system, I would go with the mysqli class and use that. The end result is you do not have to use mysqli, although it is more efficient it is not required. I tend to find mysqli a bit nicer with the extras it provides. Quote Link to comment https://forums.phpfreaks.com/topic/133228-mysqli-class/#findComment-692912 Share on other sites More sharing options...
idire Posted November 18, 2008 Author Share Posted November 18, 2008 Its only to learn php, and will be running only on my own server, so I guess doing mysqli? For mysqli I would be doing this: <?php $conn = new mysqli(‘localhost’,‘root’,‘’,‘db1’); $sql = "SELECT * from 'user'"; if ($result = $conn->query($sql)) { while ($row = $result->fetch_object()) { echo ‘Username: ‘.$row->user; echo ‘Password: ‘.$row->password; } } That code basically replaces my current code? do I need to include anything at the top before using this code? Quote Link to comment https://forums.phpfreaks.com/topic/133228-mysqli-class/#findComment-692920 Share on other sites More sharing options...
Mchl Posted November 18, 2008 Share Posted November 18, 2008 No, you don't have to include anything to have mysqli working. You should not use quotes like ‘’ Use "" or '' Apart from that your code look fine, but here's my a bit improved version <?php $conn = new mysqli("localhost","root","","db1"); $sql = "SELECT * from 'user'"; if (!$result = $conn->query($sql)) { echo $conn->error.": ".$sql; //this will display query error if something went wrong } else { while ($row = $result->fetch_object()) { echo "Username: $row->user"; // you can use object variables within "" echo "Password: $row->password"; } } If you can, you should actually use mysqli. It is recommended for MySQL server versions from 4.1 up. mysql is not deprecated by any means but it is... well kinda old Quote Link to comment https://forums.phpfreaks.com/topic/133228-mysqli-class/#findComment-693018 Share on other sites More sharing options...
idire Posted November 18, 2008 Author Share Posted November 18, 2008 Thanks, it broke my line break expressions though, any idea why? echo "Username: $row->username"."\n"; echo "Password: $row->password \n"; those no longer make a new line. full code: <?php $conn = new mysqli("localhost","root","","db1"); $sql = "SELECT * from `user`"; if (!$result = $conn->query($sql)) { echo $conn->error.": ".$sql; } else { while ($row = $result->fetch_object()) { echo "Username: $row->username"."\n"; echo "Password: $row->password \n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/133228-mysqli-class/#findComment-693047 Share on other sites More sharing options...
corbin Posted November 18, 2008 Share Posted November 18, 2008 Try throwing some <pre> tags in there, or use <br />. To a browser, \n is insignificant. Quote Link to comment https://forums.phpfreaks.com/topic/133228-mysqli-class/#findComment-693049 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.