Home » Php » I get an empty set when query MySQL database

I get an empty set when query MySQL database

Posted by: admin October 29, 2018 Leave a comment

Questions:

I have this issue that drives me insane lately in which whenever I try to query the database I always get an empty result!

Please help and I am here for any more info or details.

This is the database class:

final class database
{

    private const DB_HOST     =   'localhost';
    private const DB_NAME     =   'branches';
    private const DB_CHARSET  =   'utf8';
    private const DB_USER     =   'public_user';
    private const DB_PASS     =   '1y37*[email protected]';
    private const EMAIL_TO    =   '[email protected]';

    public static function database_connect()
    {
        try {

            // setting DSN (Data Source Name)
            $dsn = 'mysql:host=' . self::DB_HOST . ';' . 'dbname=' . self::DB_NAME . ';' . 'charset=' . self::DB_CHARSET;

            // creating a PDO (PHP Data Object) instance
            $pdo = new PDO($dsn, self::DB_USER, self::DB_PASS);
            $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

            return $pdo;

        } catch (PDOException $e) {

            if (DEBUG === true) {

                echo "<pre><br><var>";
                print_r('An error has occured during database connection initiation Big Boss: <br> ' . $e);
                echo "</var></pre><br>";

            } else {

                $headers  = "From: [email protected]" . "\r\n";
                $headers .= "Reply-To: No One" . "\r\n";
                $headers .= "MIME-Version: 1.0\r\n";
                $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

                if (mail(self::EMAIL_TO, 'An error has occured', $e, $headers)) {

                    echo 'المعذرة حدث خطاء فادح! لقد تم إرسال الخطاء إلى مطور الموقع لمعالجتة.';

                } else {

                    echo 'الرجاء التواصل مع مدير موقع شركة الجزيرة إخوان للصرافة على الإيميل التالي: <br>
                    [email protected]';

                }

            }

        }

    }

    private function __clone() {}
}

and this is how I query the database from the index page:

                try {

                    $connection = database::database_connect();
                    $get_all_branches_query = "SELECT * FROM jazz_branches_ar WHERE branch_status = ?";
                    $run_the_query = $connection->prepare($get_all_branches_query);
                    $run_the_query->execute([1]);
                    $query_results = $run_the_query->fetchAll();

                    foreach ($query_results as $branch) {

                        echo "<tr>";
                            echo "<td>", htmlspecialchars(strip_tags(stripslashes(trim($branch['branch_fax'])))) , "</td>";
                            echo "<td><a class=\"inline-links\" href=\"tel:", htmlspecialchars(strip_tags(stripslashes(trim($branch['branch_tel'])))), "\">", htmlspecialchars(strip_tags(stripslashes(trim($branch['branch_tel'])))) ,"</a></td>";
                            echo "<td>", htmlspecialchars(strip_tags(stripslashes(trim($branch['branch_address'])))), "</td>";
                            echo "<td>", htmlspecialchars(strip_tags(stripslashes(trim($branch['branch_city'])))), "</td>";
                        echo "</tr>";

                    }                      

                } catch (PDOException $e) {

                    if (DEBUG === true) {

                        echo 'An error has occurred during database retrieval Big Boss: ' . $e;

                    } else {

                        if (mail(database::EMAIL_TO, 'An error has occured', $e, $headers)) {

                            echo 'المعذرة حدث خطاء فادح! لقد تم إرسال الخطاء إلى مطور الموقع لمعالجتة.';

                        } else {

                            echo 'الرجاء التواصل مع مدير موقع شركة الجزيرة إخوان للصرافة على الإيميل التالي: <br>
                                [email protected]';

                        }

                    }

                }
Answers: