1

My table's name is userdetails, it has four attributes named name, username, mobile and password. I want to get all the mobile numbers and store it in an array using php. I have used the following php code

if($_SERVER['REQUEST_METHOD']=='GET'){
require_once('dbConnect.php');
$mobile  = $_GET['mobile'];
$sql = "SELECT MOBILE FROM USERDETAILS";

 $r = mysqli_query($con,$sql);

 $res = mysqli_fetch_array($r);

 $result = array();

 array_push($result,array(
 "MOBILE"=>$res['MOBILE']
 )
);

echo json_encode(array("result"=>$result));

mysqli_close($con);

  }

but all I am getting is the first entry of the database. Please help.

0

3 Answers 3

1

You should loop through the records by doing the following:

$result = [];
while ($array = mysqli_fetch_array($r)) {
    $result[] = $array['MOBILE'];
}
echo json_encode($result);
Sign up to request clarification or add additional context in comments.

Comments

0

For getting all number of column use while loop as

 $jsonData = array();// initialized you array
    while ($array = mysqli_fetch_array($r,MYSQLI_ASSOC)) {// add MYSQLI_ASSOC to get associative array
        $jsonData[] = $res['MOBILE'];// store data into array
    }
    echo json_encode($jsonData);// convert in into json

Comments

0

Try this:

$result = mysqli_query($con,$sql);
$mob = Array();
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $mob[] =  $row['MOBILE'];  
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.