-2

I have this piece of code that works fine with php5.6 but it breaks with php7.2. I had three errors and I was able to fix two, I believe :)

$link = mysql_connect($hostname, $username, $password);
if (!$link) {
    die('Could not connect !');
    exit();
}
else{
    mysql_set_charset('utf8',$link);
    mysql_select_db($database, $link) or die('Could not select database.');
    }

this is what I have so far

$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
    die('Could not connect !');
    exit();
}
else{
    mysqli_set_charset('utf8',$link);
    mysqli_select_db($database, $link) or die('Could not select database.');
    }

Fatal error with mysql_select_db()

How can I make this function work with php 7.2? any help will be greatly appreciated, thank you in advance!!

mp5163093
  • 37
  • 1
  • 5
  • 2
    The `mysql_*` functions have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use [**mysqli**](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Aug 01 '18 at 20:59
  • 1
    I *highly* suggest you read through the manual and the syntax used for the mysqli api: http://php.net/manual/en/book.mysqli.php - You will see that what you tried, differs from it. – Funk Forty Niner Aug 01 '18 at 20:59
  • Your question title is not very informative. Remember that future researchers are going to want to _find_ the knowledge that you will eventually received. Please make your title as clear, informative, and searchable as logically possible. Right now, your title is terribly vague. – mickmackusa Aug 01 '18 at 21:00
  • Why not declare the database in your `$link` declaration? You _did_ read the manual before coming here, right? – mickmackusa Aug 01 '18 at 21:02
  • You have reversed `$database` and `$link`. I am voting to close as _Off-topic: Typo_. – mickmackusa Aug 01 '18 at 21:04

1 Answers1

0

The mysqli_* API requires that the database connection come first (unlike the old and deprecated mysql_* API which is no longer supported in PHP 7.2):

$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
    die('Could not connect !');
    exit();
}
else{
    mysqli_set_charset($link, 'utf8');
    mysqli_select_db($link, $database) or die('Could not select database.');
    }

References:

Make sure that you did in fact assign something to the $database variable; that wasn't included in your post.

Edit: As stated in comments by Riggs, you can use 4 arguments for the connection instead.

I.e.:

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

The choice is yours.

Comment:

Much better to put the database as param 4 of the connect. mysqli_select_db() is really only for when you want to switch from one db to another using the same connection – RiggsFolly

...indeed.


In regards to the error:

Fatal error with mysql_select_db()

I hope that you're not mixing this with anything else. Those different MySQL API's do not intermix with each other.

That error alone means that it is "fatal" error as shown and is the result of that API not being supported by your server.

Using PHP's error reporting and set to catch and display, would have (most likely) thrown you an error of deprecation and/or possibly not being supported.

Reference:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141