hostfreak Posted August 22, 2007 Share Posted August 22, 2007 Is it better to create my own class with mysqli and use the procedural style or the supplied OOP style in php5. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/ Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 To be honest, I'd stick with the procedural style for now because not on host seems to have enabled mysqli, it's really annoying me because I built a massive system based on it. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331456 Share on other sites More sharing options...
hostfreak Posted August 23, 2007 Author Share Posted August 23, 2007 Well, the host isn't an issue. I am running these scripts for developmental purposes on my own system. Then when I decide to put them on a host, I won't have a problem getting mysqli enabled (if it's not already). I was just curious if it would be better running my own class with procedural mysqli or the supplied oop mysqli. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331458 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 Ah. Yes, I have a db.class.php that basically robs the mysqli.class.. look  <?php /* db.class.php Matthew Haworth - 2007 */ class db {   private $_db;   private $_queries = 0;   //public $emptty;   // constructor.   function __construct($database, $username, $password, $host = "localhost")   {     $mysqli = new mysqli($host, $username, $password, $database);     $this->_db = $mysqli;     //whilst i'm here i'd like to check if there's anything in the table and if so apply false to a public var called empty     /*$sql = "SELECT * FROM `shouts`;";     $query = $this->query($sql);     if($query->num_rows > 0) {     $this->$emptty = false;     }     */   }   // query-er   function query($sql)   {     $queryresult = $this->_db->query($sql);     $this->_queries++;     return $queryresult;   }   // fetch-er   function fetch_assoc($query)   {     $fetched = $query->fetch_assoc();     return $fetched;   }   function multifetch_assoc($query)   {     while ($fetched = $query->fetch_assoc())     {       $mf[] = $fetched;     }     return $mf;   }   function numrows($query)   {     $query = $query->num_rows;     return $query;   }   function realescapestring($string)   {     $string = $this->_db->real_escape_string($string);     return $string;   } } ?>  It's stupid really, so I am also interested in the answer to this question.. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331462 Share on other sites More sharing options...
nloding Posted August 23, 2007 Share Posted August 23, 2007 Matthew and I are delving into abstract classes and design patterns and stuff ... interesting but still confusing me. Regardless of how I feel, building a MySQL or MYSQLi class wrapper is a must if you plan on using any design patterns, as I'm finding out.  Other than that, and again depending on the scope of the site, I don't see why you would need to. Personally I want to ... and did ... kinda ... Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331468 Share on other sites More sharing options...
hostfreak Posted August 23, 2007 Author Share Posted August 23, 2007 Well, here is what I have now: <?php class MySQL { Â Â private $Link; Â Â function __construct() Â Â { Â Â Â Â include("include/constants.php"); Â Â Â Â $this->Link = mysqli_connect(Host, User, PWRD, DB) OR die('Not Connected : ' . mysqli_connect_error()); Â Â Â Â mysqli_select_db($this->Link, DB) OR die('Can\'t use '.DB.' : ' . mysqli_error($this->Link)); Â Â } Â Â function Query($SQL) Â Â { Â Â Â Â $Query = mysqli_query($this->Link, $SQL) OR die('Invalid query: ' . mysqli_error($this->Link)); Â Â Â Â return $Query; Â Â }Â Â Â function fetchAssoc($Result) Â Â { Â Â Â Â $Array = mysqli_fetch_assoc($Result); Â Â Â Â return $Array; Â Â } Â Â function fetchNumRows($Result) Â Â { Â Â Â Â $NumRows = mysqli_num_rows($Result); Â Â Â Â return $NumRows; Â Â } Â Â function Close() Â Â { Â Â Â Â mysqli_close($this->Link); Â Â } Â Â function __destruct() Â Â { Â Â } } ?> Â As you can see, it is a class that uses the procedural mysqli. I'm not sure what is more efficient though, the oop mysqli or procedural. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331484 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 Well, here is what I have now: <?php class MySQL {   private $Link;   function __construct()   {     include("include/constants.php");     $this->Link = mysqli_connect(Host, User, PWRD, DB) OR die('Not Connected : ' . mysqli_connect_error());     mysqli_select_db($this->Link, DB) OR die('Can\'t use '.DB.' : ' . mysqli_error($this->Link));   }   function Query($SQL)   {     $Query = mysqli_query($this->Link, $SQL) OR die('Invalid query: ' . mysqli_error($this->Link));     return $Query;   }   function fetchAssoc($Result)   {     $Array = mysqli_fetch_assoc($Result);     return $Array;   }   function fetchNumRows($Result)   {     $NumRows = mysqli_num_rows($Result);     return $NumRows;   }   function Close()   {     mysqli_close($this->Link);   }   function __destruct()   {   } } ?>  As you can see, it is a class that uses the procedural mysqli. I'm not sure what is more efficient though, the oop mysqli or procedural.  Ah, I think I understand you now! I though you meant, should you use mySQL or mySQLi. My question is different, I was asking should we really create a class db.php if there's already mysqli as a class? Your question is should I say, $db = new mysqli(etc,etc); or mysqli_connect(etc,etc)... I don't think it makes a difference to be honest. But object oriented programmng is the way forward, so opt for that? I don't know. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331487 Share on other sites More sharing options...
keeB Posted August 23, 2007 Share Posted August 23, 2007 I say wrap it, but maybe in a way you haven't thought of.  Remember, if you can abstract (or hide details of) a piece of code in to it's own object, you probably should.  This is how I do it: <?php interface Database {   function connect($connString, $userName, $password);   .   .   . # other functions } class MySQL implements Database {   # implement database data here   function MySQL() {     $this->connect("localhost", "keeb", "ownsyou");   } } class UserDao extends MySQL { // User object factory...    function UserDao() {      parent::__construct();    }    function getUserData($name) {    } } ?>  Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331503 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 I say wrap it, but maybe in a way you haven't thought of.  Remember, if you can abstract (or hide details of) a piece of code in to it's own object, you probably should.  This is how I do it: <?php interface Database {   function connect($connString, $userName, $password);   .   .   . # other functions } class MySQL implements Database {   # implement database data here   function MySQL() {     $this->connect("localhost", "keeb", "ownsyou");   } } class UserDao extends MySQL { // User object factory...    function UserDao() {      parent::__construct();    }    function getUserData($name) {    } } ?>  Make sense?  That'd produce an error because you didn't use all the functions defined in the interface, you didn't create the connect function in mySQL.. unless you skipped deliberately..  Anyways, that's not what the poor threadstarter is asking for, it's my fault, I've led this thread off track. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331512 Share on other sites More sharing options...
hostfreak Posted August 23, 2007 Author Share Posted August 23, 2007 Not to worry, your question was also one I had in the back of my head. Â Anyways, yeah your on with my question in reply #6. I just wasn't sure which way was more efficient; using the oop mysqli or procedural mysqli with my own class. Now that I think of it though, my own class is probably better as I have other functions as well. Â Then I see your using the oop mysqli in your own class. Does that make a difference? Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331519 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 Not to worry, your question was also one I had in the back of my head. Â Anyways, yeah your on with my question in reply #6. I just wasn't sure which way was more efficient; using the oop mysqli or procedural mysqli with my own class. Now that I think of it though, my own class is probably better as I have other functions as well. Â Then I see your using the oop mysqli in your own class. Does that make a difference? Â There is no difference in performance between using object oriented and procedural, it's genuinely a matter of preference, I prefer object oriented mysqli, because on query, with things like SELECT it returns an object and I think it's easier to deal with. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331523 Share on other sites More sharing options...
hostfreak Posted August 23, 2007 Author Share Posted August 23, 2007 I see. I think for my own preference, for the sake of keeping the classes separate, I will stick to procedural. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331530 Share on other sites More sharing options...
keeB Posted August 23, 2007 Share Posted August 23, 2007 Yes, it was omitted. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-331539 Share on other sites More sharing options...
deadimp Posted September 16, 2007 Share Posted September 16, 2007 If you're referring to the API style, then I'd use OOP, since you've already done so in the class you posted. Using the global functions 'mysqli_x()' are redundant when you have the link. It'd be easier just to type '$this->link->x()'. Be sure to look into mysqli_result in the documentation also. Quote Link to comment https://forums.phpfreaks.com/topic/66268-solved-mysqli/#findComment-349750 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.