2

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;
?>
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
iamauser
  • 11,119
  • 5
  • 34
  • 52

2 Answers2

1

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.

  • Hi, The php file is located in another server (web-hosting server), so localhost doesn't work. I realize that web-host doesn't have the php-mysql connection, i.e. If I execute the php file via command line on the 'web-host', it fails with the following error : PHP Fatal error: Call to undefined function mysql_connect(). Can this be the reason ? – iamauser Jun 22 '12 at 16:34
  • yes, that can be the reason! most likely your hosting plan does not support the php mysql module... try to put a phpinfo(); there and see if mysqli or mysql is loaded. –  Jun 22 '12 at 18:25
  • Regardless of whether or not you can get `mysql_connect` working, you *should not be using it.* Please read the entire paragraph that I posted for the exact reasons why. Even though you may "just want to get something working" this is not an acceptable reason to use a deprecated, insecure set of functions like the `mysql_*` functions. – Ricardo Altamirano Jun 22 '12 at 18:33
1

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);
?>
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
  • Thanks for the tip. I switched to the new code you mentioned, but that doesn't solve the problem I have. I tried this simple file : It doesn't printout the $usern in the browser. – iamauser Jun 22 '12 at 17:37
  • @iamauser Does it show an error message? Can you post the PDO code that you used (you can post it by editing your question)? – Ricardo Altamirano Jun 22 '12 at 17:38
  • I am new to StackOverflow asking questions. Could you please let me know the correct formatting to write code: Here is the code I used. – iamauser Jun 22 '12 at 17:46
  • @iamauser Edit your question and put that code in, since it's difficult to read in the comments. From what I can tell, you don't define `server` (nor is it a variable name in the string, since that would be `$server`). It's easier if you click "Edit" directly underneath your question and copy the code in to a new section in your question. – Ricardo Altamirano Jun 22 '12 at 17:48
  • I did now. Please check. The servername I replaced with some dummy name when I post it here. About your question whether it returns any error, no it doesn't retrurn any error, just a blank white screen. – iamauser Jun 22 '12 at 17:53
  • I don't know what exactly happened, but the same code give me the printouts of the 'echo $usern' and connects the test db. Next, I have to change my whole code to replace mysql_* with the PDO syntax. I hope after it works, otherwise I will get back with more question. Thanks for this anyways. – iamauser Jun 22 '12 at 22:08
  • @iamauser If that code works, you should accept this answer so others can learn from it. Also, you should *definitely* replace the `mysql_*` statements in your code for reasons of security and efficiency. You can accept the answer by clicking the checkmark underneath the numbers of votes. – Ricardo Altamirano Jun 22 '12 at 22:34