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

存在数据库条目,但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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分