gpt4 book ai didi

php - 多个内部连接在 PHP 中不起作用

转载 作者:行者123 更新时间:2023-11-29 05:06:00 24 4
gpt4 key购买 nike

所以我得到的这个查询在 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 时出了点问题

最佳答案

已解决!问题是我的 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;
}

关于php - 多个内部连接在 PHP 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48062563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com