0

here is my code

 $db_handle = mysql_connect('127.0.0.1', 'root', '123456'); if(!$db_handle) die(mysql_error()) 

when i was debugging the code just got a page with "This page can't be display", and the a pop up message from netbean said " Socket Exception occured", can anyone help? thanks

edit new code:

$db_handle = new PDO('mysql:host=localhost;dbname=test','admin','123456'); if($db_handle) echo 'connected'; 

it works, but the mysqli didn't work either

6
  • 2
    You shouldn't use the mysql_* functionality. You should be using the PDO object, or the mysqli functions. Commented Apr 11, 2013 at 18:20
  • Also it is a VERY VERY VERY VERY VERY VERY VERY bad idea to connect as root. Commented Apr 11, 2013 at 18:21
  • is your mysql set up to use tcp connections? generally it's not and defaults to local unixdomain sockets only. Commented Apr 11, 2013 at 18:21
  • I don't think there's a problem with your code. More likely there's something wrong with the setup of you web server. Are you able to run a php file with this code? phpinfo(); Commented Apr 11, 2013 at 18:22
  • @Jules yes, i can run phpinfo() Commented Apr 11, 2013 at 18:23

2 Answers 2

1

Please stop using mysql_query for new project, use mysqli or PDO

Here a sample:

mysqli = new mysqli($host, $user, $dbpassword, $database); if (mysqli_connect_errno()) { printf("Connection failed: %s\n", mysqli_connect_error()); exit(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

I'd -1 for saying the db name is missing. it's an OPTIONAL parameter. but pushing for mysqli/pdo makes up for it.
I tried your code too got the same error. Is it possible that the code is fine because the page even didn't show the connection error, maybe some configuration was wrong? I check the mysql workbench, the connection method is standard(TCP/IP)
Are you sure port is set, database is running and there is no firewall rule block connection somewhow?
@Fabio yes, I checked the mysql, and turned off the firewall, the problem is still there, getting crazy
0

$db_handle = new PDO('mysql:host=localhost;dbname=test','admin','123456'); if($db_handle) echo 'connected';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.