# Looking for Help with PHP connect

**URL:** <https://www.phphelp.com/t/looking-for-help-with-php-connect/30770>\
**Category:** Beginners - Learning PHP\
**Created:** [January 8, 2020, 7:11pm UTC](https://www.phphelp.com/t/looking-for-help-with-php-connect/30770 "2020-01-08T19:11:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ITsoftdev](https://www.phphelp.com/letter_avatar_proxy/v4/letter/i/dfb087/32.png) [@ITsoftdev](https://www.phphelp.com/u/ITsoftdev)\
**Post date:** [January 8, 2020, 7:11pm UTC](https://www.phphelp.com/t/looking-for-help-with-php-connect/30770/1 "2020-01-08T19:11:47Z")

</div>

**Warning** : mysqli\_select\_db() expects parameter 1 to be mysqli, string given in **C:\xampp\htdocs\elegance\storescripts\connect.php** on line **18**  
**Notice** : Undefined variable: dbname in **C:\xampp\htdocs\elegance\storescripts\connect.php** on line **20**  
**Warning** : mysqli\_error() expects exactly 1 parameter, 0 given in **C:\xampp\htdocs\elegance\storescripts\connect.php** on line **20**  
Failed to connect to database

please i need help with this. i am new to php. below is my code

```auto
<?php 

//Step 1; create a database connection
 require_once('constant.php');
	
	$con = mysqli_connect(HOST, USER, PASSWORD);
	
	// check if connection is successful
	if(!$con){
		die("Database connection failed" . mysqli_error());
	}
	
?>

<?php 
//Step 2: connect to a database

$db = mysqli_select_db(DATABASE, $con);
if(!$db){
	die("Failed to connect to database {$dbname}" . mysqli_error());
}
```

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [January 8, 2020, 7:39pm UTC](https://www.phphelp.com/t/looking-for-help-with-php-connect/30770/2 "2020-01-08T19:39:00Z")

</div>

> [@ITsoftdev](#):
>
> **Warning** : mysqli\_select\_db() expects parameter 1 to be mysqli, string given in

> [@ITsoftdev](#):
>
> ```auto
> $db = mysqli_select_db(DATABASE, $con);
> 
> ```

Just instantiate the database properly,

```auto
mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
```

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [January 9, 2020, 7:15pm UTC](https://www.phphelp.com/t/looking-for-help-with-php-connect/30770/3 "2020-01-09T19:15:20Z")

</div>

> [@ITsoftdev](#):
>
> $db = mysqli\_select\_db(DATABASE, $con);

In this line, you’ve put `$con` and `DATABASE` the wrong way around. You can see [here](https://www.php.net/mysqli_select_db) that `mysqli_select_db` expects the database link that you created with `mysqli_connect`, then the database name. The line below should work:

```auto
mysqli_select_db($con, DATABASE);

```

The return value of `mysqli_select_db` will be `true` if the connection to the db is successful, `false` otherwise; unless you’re doing error checking you can ignore the return value.

As @astonecipher mentioned above, you can also make the connection in one call with the following:

```auto
$con = mysqli_connect(HOST, USER, PASSWORD, DATABASE);

```

As a final thing; while this is a very quick and easy way to get started playing with databases and PHP, it’s not considered the best practice. The current state of the art for connecting with databases in PHP is to use the [PDO class](https://www.php.net/pdo).
