php - 如何在 PHP/HTML 中显示 MySQL select 语句

标签 php html

MySQL 选择在 PHP/HTML 中显示不正确

这是我的代码:

<?php
session_start();
require_once("database.php");
require_once("MySQL_connection.php");

/* Database connection */
$db = new MySQLConnection($config['sql_host'], $config['sql_username'], $config['sql_password'], $config['sql_database']);
$db->Connect();

unset($config['sql_password']);

/* Cron */
require_once("cron.php");

/* Display Advert */

$ad_link = $db->Query("SELECT `site_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;");

$img_link = $db->Query("SELECT `image_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;");

?>

<!DOCTYPE html>
<html>
<body>
<a href="<?php echo $ad_link ?>"><img src="<? echo $img_link ?>"></a>
</body>
</html>

由于某种原因显示为:

<html><head></head><body>
<a href="Resource id #6"><img src="Resource id #7"></a>

</body></html>

有人知道哪里出了问题吗?

忘记添加 MYSQL_connection.php 的代码,以下代码是该文件中用于连接到数据库的所有内容。

<?php


class MySQLConnection {

    private $sqlHost;
    private $sqlUser;
    private $sqlPassword;
    private $sqlDatabase;

    private $mySqlLinkIdentifier = FALSE;

    public $QueryFetchArrayTemp = array();

    private $numQueries = 0;

    public $UsedTime = 0;

    public function __construct($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase = FALSE) {
        $this->sqlHost = $sqlHost;
        $this->sqlUser = $sqlUser;
        $this->sqlPassword = $sqlPassword;
        $this->sqlDatabase = $sqlDatabase;
    }

    public function __destruct() {
        $this->Close();
    }

    public function Connect() {
        if($this->mySqlLinkIdentifier !== FALSE) {
            return $this->mySqlLinkIdentifier;
        }

        $this->mySqlLinkIdentifier = mysql_connect($this->sqlHost, $this->sqlUser, $this->sqlPassword, TRUE); // Open new link on every call
        if($this->mySqlLinkIdentifier === FALSE) {
            return FALSE;
        }

        if($this->sqlDatabase !== FALSE) {
            mysql_select_db($this->sqlDatabase, $this->mySqlLinkIdentifier);
        }

        return $this->mySqlLinkIdentifier;
    }

    public function Close() {
        if($this->mySqlLinkIdentifier !== FALSE) {
            mysql_close($this->mySqlLinkIdentifier);
            $this->mySqlLinkIdentifier = FALSE;
        }
    }

    public function GetLinkIdentifier() {
        return $this->mySqlLinkIdentifier;
    }       

    public function Query($query) {
        $start = microtime(true);
        $result = mysql_query($query, $this->GetLinkIdentifier());
        $this->UsedTime += microtime(true) - $start;
        $this->numQueries++;

        if( $result === false ){
            die($this->GetErrorMessage());
        }

        return $result;
    }

    public function FreeResult($result) {
        mysql_free_result($result);
    }

    public function FetchArray($result) {
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function FetchArrayAll($result){
        $retval = array();
        if($this->GetNumRows($result)) {
            while($row = $this->FetchArray($result)) {
                $retval[] = $row;
            }           
        }
        return $retval;
    }   

    public function GetNumRows($result) {
        return mysql_num_rows($result);
    }

    public function GetNumAffectedRows() {
        return mysql_affected_rows($this->mySqlLinkIdentifier);
    }


    // Helper methods
    public function QueryFetchArrayAll($query) {
        $result = $this->Query($query);
        if($result === FALSE) {
            return FALSE;
        }

        $retval = $this->FetchArrayAll($result);
        $this->FreeResult($result);

        return $retval;         
    }

    public function QueryFirstRow($query) {
        $result = $this->Query($query);
        if($result === FALSE) {
            return FALSE;
        }

        $retval = FALSE;

        $row = $this->FetchArray($result);
        if($row !== FALSE) {
            $retval = $row;
        }

        $this->FreeResult($result);

        return $retval;     
    }

    public function QueryFirstValue($query) {
        $row = $this->QueryFirstRow($query);
        if($row === FALSE) {
            return FALSE;
        }

        return $row[0];         
    }

    public function GetErrorMessage() {
        return "SQL Error: ".mysql_error().": ";
    }

    public function EscapeString($string) {
        if (is_array($string))
        {
            $str = array();
            foreach ($string as $key => $value)
            {
                $str[$key] = $this->EscapeString($value);
            }

            return $str;
        }

        return get_magic_quotes_gpc() ? $string : mysql_real_escape_string($string, $this->mySqlLinkIdentifier);
    }

    function GetNumberOfQueries() {
        return $this->numQueries;
    }

    public function BeginTransaction() {
        $this->Query("SET AUTOCOMMIT=0");
        $this->Query("BEGIN");
    }

    public function CommitTransaction() {
        $this->Query("COMMIT");
        $this->Query("SET AUTOCOMMIT=1");
    }

    public function RollbackTransaction() {
        $this->Query("ROLLBACK");
        $this->Query("SET AUTOCOMMIT=1");
    }

    public function GetFoundRows() {
        return $this->QueryFirstValue("SELECT FOUND_ROWS()");
    }

    public function GetLastInsertId() {
        return $this->QueryFirstValue("SELECT LAST_INSERT_ID()");           
    }


    public function QueryFetchArray($query, $all = false, $useCache = true)
    {
        $tempKey = sha1($query . ($all === true ? 'all' : 'notAll'));
        $temp = $this->QueryFetchArrayTemp[$tempKey];

        if ($temp && $useCache === true)
        {
            return unserialize($temp);
        }
        else
        {
            $queryResult = $this->Query($query);
            $result = $all === true ? $this->FetchArrayAll($queryResult) : $this->FetchArray($queryResult);

            $this->QueryFetchArrayTemp[$tempKey] = serialize($result);
            return $result;
        }
    }
}
?>

最佳答案

使用您自定义的 MySQL 类,这可能会起作用:

$result = $db->Query("SELECT `site_url`,`image_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;");

while($row = $db->FetchArray($result)) {
    echo '<a href="' . $row['site_url'] . '"><img src="' . $row['image_url'] . '"></a>';
}

$db->FreeResult($result);

但是,正如其他人指出的那样,您的自定义 MySQL 类中使用的代码已被弃用,应该更新以使用更新的 php 方法/库。

关于php - 如何在 PHP/HTML 中显示 MySQL select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553232/

相关文章:

jquery - 选择下拉菜单的 - 目标默认标题

html - flexbox 模型不产生父容器的 100% 高度

html - 我怎样才能让 2 个 <div> 居中?

html - DT 和 DD 的 CSS 等高

php - 如何链接到数据库页面

php - 使用 PHP 调整扭曲图像大小

php - 时间戳 Mysql Date_From 作为默认值

html - 如何使用媒体查询对 HTML 重新排序?

php - 如何使用 PHP 的 preg_replace 函数将 Unicode 代码点转换为实际字符/HTML 实体?

php - PHP 应用中的未读消息计数