【发布时间】:2014-09-16 10:22:42
【问题描述】:
试图从数据库中返回信息(这是空的)。它不断返回值 1 但它应该为 0。数据库为空但返回“我存在”。
<?php
set_time_limit(0);
$db = new mysqli("localhost","wwwrhino_Twitch","","wwwrhino_Twitch");
if($db->connect_errno) {
echo $db->connect_error;
die('Sorry, We are having some problems');
}
$offset = 0;
$count = 1;
do {
$Brad = json_decode(file_get_contents(
'https://api.twitch.tv/kraken/channels/greatbritishbg/follows?limit=25&offset=' . $offset
));
$Darren = json_decode(file_get_contents(
'https://api.twitch.tv/kraken/channels/Skairpigg/follows?limit=25&offset=' . $offset
));
foreach($Brad->follows as $Bradfollow) {
$username = (string)$Bradfollow->user->name;
//$result = $db->query("SELECT count(Username) FROM `Users` WHERE `Username` ='$username' ");
$result = $db->query("SELECT count(Username) AS total_users FROM `Users` WHERE `Username` ='$username' ");
if ($result->total_users > 0) {
echo 'I Exist!: '. (int)$result . ": " . $username . "<br>";
var_dump($result);
}
else {
echo 'Add me';
echo "<p>Inserting Value:". $username ."</p>";
echo "Var Dump:" . var_dump($result);
if($insert = $db->query("
INSERT INTO Users (Username, Minecraft_Name)
VALUES ('$username', 'Garrett')
")) {
echo $db->affected_rows . "<br>";
}
}
$offset+=25;
} while (!empty($Brad->follows));
?>
这是我的数据库:
谁能解释为什么结果总是返回为“1”
谢谢
【问题讨论】:
-
因为
$result甚至不是一个数字。var_dump()看看它不等于1 -
你没有检查抓取的行
-
@zerkms 我试过 var_dump($result)。以 1 的形式出现。
-
@Bradly Spicer:文档不同意你php.net/manual/en/mysqli.query.php
-
更新了我的答案。希望现在它能解决问题。
标签: php mysql mysqli phpmyadmin