PHP将MySQL行检索到数组中

时间:2016-06-19 04:25:27

标签: php mysql arrays

我正在尝试从MySQL查询中获取结果然后将其作为数组返回但最重要的是我希望其他函数将其“解码”为自定义数组

GetProfileData代码:

public function getProfileData($username){
        $data = TBWebcam::MySQLQuery("SELECT * FROM `new_user` WHERE `user_name` = \"AndrewAubury\";");
        if($data =! null){
            $userimage = $data["user_image"];
            if($userimage =! ""){
                $userimage = str_replace("%s","",$userimage);
                $userimage = "https://A LINK YOU DONT NEED TO KNOW.net/PF.Base/file/pic/user/".$userimage;
            }else{
                $userimage = null;
            }

            $usergender = $data["gender"];
            if($usergender == "1"){
                $usergender = "Male";
            }else{
                $usergender = "Female";
            }
            //echo($data["user_id"]."<br><br><br><br>");
            $userData = array(
               "id" => $data["user_id"],
               "name" => $data["full_name"],
               "username" => $data["user_name"],
               "image" => $userimage,
               "gender" => $usergender,
            );
            echo("Data: ".$data."<br><br><br>");
            return($userData);
        }else{
            return null;
        }
    }

MySQLQuery代码:

public function MySQLQuery($queryToDo){
        $servername = "my server";
        $username = "um my username :P";
        $password = "why do u need to know";
        $dbname = "the one i set it up on";

        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {return(null);}

        $sql = $queryToDo;
        $result = $conn->query($sql);
$mehArray = array();
        if ($result->num_rows > 0) {
         while($row = $result->fetch_assoc()) {
        $mehArray[] = $row;
    }
    return($mehArray);
        } else {
            return(null);
        }
        $conn->close();
    }

我得到的MySQL数组问题出在getProfileData函数

1 个答案:

答案 0 :(得分:1)

我认为它会对你有所帮助

public function getProfileData($username){
        $results = TBWebcam::MySQLQuery("SELECT * FROM `new_user` WHERE `user_name` = \"AndrewAubury\";");        
        if($results == null){
            return null;
        }       

        $data = $results[0];//getting only the first result, if you have more rows the you have use for/foreach loop 
        $userimage = $data["user_image"];
        if($userimage =! ""){
            $userimage = str_replace("%s","",$userimage);
            $userimage = "https://A LINK YOU DONT NEED TO KNOW.net/PF.Base/file/pic/user/".$userimage;
        }else{
            $userimage = null;
        }

        $usergender = $data["gender"];
        if($usergender == "1"){
            $usergender = "Male";
        }else{
            $usergender = "Female";
        }
        //echo($data["user_id"]."<br><br><br><br>");
        $userData = array(
           "id" => $data["user_id"],
           "name" => $data["full_name"],
           "username" => $data["user_name"],
           "image" => $userimage,
           "gender" => $usergender,
        );                  

        echo("Data: ".$data."<br><br><br>");
        return($userData);  
    }

$ mehArray [] = $ row的代码; MySQLQuery()发送二维结果集的方法。因此,您必须通过循环getProfileData()方法来获取结果。我在这里放了0个索引值,因为你想要占用1行,我想。