PHP connection problem with Mysql

Asked

Viewed 1,314 times

-1

inserir a descrição da imagem aquiAll the settings are right I don’t know why not connecting.. locally works, but when I step to the server gives this problem.

$hostnome = '192.185.176.204';
$usernome = 'tookc892';
$senha = '***'
$conexao = mysql_connect('$hostnome', '$usernome' , '$senha');
  • 2

    Enter the snippet of the code that presents the problem, so we can check.

  • 2

    There’s a very strange text in the middle... (?)

  • $hostname = '192.185.176.204'; $usernome = 'tookc892'; $password = '**' $connected= mysql_connect( '$hostname', '$usernome', '$password'); // the password did not set there any more correct here

  • Do you know you can accept answers? See tour. You can accept an answer per question you asked. You can even accept your answer if it was the one that helped you best.

  • There are two possible reasons: 1st mysql_query is being discontinued, 2nd you may be entering an infinite connection loop.

5 answers

5

You are using simple quotes on your connection.

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

Remove simple quotes that should work:

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

NOTE: The mysql_* functions are being discontinued by PHP, so prefer to work with something new and safer, like mysqli or PDO.

  • I made the changes but you’re still making the same mistake :/

  • @Patiihenrii There seems to be also a lock by Mysql, perhaps due to many invalid data accesses.

  • 2

    Concordance correction, PHP compiles the contents of simple quotes, it just does not interpret variables within the same... Even because the use of single quotes is lighter than double quotes, that is, in cases where there are no variables within the string the best is to use single quotes for optimized processing.

  • @Rodrigoborth because it is, I did not express very well at first, thank you for the correction. ^^

  • 1

    +1 for reinforcing the use of other technologies in place of depression mysql_*, beyond the clear answer.

4

2

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

Works locally because the local mysql configuration should be different from the remote configuration.

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

1

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

Forgot semicolon after variable $password; We still have to select the database... You must run $connected, then you could leave without setting it, so:

$hostnome = '192.185.176.204';
$usernome = 'tookc892';
$senha = '***';
$dbname = 'nome_banco';
mysql_connect("$hostnome, $usernome, $senha");
mysql_select_db("$dbname");

IN 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();
}

?>
  • I’ve done all these settings and it’s still giving the same error.. for many connection attempts. helps me.. :(

  • You managed to solve your problem?

  • I’ve been looking at the Mysql manual, you can open your phpmyadmin, go to the SQL tab and run the following command: SHOW STATUS The information returned highlights are: Connections (connections) and Aborted_connects (connections aborted). See if there are too many aborted connections, maybe this could be the problem, then you will need to reset the failed connections. From what I’ve been watching you can reset with the command: mysql_reset_connection() Which works from Mysql 5.7.3. See more about the command at: http://dev.mysql.com/doc/refman/5.7/en/mysql-reset-connection.html

  • Mysql aborts connections (Mysql Aborted_connects) when: - A client does not have privileges to connect to a database. - A client uses an incorrect password. - A connection package does not contain the correct information.

-1

An example of POO connection, 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 ...

  • Well, I wouldn’t recommend doing this for the following reasons: Your class is generating a high coupling, there are no parameters, so it could have static items. In case I need to change some information of my DB I would need to change the code of my class. You are using functions that are being discontinued so soon the class will become useless and will have to be rewritten. These are the main reasons, but there are several others.. =/

  • The idea was something didactic, I did not know that in PHP5.5 would decrypt the functions of mysql_, the privates variables were just to facilitate the vision, nowadays have PDO, alias you also used the same as me, it seems kind of confusing negative me but beauty.

  • It was not I who denied it. ;) hehe

Browser other questions tagged

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