gpt4 book ai didi

php - mysqli_query 有问题,返回 NULL 但查询在 phpmyadmin 中有效

转载 作者:行者123 更新时间:2023-11-29 09:39:05 25 4
gpt4 key购买 nike

当用户从 iOS 应用程序发送登录命令时,我尝试从数据库检索信息。为了测试这个功能,我手动启动我的 php 页面(例如 http://www.testdatabase.com/LoginFunctions.php )并以编程方式强制用户名。

问题是 mysqli_query 返回 NULL 值。如果我使用“or die(mysql_error()”,什么也不会发生。即使我使用 mysqli_num_rows 返回 1,但 $result 仍然为空。因此,当执行 mysql_fetch_assoc 时,程序崩溃,但没有显示任何错误。任何想法?谢谢

<?php
// Create connection
$con=mysqli_connect("localhost","super","super","testdb");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$action = "login";
$username = "Peperoncino";
$response = array();

if ($action == "login")
{
$query = "SELECT psw AS pswrd,id FROM Activities WHERE nome = 'Peperoncino' LIMIT 1";

if ($result = mysqli_query($con, $query))
{
$values = mysql_fetch_assoc($result);
$password = $values['pswrd'];
$response["password"] = $password;
$response["message"] = "Get information from db";
}
else
{
echo "err";
}

echo json_encode($response);
}

// Close connections
mysqli_close($con);
?>

最佳答案

您正在使用已弃用的 mysql_fetch 函数。请使用新函数

<?php
// Create connection
$con=mysqli_connect("localhost","super","super","testdb");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$action = "login";
$username = "Peperoncino";
$response = array();

if ($action == "login")
{
$query = "SELECT psw AS pswrd,id FROM Activities WHERE nome = 'Peperoncino' LIMIT 1";

if ($result = mysqli_query($con, $query))
{
$values = mysqli_fetch_assoc($result);
$password = $values['pswrd'];
$response["password"] = $password;
$response["message"] = "Get information from db";
}
else
{
echo mysqli_error($conn);
}

echo json_encode($response);
}

// Close connections
mysqli_close($con);
?>

关于php - mysqli_query 有问题,返回 NULL 但查询在 phpmyadmin 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57020785/

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