我有一个运行简单SELECT
查询的php脚本,它似乎工作正常。
这是脚本:
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();
}
现在var_dump显示正常,这是它的样子:
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's 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's 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's 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's 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's 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's a million reasons why you fail. It's 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's 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's 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's 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's 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's 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's a million reasons why you fail. It's 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."
}
}
但实际的echo json_encode($result)
返回一个空字符串。
有关错误的任何建议/想法?
答案 0 :(得分:1)
由于您使用的是PDO,因此您需要像这样设置编码...
$db = new PDO("mysql:host=$hostname;dbname=topdecka_PTC;charset=utf8",$username, $password);
当您从MySQL获取数据时,任何文本都将以“客户端编码”进行编码,如果您不进行其他配置,则可能是windows-1252。导致问题的字符是“卷曲引号”,在十六进制转储中看作92,它确认mysql客户端在windows-1252中编码文本。
如果使用mysqli,请调用mysqli_set_charset(“utf8”)
如果使用PDO和PHP&gt; = 5.3.6,则将charset参数添加到连接字符串。在早期版本中,您需要执行SET NAMES utf8。
答案 1 :(得分:0)
试试这个:
$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();