2013-02-03 22 views
-1

我又回来了,试图创建一个“自定义”博客系统。或者人们称之为CMS。这是我当前的代码:如何使用MySQL检查页面是否为空?

<?php 
//include stuff here 
$pid = $_GET['pageid']; 
$data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died."); 
mysql_real_escape_string($pid); 
while($info = mysql_fetch_array($data)) 
{ 
if (!empty($info)) { 
echo $info['data']; 
} 
else { 
echo 'This page no existo.'; 
} 
} 
?> 

发生什么事是它没有显示“This page no existo。”。作为'404'文本。 可以说有人试图直接去我的网站打字,但是输入了一个错误: localhost/blog /?pageid = 10 它没有显示404文本!

我在MySQL中有一行名为“data”。它由博客文章的这个数据组成。我也有一个称为ID的行,它是一个自动增量ID系统。 “真实”,工作页面ID为1.

谢谢, RBLXDev。

编辑: 的Vardump $信息: 的vardump:

array (size=10) 
0 => string '1' (length=1) 
'id' => string '1' (length=1) 
1 => string 'Testing potatoCMS... and the title.' (length=35) 
'title' => string 'Testing potatoCMS... and the title.' (length=35) 
2 => string 'This is a test. 
This is a new line. 
This is a cookie. 
You are getting fat. 
FAT.<br />lol' (length=88) 
'data' => string 'This is a test. 
This is a new line. 
This is a cookie. 
You are getting fat. 
FAT.<br />lol' (length=88) 
3 => string '2013-02-02' (length=10) 
'date' => string '2013-02-02' (length=10) 
4 => string 'Unspecified' (length=11) 
'author' => string 'Unspecified' (length=11) 

呀,嗯...我有奇怪的占位符。

+0

尝试使用您知道存在的数组中的索引,如:!空($ info ['id']) – David

+0

这只是使pageid = 1“存在音符”。 – Raymonf

+0

也考虑[正确转义的SQL上下文](http://bobby-tables.com/),或使用更容易使用[准备语句](http://stackoverflow.com/questions/60174/how-to-prevent -sql喷射在-PHP)。 – mario

回答

1

标识尝试这样的事情....

<?php 

     $pid = $_GET['pageid']; 
     mysql_real_escape_string($pid); 

     $data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died."); 

     $num_rows = mysql_num_rows($data); 

     if ($num_rows == NULL) { 

       echo 'This page no existo.'; 

     } else { 

        $info = mysql_fetch_array($data); 
        echo $info['data']; 
     } 
?> 

没有经过测试,

已更新!

+0

回声没有。 ._。 – Raymonf

+0

它的工作!现在将代码转换为PDO。 – Raymonf

+0

更新后,我没有插入'$ info = mysql_fetch_array($ data);' – afro360

0
  1. Please, don't use mysql_* functions in new code。他们不再维护and are officially deprecated。请参阅red box?请改为了解prepared statements,并使用PDOMySQLi - this article将帮助您决定哪个。如果您选择PDO,here is a good tutorial

  2. 您必须先在您的查询中使用之前转义您的数据

  3. mysql_num_rows()是判断查询是否有结果的最佳方法。

  4. 如果您只希望查询中有一行,则不需要遍历所有结果。

  5. 如果$_GET['pageid']将永远是一个数字,你应该cast it to an integer减少的机会,SQL注入

<?php 
//include stuff here 
$pid = $_GET['pageid']; 
mysql_real_escape_string($pid); 
$data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died."); 
if (mysql_num_rows() > 0) { 

    $info = mysql_fetch_array($data); 
    echo $info['data']; 
} 
else { 
    echo 'This page no existo.'; 
} 
?> 
+1

'mysql_real_escape_string'仍然没有意义。 – deceze

+0

我的答案使用'mysql_num_rows' – afro360

+0

我知道。稍后我会将其更改为PDO。我会在几分钟内尝试。 – Raymonf

1

首先,让我们从你来到这里为了什么:

如果记录不存在,mysql_fetch_array($data)将返回false,因此,它甚至不会再进入while块。所以,你的逻辑是错误的。

其次,您正在使用mysql_real_escape_string()错误。您需要执行的SQL查询之前调用它,你需要捕捉其输出的变量,你将要注入SQL查询:

$pid = mysql_real_escape_string($pid); 
$data = mysql_query("SELECT * FROM entries WHERE id='$pid'") or die("MySQL died."); 

第三,你可能要考虑开沟mysql_*功能完全,因为该库正处于被弃用的过程中,因为它减少了SQL注入的能力。考虑使用改进的mysqli_*库函数或PDO