0

Right now all the columns from the MySQL result are converted to JSON and printed out on the screen. I would like to print only two column $row['name'] and $row['gender'] to the screen. Any ideas guys

include('connect-db.php');

$result = mysql_query("SELECT * FROM patientvaccinedetail")


while($row = mysql_fetch_assoc( $result)) {

print_r(json_encode($row));}
1
  • 1
    You are using mysql_* functions which are now deprecated and will be removed from PHP in the future.So you need to start using MySQLi or PDO instead. Commented Jun 30, 2017 at 4:54

4 Answers 4

2
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail")
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, just select those columns you need. Don't use * which is "give me everything just in case..." AND Can we please LOOSE the mysql and use mysqli or pdo or something...
The first is i want all the column for the display, but i just want two column to covert to JSON format to send to the SMS API
1

Here is my solution for getting all from the table and display only two values....

include('connect-db.php');    
$result = mysql_query("SELECT * FROM patientvaccinedetail") //getting all the fields    

while($row = mysql_fetch_assoc( $result)) {
    $data = array('name' => $row['name'] , 'gender' => $row['gender']); //get the two values into one array

print_r(json_encode($data)); //printing the name and gender
}

If you want to get only that two fields here is another query...

include('connect-db.php');    
$result = mysql_query("SELECT name,gender FROM patientvaccinedetail") //getting two the fields    

while($row = mysql_fetch_assoc( $result)) {     

print_r(json_encode($row)); //printing the name and gender
}

3 Comments

May i ask one more thing, for the message sent it's Chinese, so right now it cant display correctly but some random character i.e.B\u578b\u809d\u708e\u75ab\
in the heading, i've set to utf-8
Open new question Because we need explanation @tam
0

If you want all data but you have to print only name & gender then ..Create one separate array & allocate name & gender...

$data_array = array();
while($row = mysql_fetch_assoc( $result)) {
 array_push($data_array,array("name"=>$row['name'],"gender"=>$row['gender']);
}
print_r(json_encode($data_array));

Comments

0
include('connect-db.php');

$result = mysql_query("SELECT name,gender FROM patientvaccinedetail")


while($row = mysql_fetch_assoc( $result)) {

print_r(json_encode($row));}

1 Comment

Please try to avoid just dumping code as an answer and try to explain what it does and why. Your code might not be obvious for people who do not have the relevant coding experience. Please edit your answer to include clarification, context and try to mention any limitations, assumptions or simplifications in your answer.

Your Answer

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.