-2

I have installed wamp server and am working on localhost.I am trying to connect database with following code

 <?php

$servername = "localhost";
$username = "";
$password = "";
$database ="weddingapp";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
mysqli_select_db("$database", $conn);
echo "Connected successfully";

?>

Connected successfully is coming along with error

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\wamp\www\Wedding-app\config.php on line 13

I am new to PHP help me in this Thanks in advance

nikita
  • 273
  • 3
  • 15

4 Answers4

1

Its clearly showing an error

mysql_select_db("$database", $conn);
                ^^^^^^^^^^^^^^^^^^

It must be

mysql_select_db($conn,$database);

Try to learn the function

mysqli_select_db ( mysqli $link , string $dbname )

SideNote: You were using deprecated mysql version start using mysqli or PDO

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • Glad to help. But before asking question try to google it or check what's wrong with your code. You can learn it from [PHP.NET/MANUAL](http://php.net/manual/en/) – Narendrasingh Sisodia Apr 17 '15 at 07:22
0
$conn = mysqli_connect($servername,$username,$password,$database) or die("Error " . mysqli_error($conn)); 
Muhammad Ahmed
  • 481
  • 2
  • 14
0

you are not giving user name, change to $username = "root";

Noor
  • 99
  • 2
  • 12
-3

try with this

Code:-

<?php

$servername = "localhost";
$username = "";
$password = "";
$database ="weddingapp";
// Create connection
$conn = mysql_connect($servername, $username, $password);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
mysql_select_db($database, $conn);
echo "Connected successfully";

?>

Mistake is you have given "$database" in line num:13 remove.

RaMeSh
  • 3,330
  • 2
  • 19
  • 31