tags:

views:

84

answers:

1

I've never had to use mysqli until today and cannot seem to get this right. What I want is a simple function that will accept a mysql procedure call and have it return those results.

When I started using these procedures, I noticed that the old way of querying the database, using mysql_query, would no longer get me the expected results; one procedure would successfully return and the other would not. After reading the manual and several other examples out there, I found that the reason for this odd behavior was because the results need to be buffered then cleared. I have tried several ways of doing this and have been unsuccessful.

What I have so far works if I create another instance of the mysqli object and will get me both results however, I don't think it's right that I should have to instantiate 20 different objects to get back 20 different queries.

Again, what I want here, is to have a single function that I can feed a procedure to and have the results returned back.

$mysqli = new mysqli('host','user','password','test');
$rs = $mysqli->query('CALL titles()');
while($row = $rs->fetch_object())
{
  print_r($row);
}

$mysqli2 = new mysqli('host','user','password','test');
$rs2 = $mysqli2->query('CALL colours()');
while($row2 = $rs2->fetch_object())
{
  print_r($row2);
}
A: 

you can make a class for this:

db.php // A db class. Call this you perform a query.

<?php       
    class MyConnection{ 
        var $db_host = 'Localhost';
        var $db_user = 'mYUserName';
        var $db_password = 'myPassword';
        var $db_name = 'mYDB';
        var $connection;


        function Connect(){
            $this->connection = @mysqli_connect($this->db_host, $this->db_user, $this->db_password) 
            or 
            die("Error: ".mysqli_connect_error());
            mysqli_select_db($this->connection, $this->db_name);
            mysqli_query($this->connection, "SET NAMES 'utf8'");
        }

        function Disconnect(){
            mysqli_close($this->connection);
        }

        function ExecSQL($query){
            $result = mysqli_query($this->connection, $query)
                                or
                                die('Error: '.mysqli_error($this->connection));
            return $result;
        }   

    }

?>

Implementation:

include "db.php";
$conn = new MyConnection;
$conn->Connect();

$rs = $conn->ExecSQL('CALL titles()');
while($row = $rs->fetch_object())
{
  print_r($row);
}

$rs2 = $conn->ExecSQL('CALL colours()');
while($row2 = $rs2->fetch_object())
{
  print_r($row2);
}
 $conn->Disconnect();
junmats
junmats, thanks for the code.. I tried this and your right, it is getting me my results however, there is something really strange about the results. I have call to get all colours and a totally seperate call to get some titles. When I query using the ExecSQL method, I am getting BOTH results back and I am only calling the colours procedure. Why might this be?
Jim
Thank you junmats! I got it. You have a parse error on line 3. You forgot the semicolon. I added that and it works. Thanks again.
Jim
junmats, can I ask.. What is the difference between this, procedural class versus the OO way? Are there really any benefits to using either or?
Jim
If you would ask me, I think it is based on the programmers preference and the program you are trying to make.
junmats
Thats good enough for me. I'm just happy to have this script and your help. I've been fighting with this all day. Thanks again.
Jim