douxiong3234 2015-04-10 10:32
浏览 24
已采纳

如何将mysql数组转换为php数组

        $cg= $mysqli->query("SELECT * FROM news WHERE id='$postid' 
        and  status='1' ORDER BY RAND()");
       while($sd= $cg->fetch_assoc()){
       $title= $sd['title'];

        }

What i want to do is turn var $title to something like

     $mytitle[1] = 'first title';

     $mytitle[2] = 'second title';

     $mytitle[3] = 'third title';
      $randnews= rand(1, 3);
      echo $mytitle[$randnews];
  • 写回答

5条回答 默认 最新

  • douduocuima61392 2015-04-10 10:50
    关注

    Try this,I have tried based on my database table query, It's working as you expected.

    Code:-

    <?php
         require_once("connect.php");
         $sql=("SELECT * FROM news WHERE id='$postid' and status='1' ORDER BY RAND()");
         $result = $con->query($sql); 
         while($row = $result->fetch_assoc()){
          $array[] = $row['title'];      
          }
         echo "After Sorting".'<pre>';
         print_r($array);
         echo '</pre>';     
    ?>
    

    $con is variable of connection class.

    $con=mysqli_connect($servername,$username,$password,$my_db);

    Output:-

    [xyz] => "test"
    [abc] => "tester"
    [pqr] => "user"
    [res] => "test"
    

    Hope this helps

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了