Andrew Aubury Andrew Aubury - 5 months ago 12
MySQL Question

PHP Retrieve MySQL Rows Into array

Hello i am trying to get results from a MySQL query and then return it as a array but then on top of that i want the other function to "decode" it in to a custom array

GetProfileData Code:

public function getProfileData($username){
$data = TBWebcam::MySQLQurey("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;
}
}


MySQLQurey Code:

public function MySQLQurey($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();
}


I am getting the MySQL array the issue is in the getProfileData function

Answer

I think it will help you

public function getProfileData($username){
        $results = TBWebcam::MySQLQurey("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);  
    }

The code of $mehArray[] = $row; of the method MySQLQuery() sending two dimensional result set. So you have to fetch the results by looping in the method getProfileData(). I have put here 0 index value, because of you want to take 1 rows, I think.

Comments