0

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::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 code:

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();
 }

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

marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Jun 19, 2016 at 4:25
3
  • I couldn't understand your question, Do you want to retrieve MySQL Rows Into array or want to encode/decode(array) method? Commented Jun 19, 2016 at 4:47
  • @BilasSarker well i want to get the row in to a array and then put it in to a new array with new names Commented Jun 19, 2016 at 4:53
  • But you are doing that when you do $userData = array(.....); Commented Jun 19, 2016 at 5:10

1 Answer 1

1

I think it will help you

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); 
 }

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.

marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
answered Jun 19, 2016 at 5:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.