MrD
MrD

Reputation: 95

Php outputing Json encoded data from mysql database

I am just having an error in my output through my php file. I believe it is an easy fix. While it should be displaying data it is displaying this.

[{"first_name":"first_name","mobile_phone":"mobile_phone"},`

and here is my php file. Really hope this is a simple fix. Thanks

   <?php

    function connect() {


        // Connecting to mysql database
        $con = mysql_connect(private);

        // Selecing database
        $db = mysql_select_db("intraweb_db") or die(mysql_error()) or die(mysql_error());

        // returing connection cursor
        return $con;
    }




//end of connect to db
connect();

$sql=mysql_query("SELECT 'first_name', 'mobile_phone' FROM `admin_contacts`");

$output = array();
while($r = mysql_fetch_assoc($sql)){
    $output[]=$r;

}
echo json_encode($output);

?>

Awesome ;) thank you guys. got it working

Upvotes: 0

Views: 98

Answers (1)

Awlad Liton
Awlad Liton

Reputation: 9351

You are doing wrong in your query. don't use single or double quote in your column name.

Try this:

$sql=mysql_query("SELECT first_name,mobile_phone FROM `admin_contacts`");
$output = array();
while($r = mysql_fetch_assoc($sql)){
    $output[]=$r;    
}
echo json_encode($output);

?>

You also need to change in this line:

$db = mysql_select_db("intraweb_db") or die(mysql_error()) or die(mysql_error());

to

$db = mysql_select_db("intraweb_db") or die(mysql_error());

Upvotes: 2

Related Questions