24

I get the following error

Warning: mysqli_error() expects exactly 1 parameter, 0 given

The problem is with this line of the code:

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

The whole code is

session_start();

require_once "scripts/connect_to_mysql2.php";

//Build Main Navigation menu and gather page data here

$sqlCommand = "SELECT id, linklabel FROM pages ORDER BY pageorder ASC";

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) { 
    $pid = $row["id"];
    $linklabel = $row["linklabel"];

    $menuDisplay .= '<a href="index.php?pid=' . $pid . '">' . $linklabel . '</a><br />';

} 
mysqli_free_result($query); 

The included file has the following line

$myConnection = mysqli_connect("$db_host","$db_username","$db_pass","$db_name") or die ("could not connect to mysql"); with reference to $myConnection, why do I get this error?
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aasim Azam
  • 263
  • 1
  • 4
  • 9
  • Something unrelated to the question: "$var" is redundant. That just opens a string, sees the $var, places its value into the string and then drops out of the string. In other words, you can just use $var. Like mysqli_connect($db_host, $db_username....) – Corbin Sep 30 '11 at 09:46
  • 1
    @Corbin, it's not necessarily redundant. Some built-in functions are strict about the types they accept, and `"$var"` will coerce a non-string variable to a string type for passing to the function. So if `$var = 0;`, `"$var"` is `"0"`. – eyelidlessness Sep 30 '11 at 09:49
  • In this situation (mysql_connect), it's definitely redundant. Also, can you name a built in function that is that strict about that? And I would find (string) $var cleaner, but "$var" would make just as much sense (and be shorter). – Corbin Sep 30 '11 at 09:51
  • 2
    I suspect you've overlooked the error message as irrelevant. It's telling you the exact line where the error is, together with a the most clear explanation possible. – Álvaro González Apr 28 '14 at 11:25
  • Instead of oldschool `or die()`, try proper `Exception` handling. – Daniel W. Apr 28 '14 at 11:27
  • 2
    @Aasim Azam Your issue is that in mysqli you need to use: **mysqli_connect_error()** (this is only for connection error!) instead of **mysqli_error($myConnection)**. – Skylex Oct 27 '16 at 02:14

4 Answers4

44

mysqli_error() needs you to pass the connection to the database as a parameter. Documentation here has some helpful examples:

http://php.net/manual/en/mysqli.error.php

Try altering your problem line like so and you should be in good shape:

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection)); 
Jason
  • 514
  • 5
  • 7
6

mysqli_error function requires $myConnection as parameters, that's why you get the warning

devpro
  • 16,184
  • 3
  • 27
  • 38
5

Change

die (mysqli_error()); 

to

die('Error: ' . mysqli_error($myConnection));

in the query

$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

At first, the problem is because you didn't put any parameter for mysqli_error. I can see that it has been solved based on the post here. Most probably, the next problem is caused by the wrong file path for the included file...

Are you sure the following code is in the 'scripts' folder and your main code file is on the same level as the script folder?

$myConnection = mysqli_connect($db_host,$db_username,$db_pass,$db_name) or die ("could not connect to mysql");
Dharman
  • 30,962
  • 25
  • 85
  • 135
sicKo
  • 1,241
  • 1
  • 12
  • 35