SQL 页面视图 PHP


SQL Page View PHP

我想在我的PHP页面上有一个页面视图,并在谷歌上搜索了这个。 我不想为它创建一个新数据库,所以我在我的数据库中找到了一个可用位置(所以不能使用任何需要在数据库中超过 1 个单元格的解决方案。

我得到了这个代码,它计算了访问量,以便该部分工作正常,但我似乎无法显示要显示的视图数(打印,我想这是正确的词)

<?php  //Adds one to the counter 
  mysql_query("UPDATE news SET post = post + 1, published=published, last_edit=last_edit WHERE id=$id");
//Retrieves the current count 
$count1 = mysql_fetch_row(mysql_query("SELECT post FROM post"));  
// Displays the count on your site print
             echo $count1; ?>

知道为什么我不会显示结果吗?

$count1是一个数组;尝试print结果以查看数组中包含的内容,如下所示:

print_r($count1);

如果您的数组包含任何内容,它将按以下方式显示:

Array(
'key' => 'value'
)

要显示基于特定key的结果,请尝试以下操作:

echo $count1['key'];

你必须替换

$count1 = mysql_fetch_row(mysql_query("SELECT post FROM post"));  

$count1 = mysql_fetch_row(mysql_query("SELECT count = count(post) FROM news"));

然后echo $count1['count'];

$count是一个array,而不是一个标量。试试echo $count[];

有一组称为debugging当事情没有按预期工作时。您迈出的第一步是通过将复杂的表达式分成简单的表达式来"分而治之":

$resource = mysql_query("SELECT count = count(post) FROM post");
if ($resource) {
    $count = mysql_fetch_row();
    if ($count > 0) {
       echo $count[0];
    } else {
       echo 'it seems that post table is empty';
    }
} else {
    echo 'I need to figure out what went wrong by looking in PHP documentation for mysql error query functions';
}

其次,调试时,不要echo使用var_dump($resource)var_dump($count)。这样,您将看到变量的实际类型和值。

附言。 mysql_query 已被弃用,可能会在任何未来的版本中删除