Echo json_encode returns empty string

I have a php script that runs a simple request SELECT

and it seems to work fine.

Here's the script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

try {
    $hostname = "localhost";
    $username = "root";
    $password = "";

    $db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC;charset=utf8",$username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if (!empty($_POST["searchword"])) {
        $searchword = $_POST["searchword"];
        $query = $db->prepare("SELECT title, posted, author_id, extract FROM articles WHERE title LIKE :searchword OR extract LIKE :searchword OR body LIKE :searchword");      
        $query->execute(array(":searchword" => "%" . $searchword . "%"));
        $result = $query->fetchAll();
        //turns timestamp into integer
        for($i = 0; $i < count($result); ++$i) {
          $result[$i]['posted'] = strtotime($result[$i]['posted']);
        }
        echo json_encode($result);
        die();
    } 
    else if (!empty($_POST["title"])) {
        $title = $_POST["title"];
        $query = $db->prepare("SELECT title, posted, author_id, body FROM articles WHERE title = :title");
        $query->execute(array(":title" => $title));
        $result = $query->fetchAll();
        $result[0]['posted'] = strtotime($result[0]['posted']);
        echo json_encode($result);
        die();
    }
    else {
        $query = $db->prepare('SELECT * FROM articles');
        $query->execute();
        $result = $query->fetchAll();
        //turns timestamp into integer
        for($i = 0; $i < count($result); ++$i) {
          $result[$i]['posted'] = strtotime($result[$i]['posted']);
        }
        echo json_encode($result);
        die();
    }
} 
catch (PDOException $e) {
    echo "Error!: " . $e->getMessage() . "<br/>";
    die();
}

      

Now var_dump shows fine, this is what it looks like:

array(1) {
  [0]=>
  array(12) {
    ["id"]=>
    string(1) "4"
    [0]=>
    string(1) "4"
    ["title"]=>
    string(22) "An introduction to ptc"
    [1]=>
    string(22) "An introduction to ptc"
    ["posted"]=>
    int(1434119848)
    [2]=>
    string(19) "2015-06-12 16:37:28"
    ["author_id"]=>
    string(1) "2"
    [3]=>
    string(1) "2"
    ["extract"]=>
    string(76) "What are they? How do they work? Is there money to be made? Risk assesement."
    [4]=>
    string(76) "What are they? How do they work? Is there money to be made? Risk assesement."
    ["body"]=>
    string(3542) "What is a ptc?

In pure basis, it advertizing. Sites that specialize in advertizing. All those banners you see surfing blogs and stuff? Same thing. Google pays the blogger for putting those banners, and said blogger gets payed for people clicking the banner. Only, with blogs the owner takes all the cash. 
The idea behind PTC is simple. I pay a site (ex. Clixx) to advertize my site (ex. PTCtesters), let say 0.03$ per visit to my site. So, Clixx admin will take 0.001$ towards covering his costs (servers, support staff, food ...), and he pays you 0.002$ to view the add. If you come to PTCtesters and buy a product, I can make a huge profit, or just use it for brand building. After all, 30$ for 10000 people knowing your name, ain't half bad ...
The problems that fictional Clixx admin came to face was, that people weren't willing to click for a couple of cents and started dropping off. So Clixx dropped payment to 0.001$, and offered his users the option to rent referrals. Each rented referral would bring you 0.001$ per click, keeping his income static, but suddenly his hardest working users can earn some serious cash as well. To not run out of referrals, he put up gates, called upgrades and rented referral limits. So the more you invested, the more you gained, which just kept you coming back, while you were earning more and more (=link to »on referrals«).
Clixx becoming madly popular, advertizers spamming his mailbox, he relaxed and smiled at his own genious.

Is there money to be made?

Sure there is. I mean, yeah, there costs and risks as with any other investement, but at the end of the day, yeah ... How much is a question of how much you have to spend.
You can do it for free, though it a rough ride. You'll be going for months before you can do anything noticable with your cash, and even when you can rent or buy referrals, the return will be miniscule at best, so yeah ... it will be slow.
If you do have some money to invest, you'll be looking at a 3-4 month return, wich you probably won't even notice since you'll be reinvesting your money. Keep in mind though, that the main cost you'll have will be your upgrade, about 90$, the rest for renting referrals (generally 0.20$ per one), and then extending said referrals to get even higher returns.

Scams, scams, left and right!

So, you're worried. Your friends told you this is a ponzi scheme. Sure, some most probably are, but some have been around for years (like, decades). You see, people usually fear something that they don't understand, especially internet buisnesses. They try to explain their fear without needed information, kinda like a deer running from an unknown sound straight into a trap. You see, the deer didn't know the trap was their, as you're friends don't understand the business model behind PTCs.
Your PTC failed? Well, it happens in »real« life too. People don't have enough financial backing, don't understand what they're getting into, and they go bust. That why you usually don't invest into new buisness ventures, without expecting sicking returns. And you don't invest money that you don't care if you loose. Laws change and mess you up, there a million reasons why you fail. It somewhat expected that all won't succeed.
Not getting paid? Be sure to read through the ToS of the given site. Some won't pay certain countries until you get an upgrade, others won't pay to an unverified paypal accout, stuff like that. Some even state that they reserve the right to pay within 15 days. Do your homework before you join."
    [5]=>
    string(3542) "What is a ptc?

In pure basis, it advertizing. Sites that specialize in advertizing. All those banners you see surfing blogs and stuff? Same thing. Google pays the blogger for putting those banners, and said blogger gets payed for people clicking the banner. Only, with blogs the owner takes all the cash. 
The idea behind PTC is simple. I pay a site (ex. Clixx) to advertize my site (ex. PTCtesters), let say 0.03$ per visit to my site. So, Clixx admin will take 0.001$ towards covering his costs (servers, support staff, food ...), and he pays you 0.002$ to view the add. If you come to PTCtesters and buy a product, I can make a huge profit, or just use it for brand building. After all, 30$ for 10000 people knowing your name, ain't half bad ...
The problems that fictional Clixx admin came to face was, that people weren't willing to click for a couple of cents and started dropping off. So Clixx dropped payment to 0.001$, and offered his users the option to rent referrals. Each rented referral would bring you 0.001$ per click, keeping his income static, but suddenly his hardest working users can earn some serious cash as well. To not run out of referrals, he put up gates, called upgrades and rented referral limits. So the more you invested, the more you gained, which just kept you coming back, while you were earning more and more (=link to »on referrals«).
Clixx becoming madly popular, advertizers spamming his mailbox, he relaxed and smiled at his own genious.

Is there money to be made?

Sure there is. I mean, yeah, there costs and risks as with any other investement, but at the end of the day, yeah ... How much is a question of how much you have to spend.
You can do it for free, though it a rough ride. You'll be going for months before you can do anything noticable with your cash, and even when you can rent or buy referrals, the return will be miniscule at best, so yeah ... it will be slow.
If you do have some money to invest, you'll be looking at a 3-4 month return, wich you probably won't even notice since you'll be reinvesting your money. Keep in mind though, that the main cost you'll have will be your upgrade, about 90$, the rest for renting referrals (generally 0.20$ per one), and then extending said referrals to get even higher returns.

Scams, scams, left and right!

So, you're worried. Your friends told you this is a ponzi scheme. Sure, some most probably are, but some have been around for years (like, decades). You see, people usually fear something that they don't understand, especially internet buisnesses. They try to explain their fear without needed information, kinda like a deer running from an unknown sound straight into a trap. You see, the deer didn't know the trap was their, as you're friends don't understand the business model behind PTCs.
Your PTC failed? Well, it happens in »real« life too. People don't have enough financial backing, don't understand what they're getting into, and they go bust. That why you usually don't invest into new buisness ventures, without expecting sicking returns. And you don't invest money that you don't care if you loose. Laws change and mess you up, there a million reasons why you fail. It somewhat expected that all won't succeed.
Not getting paid? Be sure to read through the ToS of the given site. Some won't pay certain countries until you get an upgrade, others won't pay to an unverified paypal accout, stuff like that. Some even state that they reserve the right to pay within 15 days. Do your homework before you join."
  }
}

      

But the actual one echo json_encode($result)

returns an empty string.

Any suggestions / ideas on what is wrong?

+3


source to share


2 answers


Since you are using PDO you need to set the encoding like so ...

$db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC;charset=utf8",$username, $password);

      

When you get data from MySQL, any text will be encoded in "client-side encoding", which will most likely be window-1252 unless you configure it otherwise. The character that is causing your problem is the "curly quote" which is seen as 92 in the hex dump, which confirms the mysql client is encode text on windows-1252.



call mysqli_set_charset ("utf8") if you are using mysqli

add the charset parameter to your connection string if you are using PDO and PHP> = 5.3.6. In earlier versions, you need to do SET NAMES utf8.

+1


source


Try the following:



$jsonArray = array();
$query = $db->prepare('SELECT * FROM articles');
$query->execute();
$result = $query->fetchAll();
//turns timestamp into integer
for($i = 0; $i < count($result); ++$i) {
  $result[$i]['posted'] = strtotime($result[$i]['posted']);
  array_push($jsonArray, $results[$i]);
}
echo json_encode($jsonArray);
die();

      

0


source







All Articles