2

So I'm having an issue connecting to a mySQL database I created on my local host using XAMPP. The DB itself is set up fine on 127.0.0.1, but I had to use port 8000 (instead of 80) to get it to start in XAMPP.

  $con = mysql_connect("localhost:8000","","");
 if (!$con) {
   die('Connection failed.' . mysql_error());
 }

The above doesn't work, the browser times out after a few seconds saying it cannot display the webpage (note that it doesn't give the die message). I have also tried 127.0.0.1:8000 but no luck. Creating a user in phpMyAdmin and entering the credentials didn't achieve anything either.

If I take out the port and just use:

$con = mysql_connect("localhost","","");

This connects, but I can't seem to select my database because I don't think it's connecting to the right server!

Any ideas? Thank you in advance!

1

4 Answers 4

3
  1. Do not use mysql_*it is deprecated
  2. Port 8000 is for your server not mysql. Default mysql port 3306

You better of using PDO

http://www.php.net/manual/en/pdo.construct.php

Sign up to request clarification or add additional context in comments.

Comments

1

remove port 8000 .This is not mysql port.

Comments

1

The port for MySQL is 3306. But you dont have to use that in your php code.

The reason why you cannot select your database is because you never specified that in your script. Hence, try the mysql_select_db function.

Like so:

mysql_connect('localhost','user', 'pass');
mysql_select_db('database_name');

If you plan on using this code for a bigger system, its better to use prepared statements like madara-uchiha suggested. It wont be long until these functions will print depreciation notices in your application ;)

Comments

1

You may also use mysqli

<?php $con = new mysqli("localhost", "username", "password", "dbName");
if ($con->connect_errno) {
echo "Failed to connect to MySQL: (".$con->connect_errno.")".$con->connect_error;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.