在盯着这些代码行,并在多个编码论坛上研究了几个小时之后,我被我的代码卡住了,它不能完全发挥作用。我正在尝试成功运行这个 PHP 脚本,以便可以从 MySQL 表中随机查询文本引用并显示在我网站的页脚中。每次用户刷新网站时,报价都应该改变。我还试图记录每个报价显示的次数,然后当我的网站上显示特定报价时,计数器会告诉站点用户该特定报价已显示多少次。到目前为止,我的代码是:
<?php
include('Includes/inc_connect.php');
$DBName = 'database';
if (!@mysql_select_db($DBName, $DBConnect))
echo "<p style='text-align:center'>There are no quotes to view!</p>";
else {
$TableName = "randomquote";
$SQLstring = "SELECT quote FROM $TableName";
//executes the query
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if ($QueryResult === false)
{
echo "<p>Unable to retrieve the data.</p>" . "<p>Error code: " . mysql_errno($DBConnect) . ": " . mysql_error($DBConnect) . "</p>";
}
else
{
$quote_array = array();//Creates a blank array
//use a while loop to extract the data from the database table into an indexed array
while(($Row = mysql_fetch_row($QueryResult)) !== FALSE)
{
$quote_array = $Row[0];
}
}
//assign the contents of the table to an array variable
$quote_count=count($quote_array);
$RandomArrayIndex = rand(0, $quote_count-1);
$quote = stripslashes($quote_array[$RandomArrayIndex]);
$SQLString = "UPDATE randomquote SET display_count " . " = display_count + 1 WHERE quote = " . $quote_array[$quote];
$SQLString = "SELECT display_count from randomquote WHERE quote = " . $quote_array[$quote];
$display_count = @mysql_query($SQLString, $DBConnect);
//display the random quote on the Web page
echo "<p style='text-align:center;font-style:italic'><strong>" . $quote . "</strong></p>\n";
echo "<p style='text-align:center>This quote has displayed " . $display_count . " times.</p>/n";
}
else
{
//specify that the comments cannot be read
echo "<p>The quote cannot be displayed at this time</p>\n";
}
else
{
//specify that there are no quotes
echo "<p>There are no quotes to display.</p>\n";
}
我是 PHP 和 MySQL 的学生,因此非常感谢任何和所有的帮助和建议。非常感谢!