-4

This code works correctly!

$con=mysqli_connect("localhost","root","","laboratory");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM test");

while($row = mysqli_fetch_array($result))
  {
  echo $row['name'];
  echo "<br>";
  }

mysqli_close($con);

But when I remove database_name from mysqli_connect I would use the mysql_select_db, the following error occurs "Warning: mysql_select_db() expects parameter 2 to be resource, object given in"

Code after change:

$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$db_selected = mysql_select_db("laboratory", $con);

if (!$db_selected)
  {
  die ("Can\'t use laboratory : " . mysql_error());
  }

$result = mysqli_query($con,"SELECT * FROM test");

while($row = mysqli_fetch_array($result))
  {
  echo $row['name'];
  echo "<br>";
  }

mysqli_close($con);
Daniyal Javani
  • 229
  • 1
  • 5
  • 10
  • 2
    You cannot call mysql* functions on mysqli* resources. And it is not required as there is `mysqli_select_db` – hek2mgl Sep 28 '13 at 09:14

3 Answers3

4

Replace Your Code:

$db_selected = mysqli_select_db("laboratory", $con); instead of

$db_selected = mysql_select_db("laboratory", $con);
Guru
  • 621
  • 1
  • 4
  • 12
2

Please, don't mix mysqli and mysql as they are different modules.

In your second code block you are using mysql_select_db and mysql_error, the first one requires mysql connection, not mysqli connection.

kworr
  • 3,579
  • 1
  • 21
  • 33
  • 1
    @Daniyal did you change $db_selected = mysql_select_db("laboratory", $con); to use mysqli equivalent instead? – OIS Sep 28 '13 at 09:23
-1

The order of the parameters has been changed from:

mysql_select_db($Database, $Connection);

to:

mysqli_select_db($Connection, $Database);
Wendelin
  • 2,354
  • 1
  • 11
  • 28
Zardiw
  • 11
  • 10