简体   繁体   中英

Converting string to json object or array in php

I have to make a web service. Therefore I referred some tutorials in the internet and came up with the following codes

index.php

<html>
<head>
    <title>Form page</title>
</head>

<body>

    <form action="http://localhost:81/my%20web%20service/webservice" method="get">
        Table name:<br>
        <input type="text" name="s" value=""><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

webservice.php

<?php

    include('connectdb.php');

    $something = $_GET['s'];
    $sqlcode = mysql_query("Select * from $something");

    $jsonObj= array();
    while($result=mysql_fetch_object($sqlcode))
    {
        $jsonObj[] = $result;
    }

    $final_res =json_encode($jsonObj) ;
    echo $final_res;
?>

connectdb.php

<?php
    $hostname="localhost";
    $username="root"; //write your username
    $password=""; //write your password
    $db_name="webservice_trial"; //write your db name
    $con=mysql_connect($hostname,$username,$password);
    mysql_select_db($db_name,$con) or die ("Cannot connect the Database");
    mysql_query("SET NAMES 'utf8'",$con); 

?>

The above codes works fine. When I enter a name of a table from form.php, it will retrieve all tuples in that particular table.

Now what I want to do is to make data display on another page. ie I want to transfer data from webservice.php to another page from json format. so I edited my webservice.php as following

webservice.php

<?php

    include('connectdb.php');

    $something = $_GET['s'];
    $sqlcode = mysql_query("Select * from $something");

    $jsonObj= array();
    while($result=mysql_fetch_object($sqlcode))
    {
        $jsonObj[] = $result;
    }

    $final_res =json_encode($jsonObj) ;
    echo $final_res;

    $jsonArray = (array) json_decode($final_res);
    echo $jsonArray[0];

?>

it gives the following error

[{"name":"hilton","town":"colombo","telephone":"774933705","description":"excellent"},{"name":"galadari","town":"colombo","telephone":"112894143","description":"best"},{"name":"mt. lavinia","town":"mt. lavinia","telephone":"773580324","description":"good"}]

Catchable fatal error: Object of class stdClass could not be converted to string in C:\\xampp\\htdocs\\json_folder\\my web service\\webservice.php on line 18

You can either change

$jsonArray = json_decode($final_res);

to

$jsonArray = json_decode($final_res, True);  

or, to access the name of the first item use

print $jsonArray[0]->name;

Instead of trying to typecast the decoded string to an array, use this, which is the right way to do it.

$jsonArray = json_decode($final_res, TRUE);

Now, you will have an array, which you can echo or var_dump .

var_dump($jsonArray);

As in your question, what your error says is that, you are trying to echo it as a variable, where what you are trying to echo is actually an object class. Probably because it isnt being typecasted.

Advices:

  • Please stop using mysql functions. Its deprecated. Use mysqli or PDO .

  • Read about SQL Injections. Never trust userinputs. Always sanitize them or use prepared statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM