gpt4 book ai didi

PHP 脚本似乎不起作用

转载 作者:行者123 更新时间:2023-11-30 23:52:37 26 4
gpt4 key购买 nike

我在我的网站空间上运行一个用于 Android 应用程序的脚本。虽然,它似乎不起作用。在某些网站空间上,当我尝试访问它时,我得到一个空白站点,在其他网站上,我收到 500 Internal Server Error。虽然这个脚本应该可以正常工作,因为它曾经这样做过。

它应该从表中获取所有信息并在一些 INNER JOIN 之后回显它。即使经过一些 mysqli_error 检查和一些 echo 检查变量等,我也无法跟踪错误。

变量 $con 确实存在,连接应该可以正常工作。我只是因为堆栈溢出而删除了它 ^^。

 <?php


$subId = 1;


$sql = "SELECT articles.a_id, sciences.science, articles.title, articles.content, login.username, articles.date, articles.viewed, articles.timestamp FROM articles
INNER JOIN sciences ON articles.s_id = sciences.s_id
INNER JOIN login ON articles.author = login.id
WHERE articles.s_id = ".$subId."
ORDER BY timestamp DESC";

if (!$res = mysqli_query($con,$sql)) {
echo "FAIL";
echo mysqli_error($con);
}
else {

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('a_id'=>$row[0],
'science'=>$row[1],
'title'=>$row[2],
'content'=>$row[3],
'author'=>$row[4],
'date'=>$row[5],
'viewed'=>$row[6],
'timestamp'=>$row[7]
));
}

echo json_encode(array("result"=>$result));
}
mysqli_close($con);


?>

编辑:我更新了我的代码:

    <?php


$subId = 1;

$con = mysqli_connect("HOST", "USER", "PASSWORD", "DATABASE");

if (!$con)
{
echo "Cant't connect to MySQL.<br>";
echo "Debug: " . mysqli_connect_errno()
}


$sql = "SELECT articles.a_id, sciences.science, articles.title, articles.content, login.username, articles.date, articles.viewed, articles.timestamp FROM articles
INNER JOIN sciences ON articles.s_id = sciences.s_id
INNER JOIN login ON articles.author = login.id
WHERE articles.s_id = ".$subId."
ORDER BY timestamp DESC";

if (!$res = $con->query($sql))
{
echo "FAIL";
}
else
{

$result = array();

while($row = $con->fetchArray($res))
{
array_push($result); //use the query to rename fields, if needed
}

echo json_encode(array("result"=>$result));
}



?>

最佳答案

我认为部分问题是您混淆了 mysql 和 mysqli 函数。您在程序上使用 mysqli,即:mysqli_fetch_array(),它不存在。您必须转换为 mysql,或者像这样使用 mysqli 函数:

$subId = 1;

$con = mysqli_connect('host', 'user', 'pass', 'dbname');

if (!$con)
{
echo "Cant't connect to MySQL.<br>";
echo "Debug: " . mysqli_connect_errno()
}


$sql = "SELECT articles.a_id, sciences.science, articles.title, articles.content, login.username, articles.date, articles.viewed, articles.timestamp FROM articles
INNER JOIN sciences ON articles.s_id = sciences.s_id
INNER JOIN login ON articles.author = login.id
WHERE articles.s_id = ".$subId."
ORDER BY timestamp DESC";

if (!$res = $con->query($sql))
{
echo "FAIL";
}
else
{

$result = array();

while($row = $con->fetchArray($res))
{
array_push($result); //use the query to rename fields, if needed
}

echo json_encode(array("result"=>$result));
}

关于PHP 脚本似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124526/

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