1

When launching my code I get a failed query and the following errors:

mysqli_query() expects parameter 1 to be mysqli, null given in

mysqli_error() expects parameter 1 to be mysqli, string given in

<?php
include('mysql_config.php');

function mysqlConnect()
{
    global $mysql_hostname, $mysql_username, $mysql_password, $mysql_database;
    $link = mysqli_connect($mysql_hostname, $mysql_username, $mysql_password) 
    or die('Could not connect: ' . mysqli_error());
    mysqli_select_db($link,$mysql_database) or die('Could not select database');
    return $link;
}

function mysqliClose($link)
{
    mysqli_close($link);
}

function sendQuery($query)
{
    $result = mysqli_query($link, $query) or die('Query failed: ' . mysqli_error("could not query"));
    return $result;
}

?>

How do I properly format the mysqli_query and mysqli_error functions?

John Smith
  • 387
  • 1
  • 4
  • 11
  • 1
    Where did `$link` come from? Check your `sendQuery` function.. Error is clear.. `$link` is not defined.. – Bhuvan Rikka Dec 22 '12 at 12:53
  • 2
    You have a scope problem: http://php.net/manual/en/language.variables.scope.php – Pekka Dec 22 '12 at 12:53
  • You better consider to change `or die(...)` approach to error handling. See http://www.phpfreaks.com/blog/or-die-must-die for starters. – peterm Dec 22 '12 at 20:51
  • Does this answer your question? [Warning: mysqli\_query() expects parameter 1 to be mysqli, null given in](https://stackoverflow.com/questions/18862743/warning-mysqli-query-expects-parameter-1-to-be-mysqli-null-given-in) – Dharman Jan 04 '20 at 20:40

1 Answers1

2

There are two errors in the code above:

  • You missed to declare $link global as $mysql_hostname etc.
  • You passed the wrong argument type to mysqli_error() it expects mysqli and you passed a string

I have changed your example:

<?php

include('mysql_config.php');

// declaring an additional global var.
$link = NULL;

function mysqlConnect()
{
    global $link; // using the global $link
    global $mysql_hostname, $mysql_username, $mysql_password, $mysql_database;
    $link = mysqli_connect($mysql_hostname, $mysql_username, $mysql_password) 
    or die('Could not connect: ' . mysqli_connect_error());
    mysqli_select_db($link,$mysql_database) or die('Could not select database');
    return $link;
}

function mysqliClose($link)
{
    mysqli_close($link);
}

function sendQuery($query)
{
    global $link; // using the global $link
    $result = mysqli_query($link, $query) or die('Query failed: '
      . mysqli_error($link)); // note $link is the param
    return $result;
}
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • In a statement `die('Could not connect: ' . mysqli_error())` you better use `mysqli_connect_error()` instead of `mysqli_error()`. See [mysqli_connect](http://php.net/manual/en/mysqli.construct.php) – peterm Dec 22 '12 at 20:42
  • 1
    It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jan 04 '20 at 20:39
  • @Dharman I agree on that. I just took the code from the OP. Not sure if it's worth to change the answer 8 years later, especially because I'm not coding PHP since years :) ... Feel free to edit it! – hek2mgl Jan 04 '20 at 21:26