1

I am relearning PHP from the ground up and I would really like to COMPLETELY understand the language. I know how to use the mysql_connect function. I would like to know the why's and how's of it more thoroughly though. My question is as follows:

$connection = mysql_connect("localhost", "root", "");
if (!$connection) {
    die("Connection failed: " . mysql_error());
}

Line 1 of this function, if I am not mistaken, returns a value of TRUE or FALSE. TRUE if a connection was successful or FALSE if it was not. Is the return value of this function stored in the variable $connection (If so, and the connection is successful, is the value TRUE assigned to the variable) or is the database login credentials stored in the variable $connection? Hows does that work. Thanks!

user2833068
  • 139
  • 7
  • 13
  • If you are learning PHP or re-learning PHP; you should look into PDO or MySQLi. – Ryan Oct 16 '13 at 17:27
  • I'll say i'm learning PHP. Like I asked the guy below may I ask why you recommend PDO or MySQLi? I'm following along on Lynda.com right now for their Essentials Course on PHP and MySQL. – user2833068 Oct 16 '13 at 17:35
  • 1
    @user2833068 - the mysql_ functions are in the process of being removed from PHP, so code that's written using them will stop working at some point in the future. Both PDO and mysqli_* also make it easier for you to write code that's safer and more efficient. – andrewsi Oct 16 '13 at 17:46
  • You can read the answer to this question in the PHP manual. It's not a good question for SO. – markus Oct 16 '13 at 17:59
  • How long will it be before these mysql_functions are removed? This is depressing. I've spent 3 weeks on it already. Lost... All lost.. =/ – user2833068 Oct 16 '13 at 18:17

2 Answers2

2

Quoting the docs http://php.net/manual/en/function.mysql-connect.php

Returns a MySQL link identifier on success or FALSE on failure.

So its a resource when the connection is a success, a boolean when its a failure. Its both ;)

The credentials are not stored in the return values. Its a link to identify the MySQL connection.

Gopi Krishna
  • 486
  • 2
  • 6
  • Thanks for your answer. All the answers were right but yours also explained to me why it was the right answer and what the right answer meant so I'll accept yours. Thanks a bunch. =) – user2833068 Oct 16 '13 at 17:29
0

From the manual (best place to look): http://us1.php.net/mysql_connect

Returns a MySQL link identifier on success or FALSE on failure.

Also, move to mysqli or PDO, now...

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • I keep hearing people tell me to move to mysqli or PDO. No one ever tells me why. Would you care to elaborate please? I would love to know. – user2833068 Oct 16 '13 at 17:28
  • 1. mysql is deprecated. No support in the future. 2. Does'nt protect against SQL injection. Use PDO prepared statements for this.(not exhaustive protection though) See this http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php for a better answer – Gopi Krishna Oct 16 '13 at 17:45
  • How long will it be before these mysql_functions are removed? This is depressing. I've spent 3 weeks on it already. Lost... All lost.. =/ – user2833068 Oct 16 '13 at 18:17