PHP echo request result in class?

I have a question about the PHP class. I am trying to get the result from Mysql via PHP. I would like to know if it is better to display the result inside the class or save the result and process it in html.

For example, the result of displaying inside a class

class Schedule {
           public $currentWeek;

            function teamQuery($currentWeek){

            $this->currentWeek=$currentWeek;


            }
            function getSchedule(){
                $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
                    if (!$connection) {
                        die("Database connection failed: " . mysql_error());
                    }

                    $db_select = mysql_select_db(DB_NAME,$connection);
                    if (!$db_select) {
                        die("Database selection failed: " . mysql_error());
                    }

                  $scheduleQuery=mysql_query("SELECT guest, home, time, winner, pickEnable FROM $this->currentWeek ORDER BY time", $connection);

                    if (!$scheduleQuery){
                        die("database has errors: ".mysql_error());
                          }


                    while($row=mysql_fetch_array($scheduleQuery, MYSQL_NUMS)){
                    //display the result..ex: echo $row['winner'];

                    }     
                    mysql_close($scheduleQuery); 

                    //no returns
                    }


        }

Or return the result of the request as a variable and descriptor in php

class Schedule {
           public $currentWeek;

            function teamQuery($currentWeek){

            $this->currentWeek=$currentWeek;


            }
            function getSchedule(){
                $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
                    if (!$connection) {
                        die("Database connection failed: " . mysql_error());
                    }

                    $db_select = mysql_select_db(DB_NAME,$connection);
                    if (!$db_select) {
                        die("Database selection failed: " . mysql_error());
                    }

                  $scheduleQuery=mysql_query("SELECT guest, home, time, winner, pickEnable FROM $this->currentWeek ORDER BY time", $connection);

                    if (!$scheduleQuery){
                        die("database has errors: ".mysql_error());
                    // create an array    }
                    $ret = array();   

                    while($row=mysql_fetch_array($scheduleQuery, MYSQL_NUMS)){

                    $ret[]=$row;
                    }     
                    mysql_close($scheduleQuery); 

                    return $ret;  // and handle the return value in php
                    }


            }

Two things here:

  • I found that the return variable in php is a bit complicated for the game, as it is a two-dimensional array. I am not sure what the best practice is, and would like to ask you for expert opinions.

  • Every time I create a new method, I need to recreate the $ connection variable: see below

    $connection = mysql_connect (DB_SERVER, DB_USER, DB_PASS);                       if (! $connection) {                           die ( " :". mysql_error());                       }

                        $db_select = mysql_select_db(DB_NAME,$connection);
                        if (!$db_select) {
                            die("Database selection failed: " . mysql_error());
                        }
    

. , , , ? php-. , , , . .

+3
2

"", . , PHP-, , , . , , . , , . , .

. . . , . , , . .

PHP-, - . , , , .

+2

, , , else .

mebmer :

protected $_connection = null;

. db .

, , mysql. Mysqli PDO_Mysql. - . , .

+1

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


All Articles