0

What I'm trying to do is to create a connection to a MySQL database using a random function that returns (or I wish it would) a mysqli_result. Unfortunately it doesn't work. It returns boolean instead. The function:

function itsName()
{
    $host="localhost";
    $user="root";
    $password="";
    $database="";

    return mysqli_connect($host,$user,$password,$database);
}

And when I try to create a connection and use it in a query:

$con = itsName();

$query = mysqli_query($con, "ANY QUERY");
$fetch = mysqli_fetch_assoc($query);

Any normal query doesn't respond with an error, but when I try to, for example, fetch it to an assoc array it gives this error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\site\site.php on line X

It's obvious that itsName function returns boolean instead of mysqli_result.

My question is, is it even possible to achieve what I'm trying to do here? To make my function return a mysqli_result instead of boolean. If so, please help me out with how the code should look like.

And please don't ask why am I even trying to do something like this. Just an experiment.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Varelse
  • 11
  • 3
  • 4
    Actually, `mysqli_query()` returns you the bool, and not the connection part. You have an error in the query when you get that error. Turn on full error reporting to see what error you're getting from the query. – Andrius Nov 20 '15 at 10:05
  • 1
    Try to check the query's error with `mysqli_error($con);` – Ahmad Nov 20 '15 at 10:12
  • @deceze, may I ask for clarification? The OP makes this statement: *My question is, is it even possible to achieve what I'm trying to do here? To make my function return a mysqli_result instead of boolean. If so, please help me out with how the code should look like.*, seems like Andrius doesn't address the OP's question with his comment... – Can O' Spam Nov 20 '15 at 10:37
  • i tried, it works. You have probably an error in the query?! – Luca Giardina Nov 20 '15 at 10:39
  • @Sam Why do you think `mysqli_connect` returns a boolean? If it did, `mysqli_query` would throw an error, not `mysqli_fetch_assoc`! – deceze Nov 20 '15 at 10:42
  • Actually @Andrius was just in time, we are telling you the same story. The error is in the query, put the mysqli_connect inside a function works. – Luca Giardina Nov 20 '15 at 10:44

1 Answers1

-3

I think what you are looking for is something like this:

function Connect($hostName = null, $username = null, $password = null, $dbName = null, $port = null, $socket = null, $flags = null)
{
    $link = mysqli_init();
    $link->real_connect($hostName, $username, $password, $dbName, $port, $socket, $flags)
        or die("Failed to connect to the server");
    return $link;
}

$link = Connect("localhost", "username", "password", "db_name");
$link->query("SELECT * FROM `table` WHERE 1;");

You could also use the real_connect to detirmine the return:

return $link->real_connect($hostName, $username, $password, $dbName, $port, $socket, $flags) ? $link : false;

Then have:

$link = Connect("localhost", "username", "password", "db_name");
if (!$link)
    die("Failed to connect to the server");

But I honestly do not not why you'd want a wrapper for it??

Note This uses the OOP approach rather than the pragmatic version you used in your question.

Edits

The reason you are getting a bool is because both mysqli_connect and real_connect both return the boolean type, meaning you'd have to create a link of some form and then return it as I have in my Connect function example.

IMPORTANT! Also, when using mysqli_init, you need to ensure that the first mysqli function after is real_connect, else you will get an exception.

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45