Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?
                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, 2011 at 9:46
                @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, 2011 at 9: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, 2011 at 9:51
                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, 2014 at 11:25
                @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, 2016 at 2:14

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

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");