0

I am using an Ajax Search form which upon connecting to the database, will show search terms upon typing.

But I am getting database connection issue on my site.

This is the code I have in my connection file (db.inc):

<?php

$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";

mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($database) or die(mysqli_error()); 

?>

But on the frontend, I am getting errors as below:

Warning:  mysqli_select_db() expects exactly 2 parameters, 1 given in xxxx/db.inc.php on line 9
Warning:  mysqli_error() expects exactly 1 parameter, 0 given in xxxx/db.inc.php on line 9

Can anyone please check what I am doing wrong? I have pasted the complete db.inc file above.

Thank you in advance for helping.

  • 1
    Looks like a mysql_ to mysqli_ conversion, have a look at https://stackoverflow.com/questions/1390607/how-to-change-mysql-to-mysqli which may have other things you may encounter. – Nigel Ren Apr 21 '18 at 07:39
  • Don't just change `mysql` to `mysqli` , RTFM as well. – DEarTh Apr 21 '18 at 07:44

4 Answers4

3

As the error is saying, mysqli_select_db() expects 2 parameters. The first one is a link returned by mysqli_connect() and the second parameter should be the database name. The second error means that you have to add the link to the connection as a parameter to mysqli_error. So you'll have to write it like this:

$link = mysqli_connect($hostname, $username, $password);

if (!$link) {
    die('Connection error: ' . mysqli_connect_error());
}

mysqli_select_db($link, $database)); 
  • 1
    Why do mixup Mysql and Mysqli? – denny Apr 21 '18 at 07:40
  • Another thing, `error()` function cannot be used in case of error of `mysqli_connect()`. – Syscall Apr 21 '18 at 07:43
  • Hi Mădălin, Thanks a lot it's working fine now. Struggling with this from a day. Now I have another error. Upon adding the search term, it shows another error: Fatal Error: : Uncaught Error: Call to undefined function mysql_query() in xxxx/search.php:56 Stack trace:#0 {main} thrown in xx/search.php:56 The code on that line are: http://take.ms/zAtiW – Jitendra Mishra Apr 21 '18 at 07:47
  • MySQL extension became deprecated and it was removed in PHP 7. You should use [MySQLi](http://php.net/manual/en/book.mysqli.php) extension or PDO. So instead of `mysql_query` you will use `mysqli_query`. –  Apr 21 '18 at 07:52
  • Thanks again. I have made mysql_query() to mysqli_query() but now a new error shows: : Warning: mysqli_query() expects at least 2 parameters, 1 given in xxx/search.php line 56 – Jitendra Mishra Apr 21 '18 at 07:56
  • @jitendra Mishra, `mysqli_query()` expects first parameter to be the link to the connection and the second one the query. Check the [documentation](http://php.net/manual/ro/mysqli.query.php) about mysqli_query. I edited my answer. You should use `mysqli_connect_error()` instead of `mysqli_error()` to get the connection error. `mysqli_error()` it's used to get the last error of a `mysqli_query()`. Sorry for the mistakes in my answer. I hope it's ok now. –  Apr 21 '18 at 08:12
  • Hi Mădălin, Thanks for all your help and tips. I have managed to fix the issue completely. :) – Jitendra Mishra Apr 21 '18 at 08:46
1
$username = "xxxx";
$password = "xxxx";
$hostname = "xxxx";
$database = "xxxx";

$conn=mysqli_connect($hostname, $username, $password) or die(mysql_error());
mysqli_select_db($conn, $database) or die(mysqli_error()); 
jvk
  • 2,133
  • 3
  • 19
  • 28
0

Here, the second parameter is missing. So, the correct syntax will be

mysqli_select_db($link, $database) or die(mysqli_error());

You are using Procedural Style and in this style, two parameters will be passed in mysqli_select_db().So, the correct syntax will be

bool mysqli_select_db ( mysqli $link , string $dbname ) 

link Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

dbname The database name.

For more details, you can find all the details https://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.select-db.html

WordpressCoder
  • 303
  • 1
  • 7
0

You can also add the database name in the single line which will be mysqli_connect($hostname, $username, $password, $database);

WordpressCoder
  • 303
  • 1
  • 7