0

I am trying to write php to connect data from myqsl (i use free host 000webhost.com)

file connect:

<?php

    $connect = mysqli_connect("files.000webhost.com:21", "nguyendang", "password", "foodcourt_db") or die ("could not connect to sever " . mysqli_connect_error()) ;
    mysqli_query($connect, "SET NAMES 'utf8'"); 

?>

I had these errors when i ran

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Can you add the relevant code ( redact passwords etc ) and show how it is being used. Are you using `stored procedures` ? – Professor Abronsius Jul 08 '20 at 10:06
  • is it really using port 21? That is typically reserved for FTP – Professor Abronsius Jul 08 '20 at 10:09
  • when i try with no port, it shows this : "Warning: mysqli_connect(): (HY000/2002): Connection refused in /storage/ssd4/959/14261959/public_html/db_conn.php on line 2 could not connect to severConnection refused" – Đăng Nguyễn Jul 08 '20 at 10:15
  • i dont know what is the typical port – Đăng Nguyễn Jul 08 '20 at 10:20
  • Does this answer your question? [How to connect to MySQL database in PHP?](https://stackoverflow.com/questions/31376095/how-to-connect-to-mysql-database-in-php) – Dharman Jul 08 '20 at 10:25
  • See https://stackoverflow.com/questions/10053613/apache-mysql-packets-out-of-order-on-3306 – Dharman Jul 08 '20 at 10:25
  • 2
    My guess is that you've mixed up the mysql server with the ftp server. Not only are you using port 21, which usually is used for FTP (Mysql defaults to 3306). Then the URL starts with `files.` which is an odd subdomain for a SQL server. I would recommend that you talk to the hosting company and ask what host, database and credentials you should use to connect to the database. You should also ask if they support remote connections or if you need to use `localhost` and run the code on their servers. – M. Eriksson Jul 08 '20 at 10:30
  • i think i see my problem, tks guys – Đăng Nguyễn Jul 08 '20 at 10:38

2 Answers2

0

Try hostname with localhost. And also check the MySQL server is currently running. This may happen in hosting for less duration due to overload of MySQL Server

Praveen
  • 294
  • 4
  • 9
0

Try to use this connection:

$link = mysqli_connect("localhost", "username", "password", "database_name");
//check connection
if($link === false) {
die("ERROR could not connect. " .mysqli_connect_error());
}
avi haviv
  • 1
  • 1