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