10

I got this error in my code and I don't know how to solve it my code:

<?php
session_start();
include_once"connect_to_mysql.php";

$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "root"; 
// Place the password for the MySQL database here
$db_pass = "****"; 
// Place the name for the MySQL database here
$db_name = "mrmagicadam";

// Run the actual connection here 
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");        
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
$menuDisplay="";


while($row=mysql_fetch_array($query)) {
    $pid=$row["id"];
    $linklabel=$row["linklabel"];
$menuDisplay='<a href="index.php?pid=' .$pid . '">' .$linklabel. '</a><br/>';
}
mysqli_free_result($query);

?>

and this is error:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\limitless\connect_to_mysql.php on line 17

What I am doing wrong?

Dharman
  • 30,962
  • 25
  • 85
  • 135
shaymaa
  • 135
  • 1
  • 1
  • 4
  • Tip: use the search feature to search for the exact error message you're getting. I can guarantee you this question has been answered many times before. – deceze Apr 15 '12 at 08:50

2 Answers2

28

You are mixing mysqli and mysql extensions, which will not work.

You need to use

$myConnection= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); 

mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");   

mysqli has many improvements over the original mysql extension, so it is recommended that you use mysqli.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
F21
  • 32,163
  • 26
  • 99
  • 170
  • 3
    Or make the jump to PDO which offers many features MySQLi does not and makes it a lot easier to avoid SQL Injection. Plus it lets you do object mapping (in case you don't want to use an ORM but like the idea)... http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons – Jeremy Harris Apr 15 '12 at 08:56
7

You are using improper syntax. If you read the docs mysqli_query() you will find that it needs two parameter.

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

mysql $link generally means, the resource object of the established mysqli connection to query the database.

So there are two ways of solving this problem

mysqli_query();

$myConnection= mysqli_connect("$db_host","$db_username","$db_pass", "mrmagicadam") or die ("could not connect to mysql"); 
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysqli_query($myConnection, $sqlCommand) or die(mysqli_error($myConnection));

Or, Using mysql_query() (This is now obselete)

$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");        
$sqlCommand="SELECT id, linklabel FROM pages ORDER BY pageorder ASC";
$query=mysql_query($sqlCommand) or die(mysql_error());

As pointed out in the comments, be aware of using die to just get the error. It might inadvertently give the viewer some sensitive information .

Starx
  • 77,474
  • 47
  • 185
  • 261
  • In your second example you are mixing MySQL and MySQLi libraries. I've heard that works in some cases, but I imagine it is wise to stick to just one? – halfer Aug 01 '14 at 21:19
  • 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:37