查询MySQL数据库时,我得到一个空集

时间:2018-05-23 20:12:07

标签: php mysql database pdo

我有这个问题让我最近感到疯狂,每当我尝试查询数据库时,我总是得到一个空的结果!

请帮忙,我在这里了解更多信息或详情。

这是数据库类:

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*V9ddGd@';
    public const EMAIL_TO    =   'someone@gmail.com';

    public static function database_connect()
    {
            // setting DSN (Data Source Name)
            $dsn = 'mysql:host=' . self::DB_HOST . ';' . 'charset=' . self::DB_CHARSET;

        try {

            // 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: admin@company.com" . "\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>
                    someone@gmail.com';

                }

            }

        }

    }

    private function __clone() {}
}

这就是我从索引页面查询数据库的方式:

    <?php
  $connection = Database::database_connect();
  $select_database = "use branches";
  Database::database_query($connection, $select_database);
                        $get_all_branches_query = "SELECT * FROM jazz_branches_ar WHERE branch_status = 1";
                        $query_results = Database::database_query($connection, $get_all_branches_query);
                        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>";
                        }
                        ?>

注意:

  • 我正在使用与GoDaddy的共享托管。
  • 以前添加的旧代码完美无缺。

这是我的旧数据库类,你可以看到它否定了所有kindes的安全措施:

class Database
{
    private static $_db_host = 'mysql:host=localhost;charset=utf8';
    private static $_db_user = 'jazz_god';
    private static $_db_pass = 'LcaQVNH3qz6Cksow';
    public static function database_connect() 
    {
        return $db_info = new PDO(Database::$_db_host, Database::$_db_user, Database::$_db_pass);
    }
    public static function database_query($db_connection, $query) 
    {
        $statment = $db_connection->prepare($query);
        $statment->execute();
        $result = $statment->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }
}

这是用于查询数据库的代码:

<?php
                    $connection = Database::database_connect();
                    $select_database = $connection->prepare('use jazz_agents_branches');
                    $select_database->execute();
                    $get_all_branches_query = "SELECT * FROM jazz_branches_ar WHERE branch_status = 1";
                    $query_results = Database::database_query($connection, $get_all_branches_query);
                    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>";
                    }
                    ?>

虽然我的应用程序存在风险,但我决定在下面包含我正在使用的库和php版本列表: PHP version and Libraries

1 个答案:

答案 0 :(得分:0)

就这样说:

$dsn = 'mysql:host=' . self::DB_HOST . ';' . 'charset=' . self::DB_CHARSET;

并尝试:

 $connection = database::database_connect();
 $select_database = $connection->prepare('use branches');
 $select_database->execute();

您的代码必须是:

    <?php
  $connection = Database::database_connect();
  $select_database = "use branches";
  Database::database_query($connection, $select_database);
                        $get_all_branches_query = "SELECT * FROM jazz_branches_ar WHERE branch_status = 1";
                        $query_results = Database::database_query($connection, $get_all_branches_query);
                        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>";
                        }
                        ?>

您班上的电子邮件错误会将电子邮件更改为公开,但您无法访问班级的私人财产:

public const EMAIL_TO    =   'someone@gmail.com';