G Stewpot
G Stewpot

Reputation: 97

Using MYSQLI in seperate / distinct PHP functions

I'm transitioning from MYSQL to MYSQLI and I am in need of assistance with putting MYSQLI into separate / distinct functions.

From all the "tutorials" i have located on the web, they all have everyrything in one big long code, and not distinct / separate functions that my main scripts can call.

Eg :-

what i'm after is :-

MYSQLI.PHP

     <?
     function connect_mysqli()
     {
     $con=mysqli_connect("localhost","wrong_user","my_password","my_db");
     // Check connection
     if (!$con)
     {
     die("Connection error: " . mysqli_connect_errno();
     }
     // Return the connection back to where i called it ??
     }
     function do_query ($sql)
     {
     $row = $con->query("$sql")->fetch_array();
     return $row;
     }
     function close_mysqli()
     {
      $mysqli->close();
     }
     ?>

in my script i want to call :-

another.php

      <?
      include_once("MYSQLI.PHP");
       connect_mysqli();

         ....

        do some SELECT
       do some UPDATE

       close_mysqli();
        ?>

So far, from the error codes I am receiving, the "connection" to mysqli is not being passed to/from my other script(s)

Has anyone got a working / tested example of mysqli using functions (not just half the code) - but a working example of simple SELECT

Once i get that far, i can do the rest.

Upvotes: 1

Views: 313

Answers (2)

Steini
Steini

Reputation: 2783

Example Demos Scroll down there are a lot of exmaples...

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
    printf("Table myCity successfully created.\n");
}

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", $result->num_rows);

    /* free result set */
    $result->close();
}

/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}

$mysqli->close();
?>

Upvotes: 0

silly
silly

Reputation: 7887

fix your include file to

/**
 * @return mysqli
 */
function connect_mysqli()
{
    $con = mysqli_connect("localhost","wrong_user","my_password","my_db");
    // Check connection
    if (!$con)
    {
        die("Connection error: " . mysqli_connect_errno());
    }

    return $con;
}

function do_query ($con, $sql)
{
    $row = $con->query("$sql");
    if($row) {
        return $row->fetch_array();
    }
    return null;
}

function close_mysqli($con)
{
    $con->close();
}

now you can run a script like this

include_once("MYSQLI.PHP");
$connection = connect_mysqli();

if(null !== $connection) {
    print_r(do_query($connection, "SELECT * FROM yourTable"));

    close_mysqli($connection);
}

but for correct handling create a connection interface and a implementation for mysqli like this

interface myConnectionClass {
    function connect();
    ....
}

and a mysqli implementation

class myMysqlIConnection implements myConnectionClass {
    function connect() {
       //do more... save connection etc...
       return true; //sucess
    }
}

Upvotes: 5

Related Questions