1

I've just started to look into mysqli. When I make the mysqli connection with the database I am having a problem with the function.

My Code:

$link=mysqli_connect("host", "user","password","database_name", true) or die ('I cannot connect to the database because: ' . mysqli_connect_error()); 

$query="CALL `database_name`.`table_name`('107','0100000000',63,122)";
  $result=mysqli_query($link,$query);
  $rs = mysqli_fetch_array($result);

$image = $rs['image'];
$title = $rs['title'];

echo $title;

mysqli_close($link);

When I execute this in my PHP code it throws an error.

Error:

Can't connect to MySQL server on 'host_name' (110)

But when I use it with MySQL extension it works fine.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • MySQL is not going to deprecate "soon" it is already deprecated since ages. Its going to be totally removed soon. – Steini Nov 13 '14 at 05:46
  • 1
    Use the right host name instead of `host_name`. – mario Nov 13 '14 at 05:46
  • 1
    check the _mysql_*_ config and above.... they are not same. i can bet on that. – itachi Nov 13 '14 at 05:47
  • 1
    The 5th argument is the port number. `true` evaluates to `1` and that's not the correct port. Just remove this argument as it is not required. (in `mysql_connect()` the 5th argument was 'new link'). – worldofjr Nov 13 '14 at 05:49

4 Answers4

3

The next aprameter after "database_name" is "port".

If you put true which evaluates to 1, you are trying to connect to port 1 (host_name:1)

Remove true or put the port of your MySQL Server incase you changed it (3306 is the standard port for MySQL and doesnt require to be passed)

http://php.net/manual/de/mysqli.construct.php

Steini
  • 2,753
  • 15
  • 24
2

Just remove the argument "TRUE". It is not required as you see below.

Try this one

$link = mysqli_connect('localhost', 'root', '');
    mysqli_select_db($link, 'database_name') or die ('I cannot connect to the database because: ' . mysqli_connect_error()); 
DJ MHA
  • 598
  • 3
  • 18
2

The 5th argument in mysqli_connect() is the port number.

true evaluates to 1 and that's not the correct port. Just remove this argument as it is not required - the default will be used.

In mysql_connect() the 5th argument was 'new link', so true was appropriate.

As you are just migrating to MySQLi, you might want to read this answer which gives you the basic changes from the old MySQL extension.


By the way, as you were using syntax for declaring a new connection, I thought you might want to know that you can have more than one connection when using mysqli. All you have to do is set it to a new variable, ie;

$link1 = new mysqli($host,$username,$password,$database);
$link2 = new mysqli($host2,$username2,$password2,$database2);

Then you can choose the connection you use when executing queries etc. For example;

Procedural:

$result1 = mysqli_query($link1,$sql);

OO:

$result2 = $link2->query($sql);
Community
  • 1
  • 1
worldofjr
  • 3,868
  • 8
  • 37
  • 49
0

One way of connecting -

Syntax for connection:

mysqli_connect(host,username,password,dbname,port,socket);

Example;

//connection 
$con = mysqli_connect("myhostname","myusername","mypassw","mydatabase") or die("Error".mysqli_error($con)); 

//query
$query = "SELECT * FROM mytablename" or die("Error" . mysqli_error($con)); 

//execute 
$result = mysqli_query($con, $query); 
worldofjr
  • 3,868
  • 8
  • 37
  • 49
Sunil Goli
  • 449
  • 4
  • 11