所以我得到了这个在mysql phpmyadmin中完全正常工作的查询,但是当我在php文件中使用它时它无效。
SELECT *
FROM services_package
JOIN service_in_package
ON services_package.id = service_in_package.package_id
JOIN itv
ON service_in_package.service_id = itv.id
JOIN tv_genre
ON itv.tv_genre_id = tv_genre.id
WHERE services_package.id = 25
任何想法为什么?
我已经多次尝试了,问题出现在这里:
INNER JOIN tv_genre ON itv.tv_genre_id = tv_genre.id
这是php代码:
$sql = "
SELECT *
FROM services_package
JOIN service_in_package
ON services_package.id = service_in_package.package_id
JOIN itv
ON service_in_package.service_id = itv.id
JOIN tv_genre
ON itv.tv_genre_id = tv_genre.id
WHERE services_package.id = 25
";
$rows = array();
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
echo json_encode($rows);
} 好的,所以我没有错误。即使出现错误也是如此。循环循环8次(这是正确的,因为它应该返回8行)但它没有输出。
它确实在以下情况下起作用:
INNER JOIN tv_genre ON itv.tv_genre_id = tv_genre.id
已删除。我很困惑。
更新: 我检查了phpmyadmin中的行,注意到多行具有相同的名称' ID'并认为它可能会在转换为JSON时产生冲突,因此我提出了一个新的查询:
SELECT itv.id AS channel_id,
tv_genre.id AS genre_id,
services_package.id AS service_package_id,
itv.name AS channel_name,
itv.descr AS channel_description,
services_package.type AS channel_type,
itv.number, itv.censored,
itv.cmd AS channel_source,
tv_genre.title AS genre_name,
itv.logo
FROM services_package
JOIN service_in_package ON services_package.id = service_in_package.package_id
JOIN itv ON service_in_package.service_id = itv.id
JOIN tv_genre ON itv.tv_genre_id = tv_genre.id
WHERE services_package.id =25
在phpmyadmin中工作,而不是在php文件中工作:/
var_dump结果(由于显而易见的原因改变了channel_source):
array(11) { ["channel_id"]=> string(3) "846" ["genre_id"]=> string(2) "10" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(8) "HAC 2 HD" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(1) "5" ["censored"]=> string(1) "0" ["channel_source"]=> string(45) "http://test.com" ["genre_name"]=> string(8) "Englisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "845" ["genre_id"]=> string(2) "10" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(8) "HAC 1 HD" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(1) "4" ["censored"]=> string(1) "0" ["channel_source"]=> string(44) "http://test.com" ["genre_name"]=> string(8) "Englisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "831" ["genre_id"]=> string(2) "10" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(9) "HAC 3 HD " ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(1) "6" ["censored"]=> string(1) "0" ["channel_source"]=> string(45) "http://test.com" ["genre_name"]=> string(8) "Englisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "829" ["genre_id"]=> string(2) "10" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(8) "HAC SD 3" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(1) "3" ["censored"]=> string(1) "0" ["channel_source"]=> string(39) "http://test.com" ["genre_name"]=> string(8) "Englisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "828" ["genre_id"]=> string(2) "10" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(8) "HAC SD 2" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(1) "2" ["censored"]=> string(1) "0" ["channel_source"]=> string(39) "http://test.com" ["genre_name"]=> string(8) "Englisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "814" ["genre_id"]=> string(2) "10" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(13) "Go To Luxe.TV" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(2) "23" ["censored"]=> string(1) "0" ["channel_source"]=> string(41) "http://test.com" ["genre_name"]=> string(8) "Englisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "808" ["genre_id"]=> string(1) "8" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(16) "Nature & Animaux" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(2) "10" ["censored"]=> string(1) "0" ["channel_source"]=> string(36) "http://test.com" ["genre_name"]=> string(11) "Französisch" ["logo"]=> string(0) "" } array(11) { ["channel_id"]=> string(3) "807" ["genre_id"]=> string(1) "8" ["service_package_id"]=> string(2) "25" ["channel_name"]=> string(8) "HAC SD 1" ["channel_description"]=> string(0) "" ["channel_type"]=> string(2) "tv" ["number"]=> string(1) "1" ["censored"]=> string(1) "0" ["channel_source"]=> string(39) "http://test.com" ["genre_name"]=> string(11) "Französisch" ["logo"]=> string(0) "" }
我现在唯一能想到的是将结果转换为JSON时出错了
答案 0 :(得分:3)
<强>解决了!问题是我的mysql结果包含一些无法转换为JSON的字符使用此函数来修复它:
function utf8ize($mixed) {
if (is_array($mixed)) {
foreach ($mixed as $key => $value) {
$mixed[$key] = utf8ize($value);
}
} else if (is_string ($mixed)) {
return utf8_encode($mixed);
}
return $mixed;
}