I am trying to learn and study in PHP. I have no programming experience at all. I just finished a series of videos from the 200 series on Youtube made by PHPAcademy, and I get an idea of ββwhat you need to think like a programmer.
I am reading another tutorial that contains all the source code and instructions for creating a complex login system with front and back end settings. I just started, but I'm already a little confused. I need to create a PHP script called "database.php" that connects to the database.
The code written in the source code is as follows:
<?php session_start(); ob_start(); $hasDB = false; $server = 'localhost'; $user = 'root'; $pass = ''; $db = 'acl_test'; $link = mysql_connect($server,$user,$pass); if (!is_resource($link)) { $hasDB = false; die("Could not connect to the MySQL server at localhost."); } else { $hasDB = true; mysql_select_db($db); } ?>
MY QUESTION: 1) I have never studied the is_resource function. Therefore, I looked through it, but the definition does not make any sense to me. Is_resource just checks to see if you returned something? I do not understand why this is necessary, when you can do it as follows: (as I learned from PHPacademy)
<?php $conn_error = 'Could not connect.'; $server = 'localhost'; $user = 'root'; $pass = ''; $db = 'tutorial'; if ( !@mysql _connect($server, $user, $pass) || !@mysql _select_db($db)) { die($conn_error); } ?>
2) What is the use or meaning of using the "is_resource ()" function, and not just testing mysql_connect () directly? And what does $ hasDB mean? In the above example, IF tests if is_resource ($ link) is NOT true, then it is omitted and reads $ hasDB = false, and then goes to die () and kills the page. Why is $ hasDB even necessary at this point?
3) Isn't it better to use OOP to connect to the database and create some classes / instances?
I ask because, although I can probably write it the way I found out, I see and observe new and different ways to do the same, and I want to study well from the start, a pure coding habit. Sorry if this is such a newbie, but you need to start something. Sometimes the online definition of functions does not show real consequences, especially for future encoding.
ANY help is appreciated. Thanks.