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条)

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决