PHP connection problem with MySQL

-1

All the settings are correct I do not know why it is not connecting .. locally it works, but when I go to the server it gives this problem.

$hostnome = '192.185.176.204';
$usernome = 'tookc892';
$senha = '***'
$conexao = mysql_connect('$hostnome', '$usernome' , '$senha');
    
asked by anonymous 11.03.2014 / 20:55

5 answers

5

You are using single quotes on your connection.

Variables within single quotes are not compiled by PHP, so the server is literally trying to connect to the user '$usuario' , to the host '$hostnome' with the password '$senha' and not to the value contained in the variables , as you wish.

Remove the single quotes that should work:

<?php $conexao= mysql_connect( $hostnome, $usernome , $senha);

NOTE: The mysql_ * functions are being deprecated by PHP, so you may want to work with something newer and more secure, such as mysqli or PDO.

    
11.03.2014 / 21:06
4

Your hostname has been blocked by MySQL because it exceeds the default of 10 attempts ... Open the console of your MySQL and enter:

mysql> SET GLOBAL max_connect_errors=10000;

Reference: link

    
11.03.2014 / 21:24
2

The first error was explained by Matthew, there should be no single quotes in your mysql_connect. After many attempts to connect with error your host has been blocked, you have to unlock it with the command: mysqladmin flush-hosts to be able to access again.

It works locally because the local mysql configuration must be different from the remote configuration.

I would also like to stress that you should abandon the mysql commands * replacing at least mysqli * under the risk of one day your site will stop working.

    
11.03.2014 / 21:21
1
$hostnome = '192.185.176.204';
$usernome = 'tookc892';
$senha = '***';
$dbname = 'nome_banco';
$conexao = mysql_connect("$hostnome", "$usernome", "$senha");
$sel_banco = mysql_select_db("$dbname");

You forgot the semicolon after the $ password variable; Still need to select the database ... You should run $ connection, so I could leave it without defining it, like this:

$hostnome = '192.185.176.204';
$usernome = 'tookc892';
$senha = '***';
$dbname = 'nome_banco';
mysql_connect("$hostnome, $usernome, $senha");
mysql_select_db("$dbname");
PDO:
<?php
//Arquivo de conexão com o Banco de Dados
try{
    $driver = 'mysql';
    $host = '192.185.176.204';
    $usuario = 'tookc892';
    $senha = '***';
    $dbname = 'nome_banco';

    $conn = new PDO("$driver:host=$host;dbname=$dbname", $usuario, $senha); 
} catch (PDOException $e){
    echo "Erro ao conectar no banco de dados $dbname <BR/>";
    echo $e->getMessage();
    die();
}

?>
    
12.03.2014 / 00:09
-1

A connection example in POO, I used to use it I think it's working ...

class Db{

//Variáveis Privadas//

private $user       = 'root';
private $password   = 'senha';
private $database   = 'nomedobanco';
private $host       = 'localhost'; 

/**************************************
*              Conectar              *
*      Função de conexão protected   *
**************************************/
 protected function conect(){
   if( mysql_connect($this->host, $this->user, $this->password)){
       if(!mysql_select_db($this->database)){
            return ('Erro na seleção do banco');
       }else{
            return true;
       }
   }else{        
       return ('Não foi possível conectar ao banco de dados.');
   }
 }
}

I hope it helps ...

    
12.03.2014 / 15:48