Remote connection to MySQL works via command line, but fails when using php's mysql_connect from a web browser

StackOverflow https://stackoverflow.com/questions/11160068

  •  16-06-2021
  •  | 
  •  

Domanda

I am trying to connect to a MySQL server using PHP's 'mysql_connect()' function, but the connection fails. This is my code:

$con = mysql_connect("example.net", "myusername","") or die("Could not connect: ".mysql_error()); 

I placed this code inside a PHP script, which I try to open using a web browser (the script is stored on a remote host which has PHP enabled) but it doesn't work. It doesn't return the die error either. Echoing something before the $con successfully outputs in the browser, whereas nothing outputs after that line. If I type:

mysql -h example.net -u myusername

from a remote machine, I could connect to the DB without any problem and do queries and other modifications.

Update : I also tried this after some suggestion, but no improvement:

<?php
    $usern = "myusername";
    $dbh = new PDO('mysql:host=servername.net;dbname=test', $usern, "");
    echo $usern;
?>
È stato utile?

Soluzione

What operating system is the remote host running PHP using? Perhaps MySQL isn't enabled in php.ini. Also, please don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process (see the red box). Instead, you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you care to learn, this is a good PDO tutorial.

Have you tried using PDO or the MySQLi interface? If you're trying to learn PHP, you should not be using the mysql_* functions regardless. See if you can access the database by using a line similar to this:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

If you need more detailed documentation, this code comes directly from the documentation itself.

EDIT: Also, try using PDO's error checking functionality. This example creates a database connection using PDO and tries to perform a simple query. It doesn't use prepared statements or any of those features, so it's not production-ready code (i.e. *don't just throw this into your code without understanding how to improve it) and you'll need to edit it to include a SELECT query that's relevant to your database, but it should at least tell PDO to provide more information about the errors it encounters.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$dbhost     = "localhost";
$dbname     = "test";
$dbuser     = "root";
$dbpass     = "admin";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM booksa";
$q = $conn->query($sql) or die("ERROR: " . implode(":", $conn->errorInfo()));

$r = $q->fetch(PDO::FETCH_ASSOC);

print_r($r);
?>

Altri suggerimenti

Is the php file located on the same server as the mysql database, if so you might have to use 'localhost' as the first argument for mysql_connect() instead the external address.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top