dtng25909 2019-07-13 16:09
浏览 230
已采纳

遇到mysqli_query问题,返回NULL但查询在phpmyadmin中有效

I'm tring to retrive information from a database while a user send Login command from iOS app. To test this function i'm launching my php page manually (ex. http://www.testdatabase.com/LoginFunctions.php) and forcing username programmatically.

The problem is that mysqli_query return NULL value. if i use "or die(mysql_error()" nothing happens. Even if i use mysqli_num_rows return 1, but $result is still empty. So when mysql_fetch_assoc is been executed the programm crashes without showing any error. Any idea? Thanks

<?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);
?>
  • 写回答

1条回答 默认 最新

  • dousaoxiancy199896 2019-07-13 16:21
    关注

    You are using the deprecated mysql_fetch function.Use the new one

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀