Jump to content

[SOLVED] MySQLI


hostfreak

Recommended Posts

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 4 weeks later...

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.