dongzj2015 2012-06-30 13:51
浏览 167
已采纳

SQL where子句不起作用

I'm here again, with a problem. This is my code:

$sql = "SELECT * FROM paginas WHERE subpagina='0'"; 
$q = $con->prepare($sql);
$q->execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
$row = $q->fetch();

if (count($row) != "0")
{
    echo '<ul>';
    while($row = $q->fetch())
    {
       echo '<li><a href="?page='. $row['ID'] . '">' . $row['pagina'] .'</a></li>';
    }
    echo '</ul>';   
}

The problem is, all the records in the database where subpagina = 0 will be echoed on the page. But, if I use another string like 3 in the database, and in this code, it doesn't work.

I have done some thing like var_dump $row, var_dump(erroinfo()), but it only returns FALSE.

I hope you can help me.

EDIT: here is my data structure:

CREATE TABLE IF NOT EXISTS `paginas` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `pagina_link` varchar(150) NOT NULL,
  `content` varchar(10000) NOT NULL,
  `pagina` varchar(100) NOT NULL,
  `reactie` varchar(1) NOT NULL,
  `subpagina` varchar(11) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=39 ;

EDIT2: here is the data

INSERT INTO `paginas` (`ID`, `pagina_link`, `content`, `pagina`, `reactie`, `subpagina`) VALUES
(32, '?pagina=Home', '<h1>Welkom!</h1><div>Dit is een demo van het cms combora! Je mag hier op uittesten. Je kan inloggen met als gebruikersnaam demo en paswoord demo. Gelieve deze pagina niet te verwijderen.</div><div><br></div> <a href="?pagina=Home&foto=2&l=j"><img width="250px" height="250px;" src="fotos/foto2.jpg"></a>', 'Home', '1', '0'),
(33, '?pagina=test1', '<br>dslmlmqdhfkqsjdf', 'test1', '1', '0'),
(34, '?pagina=test2', '<br>', 'test2', '0', '0'),
(35, '?pagina=Nice', 'fasdfdsfadsf<br>', 'Nice', '1', '0'),
(36, '?pagina=Foto viewer ', 'Deze pagina is een test van de zelfgemaakt foto viewer:<div><br></div>
<a href=\\"?foto=1&amp;l=j\\"><img width=\\"250px\\" height=\\"250px;\\" src=\\"1.jpg\\"></a>
<a href=\\"?foto=2&amp;l=j\\"><img width=\\"250px\\" height=\\"250px;\\" src=\\"2.jpg\\"></a>
<a href=\\"?foto=3&amp;l=j\\"><img width=\\"250px\\" height=\\"250px;\\" src=\\"3.jpg\\"></a><div><br></div>', 'Foto viewer ', '1', '0'),
(38, 'sdkjfqsdfj', 'sdjmkqjfdmlkqsfdlmkqjsdflkjdlmskfjqslmdkfj', 'test56', '1', '2
');

I'm not sure if you mention this?

  • 写回答

3条回答 默认 最新

  • dpw43061 2012-06-30 13:55
    关注

    You mentioned it is a string. Try

    $sql = "SELECT * FROM paginas WHERE subpagina='0'"; 
    

    Fiddled a limited set of your table. Added some data and the above SELECT simply works. So that's not the problem. Are you sure you have other strings than '0' in the subpagina field?

    See this sqlfiddle

    Another thing you can try is

    There's no such thing as subagine = 3 in your data. Only 0 and 2 exist

    $row = $q->fetchAll(); 
    print_r($row);
    /* print_r shows Array only */
    /* loop the array and print the values */
    foreach($row as $key => $value)
    {
       echo $key + " " + $value + "
    ";
    }
    

    EDIT

    See it only just now: you do a fetch() assing it to $row and check the count. Then you start the while loop wiht anohter fetch() which will get the NEXT row of the result. The first one is skipped always because of the first fetch()

    use something like this:

    $rows = $q->fetchAll(); 
    
    echo '<ul>';
    foreach($rows as $value)
    {
       echo '<li><a href="?page='. $value['ID'] . '">' . $value['pagina'] .'</a></li>';
    }
    echo '</ul>'; 
    

    if you not want the empty <ul> tags you should check if count($rows) > 0 before you echo/foreach/echo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?