-2

I asked this question 20 minutes ago but when I inserted the code, the site takes a long time to load. I received the following error messages, 10 minutes later:

Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11  

Warning: mysql_connect(): Error while reading greeting packet. PID=2252 in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11  

Warning: mysql_connect(): MySQL server has gone away in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11  

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 11

Using the following code:

<?php
$conn_error = 'Colud not connect.';

$mysql_host = "localhost:8080";
$mysql_user = "liam";
$mysql_pass = "";

$mysql_db = 'socialnetwork';


mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die("Couldn't Connect") ;
mysql_select_db('mysql_db') or die($conn_error);

echo 'Connected!';
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user2227874
  • 31
  • 1
  • 1
  • 9
  • Are you sure 8080 is the right port for your mysql server? That's a rather unusual port for mysql. – Andrew Leap Apr 12 '13 at 21:15
  • 1
    Not sure how related it is, but your error messages indicate that you are using the deprecated `mysql_*` extension. See [this question](http://stackoverflow.com/questions/15293872/mysql-deprecated) for using the new `mysqli` or `PDO` extensions. – ajp15243 Apr 12 '13 at 21:17
  • @ajp15243 Their error messages? That could be seen by just looking at the code c.c, 1+ though. – Daedalus Apr 12 '13 at 21:20
  • @Daedalus Yeah I noticed that later, after seeing that the last two lines didn't have a `$` in front of the `mysql_`. – ajp15243 Apr 12 '13 at 21:27
  • Please do not post duplicates. They only scatter people's answers and increase how much work everyone has to do. Just edit your question or post a comment. – ameed Apr 12 '13 at 22:06

2 Answers2

6

You are using a string as a variable:

mysql_select_db('mysql_db') or die($conn_error);

Should be:

mysql_select_db($mysql_db) or die($conn_error);

Should be × 2

// make your db the current db http://php.net/manual/en/function.mysql-select-db.php
$db_selected = mysql_select_db($mysql_db);
if (!$db_selected) {
    die ("Can\'t use $mysql_db : " . mysql_error());
}

But REALLY should be (using PDO, change all your code):

    try {
        $DB= new PDO('mysql:host=localhost;dbname=socialnetwork', 'liam', '');
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
Axel A. García
  • 683
  • 9
  • 21
  • Worked but one error left Parse error: syntax error, unexpected '') or die($conn_error); ' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\ShareLife\inc\scripts\mysql_connect.inc.php on line 12 – user2227874 Apr 12 '13 at 21:17
  • 2
    You should state it's a different extension for your expansion.. because as it is, you're providing a wrong answer for what they're using currently. – Daedalus Apr 12 '13 at 21:22
2

Windows use TCP/IP which uses localhost for better performance because of the less overhead it has.

$mysql_host = "localhost";

Unix systems uses Unix Domain Socket

$mysql_host = '127.0.0.1';

But apache connects to PHP using TCP/IP or Unix Domain Socket. so either way should work.

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
  • 1
    The IP-address in your second example should be within quotes, otherwise PHP will attempt to concatenate the numbers; `$mysql_host = '127.0.0.1';` Please modify your answer via the [edit] link. Also, your answer could be improved by adding some explanation so that the OP may learn something from it. – thaJeztah Apr 12 '13 at 22:31