0

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

4

1 Answer 1

2

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());
Sign up to request clarification or add additional context in comments.

2 Comments

To elaborate, the single quotes around 'first_name' and 'mobile_phone' makes mysql return them as strings, rather than the values in the columns with those names. (You should use backticks for that)
@user3169485 : did you try this? if it worked then accept an 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.