1

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

  • Connect to MYSQLI
  • Do SELECT
  • Exit MYSQLI

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.

2
  • Have you looked at OOP? Is that something you are willing to consider as it would solve your issue nicely. Commented Jan 28, 2014 at 10:27
  • Your connection is actually not passed. Read about the variable scope Commented Jan 28, 2014 at 10:31

2 Answers 2

5

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hello. Your answer works very good. Except i can't get a result with "if($row)" subroutine) - If I use the following code, it works fine :) - while($row = mysqli_fetch_array($result)) { print_r($row); }
0

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();
?>

9 Comments

And why do I get -1 for googling that for him? I did the work he would be able to do on his own within 5 seconds.
This should be a comment.
But it directly answers his question and would finish this topic so there is no need to create a comment.
It doesn't answer his question at all. All you do is spout what he can read. What he wants is a function he can call, that's not a function.
Well, it contains exactly what he demanded "Has anyone got a working / tested example of mysqli using functions (not just half the code) - but a working example of simple SELECT" - should I reinvent the wheel? When I write it out of my head it wont look much different you know...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.