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 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染