douchai7891 2014-05-30 19:28
浏览 66
已采纳

使用mysqli预处理语句选择distinct?

I am trying to select distinct from mysql database using mysqli prepared statements.

however, when I run my code, I get no error at all but I cannot get the results from the mysql database either!

this is my mysqli prepared statement:

$stmt = $db_conx->prepare("SELECT DISTINCT `title`, `url` FROM `$Pages`");
echo $storePages;
$stmt->execute();
$stmt->bind_result($title, $url);
$stmt->store_result();
while($line=$stmt->fetch()){
    $tvalue[] = $line;
     }

but This code works and i do not want to use it as it is not mysqli prepared statement:

$sql = "SELECT DISTINCT title, url FROM $Pages";
$query = mysqli_query($db_conx, $sql);
    while($line = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        $tvalue[] = $line;
    }

could someone please help me out with this?

Thanks

  • 写回答

2条回答 默认 最新

  • dqba94394 2014-05-30 20:47
    关注

    $stmt->fetch() does not return the row fetched, it only returns true, false, or null. Please read the documentation.

    The results are stored in the variables you told it to use in $stmt->bind_result().

    $stmt->execute();
    $stmt->bind_result($title, $url);
    $stmt->store_result();
    while($stmt->fetch()) {
        $tvalue[] = array('title'=>$title, 'url'=>$url);
    }
    

    If you have mysqlnd installed (which should be enabled by default since PHP 5.4), it would be simpler for your case to fetch_all(). That way you don't need bind_result(), nor do you need to loop over rows:

    $stmt->execute();
    $result = $stmt->get_result();
    $tvalue = $result->fetch_all();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器