PHP - Mysql remote connection

Asked

Viewed 145 times

0

Good Afternoon!

I am setting up a server to host an API, for my connection tests I am using the code below, however I can not make the connection to the database and nor see what error PHP returns. I’ve searched the internet and I don’t understand what’s wrong with my code. I’ve been able to connect and write to the bank with a Python script.

<?PHP

# PHP 7
$conexao = mysqli_connect('187.182.164.73','Admin','SENHA');
$banco = mysqli_select_db($conexao,'pessoa');
mysqli_set_charset($conexao,'utf8');

$sql = mysqli_query($conexao,"select * from tb_pessoa") or die(error_reporting(E_ALL));
while($dados=mysqli_fetch_assoc($sql))
    {
        echo $dados['nome'].'<br>';
    }
?>
  • Utilize mysqli_error to verify which error.

  • Need to change password urgently, your DB is exposed on the internet and you passed the credentials in the post.

3 answers

1

You can use the function mysqli_connect_errno() to check which is the connection error or the function mysqli_query() to find out which query error is executed. For example on the connection do this:

$conexao = mysqli_connect('187.182.164.73','Admin','SENHA');

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}

Where the function mysqli_connect_error() will show what the error is.

In the executed query do so:

if (!mysqli_query($conexao,"select * from tb_pessoa") or die(error_reporting(E_ALL)))) {
  print_r(mysqli_error_list($conexao));
}

Where the mysqli_error_list() function will show the error when executing the query.

So you can see what the errors are in the database. I hope I’ve helped.

-1

$servidor = "HOST";
$usuario = "USUARIO";
$senha = "SENHA";
$dbname = "DB_NAME";
$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

$sql = "SELECT * FROM tb_pessoa";
$resultado = mysqli_query($conn, $sql);
while($dados= mysqli_fetch_assoc($resultado)){
     echo "$dados['nome'] . "<br>";
}

-2

If you are using apache, open php.ini you can change the "display_errors" from "Off" to "On".

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • 3

    @Tiedttech, when making a response exclusion recommendation and suggested text does not fit the case use the first option Não são necessários comentários and it is up to you to return the reply and leave a comment to the AR.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.