Jump to content

cannot connect to mysql


jjjcr

Recommended Posts

hi guys, i'm stuck need your brilliant ideas.

 

just trying to get my hands wet on php and mysql.

 

had this code below, but i'm not getting anything. there's no error displayed, no successful connection message.

 

but using command prompt, i'm able to connect to mysql but using php code cannot.

 

any ideas why there is no message displayed using this code below, if i run the code it's just a blank page.

 

please help, thanks in advance.

 

<html>

<body>

 

 

 

<?php

 

// we connect to example.com and port 3307

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

 

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

 

// we connect to localhost at port 3307

$link = mysql_connect('127.0.0.1', 'mysql_user', 'mysql_password');

if (!$link) {

    die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

 

 

?>

 

 

</body>

</html>

 

Link to comment
Share on other sites

Also, please use the


tags when posting code, as it helps make both your post and the code a lot easier to read.

 

As for the blank page: That sounds like the classic PHP fatal error with error reporting turned off. You should always have full error reporting turned on on your development server, as it'll tell you exactly what fails and why. In a pinch you can check the error logs for the server, but it's a lot easier to just have the error reporting turned on.

 

PS: You'll also want to use the MySQLi library of functions, as the mysql_* () functions are deprecated and will be removed soon. Plus, no need to manually close the connection to the database server, PHP handles that stuff automatically.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.