A quick PHP request regarding the is_resource () function

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.

+4
source share
4 answers

1) mysql_connect () will return the resource or false on error. Using is_resource () is a way to check if mysql_connect () is successful .

3) Isn't it better to use OOP to connect to the database and create some classes / instances?

If you are already familiar with OOP in other languages, I definitely recommend that you do this, according to your needs, of course. A good way to get started would be to use the PHP Framework, for example.

+1
source

1) The is_resource () function tells you if a variable is set to a resource type or not.
However, the code you have from any "PHPacademy" is no better. it uses strange things like @ , die() and $conn_error , of which the former two should not be used at all, and the latter is not defined anywhere.
It should be at least something like

 if (!mysql_connect($server, $user, $pass) || !mysql_select_db($db)) _503(); 

where _503() is a function that sends the appropriate HTTP status code and displays some predefined error page.
if you use the error handler on the site, you can simply omit this condition, leaving the functions as they are - the cleanest way:

 mysql_connect($server, $user, $pass); mysql_select_db($db); 

2) I doubt there is any benefit. $ hasDB seems to be a boolean variable that tells you if you have a DB or not. It seems completely useless to me. However, you should ask the author of this code to get a specific answer.
However, you can use the mysql_connect() return value as a variable for such a flag.

3) OOP is good only if you understand it. In this case, it is usually better.

+2
source

1 + 2) The function returns either a resource or false, so checking for one is as good as the other.

3) Only where the use of OOP makes sense.

+1
source

Keep him a simple dude. Why are you terminating your database in a session and buffering the output? As far as I know, is_resource is extraneous and redundant - you really only need to ensure that the db connection is not false (otherwise it always returns the resource). The php.net mysql_connect function works fine, and a complete example is provided:

 <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> 

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

Welcome to StackOverflow.

0
source

Source: https://habr.com/ru/post/1398444/


All Articles