-1

I have some existing PHP code that is now throwing a fatal error since we migrated to PHP 7.0. Using this Stackoverflow Question I've altered this line:

$link = mysql_connect($host.':'.$port, $user, $pass) or die("Can not connect." . mysql_error());

to this:

$link = new mysqli($host.':'.$port, $user, $pass);

I am now throwing this error which I guess is some progress. The syntax seems fine? What am I missing?

Warning: mysqli::__construct(): (HY000/2002): Connection timed out
Community
  • 1
  • 1
Rocco The Taco
  • 3,695
  • 13
  • 46
  • 79
  • 2
    Did you try `$mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket);` as per http://php.net/manual/en/mysqli.construct.php – Funk Forty Niner Feb 21 '17 at 18:44

2 Answers2

1

The constructor class looks like this:

__construct (
    [ string $host = ini_get("mysqli.default_host") 
    [, string $username = ini_get("mysqli.default_user") 
    [, string $passwd = ini_get("mysqli.default_pw") 
    [, string $dbname = "" 
    [, int $port = ini_get("mysqli.default_port") 
    [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

So the order is host, username, password, database, port, socket. You'll need to pass in the port in a separate variable:

$link = new mysqli($host, $user, $pass, null, $port);

Edited because I copied the wrong bit of code..

aynber
  • 22,380
  • 8
  • 50
  • 63
0

Try just using the function.

$link = mysqli_connect($host,$user,$pass,$db).

The order is host, user, pass, db

http://php.net/manual/en/function.mysqli-connect.php

William Robertson
  • 15,273
  • 4
  • 38
  • 44
clearshot66
  • 2,292
  • 1
  • 8
  • 17