2

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;
        }
    }
}
?>
4

3 回答 3

0

您正在使用一些自定义 MySQL 包装器,但您可能应该使用或类似方法从查询mysql_result()中获取结果。mysql_fetch_array()

于 2013-01-27T22:50:51.397 回答
0

您最好阅读mysqli,这是执行 MySQL 查询的一种更安全的方法。你可以在这里找到优秀的文档:http: //php.net/manual/en/book.mysqli.php,这里还有一个非常有用的教程,涵盖了像你这样的简单查询:http ://www.phphaven.com /article.php?id=65

对于您的具体问题,我将使用以下内容:

$query = "SELECT `site_url`,`image_url` FROM `adverts` WHERE `zone`=1 AND `days`>0 ORDER BY RAND() LIMIT 0,1;";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // I've split these lines up to make them a little more readable.
        echo '<a href="';
        echo $row['site_url'];
        echo '">';
        echo '<img src="';
        echo $row['image_url'];
        echo '"</img></a>';
    }
}
else {
    echo 'NO RESULTS';  
}

我希望这有帮助。

于 2013-01-27T23:01:30.790 回答
0

使用您的自定义 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 方法/库。

于 2013-01-27T23:23:15.723 回答