douzhang3356 2016-09-28 05:50
浏览 46
已采纳

如何使用数字将mysql转换为数组结果

I have php code like this

$sql = mysql_query("SELECT userName FROM `rcpa_users` WHERE upline_users = '$selectuname'");
while($tjans = mysql_fetch_array($sql)){    

print_r($tjans);

}

But output show this

Array ( [userName] => mita ) 
Array ( [userName] => wendyang ) 
Array ( [userName] => viviekosasih )

Now, how to convert array like this result

Array
(
    [0] => mita
    [1] => wendyang
    [2] => viviekosasih
)

so I can call with echo $tjans[0], $tjans[1], $tjans[2]

Thank You..

  • 写回答

5条回答 默认 最新

  • douhuifen9942 2016-09-28 05:54
    关注

    mysql_fetch_assoc() - Fetch a result row as an associative array.This function will return a row as an associative array where the column names will be the keys storing corresponding value.

    Description: Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

    After that you need to do the array_values() so that it converts all the values to array with numeric keys.

    array_values — Return all the values of an array

    array_values() returns all the values from the array and indexes the array numerically.

    If your query returns multiple rows as output you can prefer this method:

    $sqlxx = mysql_query("SELECT userName FROM rcpa_users WHERE upline_users = '$selectuname'"); 
    $final_tjans = array();
    while($tjans = mysql_fetch_assoc($sqlxx))
    {
        $final_tjans[] = $tjans['userName'];
    }
    print_r($final_tjans);
    

    If you code returns only single row as output:

    $sqlxx = mysql_query("SELECT userName FROM rcpa_users WHERE upline_users = '$selectuname'"); 
    $tjans = mysql_fetch_assoc($sqlxx));
    $final_tjans = array_values($tjans); 
    print_r($final_tjans);
    

    Example:

    <?php
    $array = array("size" => "XL", "color" => "gold");
    print_r(array_values($array));
    ?>
    

    Output:

    Array
    (
        [0] => XL
        [1] => gold
    )
    

    Note: Usage of mysql extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

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

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多