dongshang1768 2018-01-29 11:04
浏览 46
已采纳

存在数据库条目,但search_result返回false

<?php
require 'database.php';

//if (!empty($_POST['search'])):

$sql1 = "SELECT * FROM cookies2 WHERE  cookie LIKE '%".$_POST['search']."%'";

$request1 = $conn->prepare($sql1);
$request1->bindparam(':search', $_POST['search']);

$request1->execute();
$result1 = $request1->fetch(PDO::FETCH_ASSOC);
var_dump($request1);
$ff = $result1['cookie'];
//endif;
?>
<html>


<meta charset="utf-8" /> 
<body>


<form action="comments.php" method="post"/>
<input type="text" name="search" placeholder="search for user comments"/>
<input type="submit"/>

    <?php
    if(!empty($request1)):
    while($search_result = $request1->fetch(PDO::FETCH_ASSOC)){ 
    var_dump($search_result);?>

    <h1>baaaaaa</h1>
    <p><?= $search_result['cookie']?></p>
    <h3><?= $search_result['comment']?></h3><?php }


 endif;
?>



</body>
</html>

So I am writing script for my cookie app. And This script works just fine in another page.php to search for cookies but I been stuck 2 days and loop wont start and search_result return false most of the times. Is my bindparam wrong or have I missed some basic indenting? I want to have this table for user comments on specific cookies. to search for. I am writing this in danger get banned my last question probably. I like stack overflow but its not for beginners I guess. All feedback is welcome I don't know what is wrong when almost identic script worked before this. regards. don

ps baaaa is showing sometimes and the loop don't show and even though I have ie bak in table nothing shows, and I have been looking for similar posts but nothing is as my problem.

  • 写回答

2条回答 默认 最新

  • duanpie4763 2018-01-29 11:11
    关注

    Change your query statements like this:

    $sql1 = "SELECT * FROM cookies2 WHERE  cookie LIKE :search"; // Changed line no.1
    
    $request1 = $conn->prepare($sql1);
    $request1->bindparam(':search', "%".$_POST['search']."%", PDO::PARAM_STR); // Changed line no.2
    
    $request1->execute();
    $result1 = $request1->fetch(PDO::FETCH_ASSOC);
    var_dump($request1);
    

    For more details: See

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

报告相同问题?

悬赏问题

  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏