简体   繁体   English

XAMPP,Eclipse,PHP,MySQL:致命错误:在null上调用成员函数fetch_array()

[英]XAMPP, Eclipse, PHP, MySQL: Fatal error: Call to a member function fetch_array() on null

Beginner here, I am trying to make a call from the PhoneGap application to a web server using XAMPP local host for a school assignment. 在这里的初学者,我尝试使用XAMPP本地主机从PhoneGap应用程序向Web服务器发出呼叫,以进行学校作业。 I have a working login.php file from one of the previous projects. 我从先前的项目之一中获得了一个有效的login.php文件。 However, once transferred to the new project, Eclipse would not run the PHP codes and thus, the whole project, displaying the error as shown above: “Fatal error: Call to a member function fetch_array() on null”. 但是,一旦转移到新项目中,Eclipse将不会运行PHP代码,因此不会运行整个项目,并显示如上所示的错误:“致命错误:在null上调用成员函数fetch_array()”。

The differences between the project now and the ones before was where I placed the PHP files. 现在和以前的项目之间的区别是我放置PHP文件的地方。 Since I no longer have a microsoft azure subscription, I have switched to local host XAMPP. 由于我不再具有Microsoft Azure订阅,因此我已切换到本地主机XAMPP。 As a result, also changing my php file location from http://example.cloudapp/login.php to the same folder where I have my html files, to htcdocs/project/login.php. 结果,还将我的php文件位置从http://example.cloudapp/login.php更改为我拥有html文件的文件夹,并更改为htcdocs / project / login.php。 Here are the login.php codes: 这是login.php代码:

 <?php header("Access-Control-Allow-Origin: *"); header("Content-Type: application/json; charset=UTF-8"); error_reporting(E_ERROR); try{ $conn = new mysqli("127.0.0.1", "root", "root", "bencoolen"); $userid = $_GET["userid"]; $password = $_GET['password']; $query = "SELECT count(*) as found from profiles where userid ='" . $userid . "' and password = '" . $password . "'"; $result = $conn->query($query); $count = $result->fetch_array(MYSQLI_NUM); $json_out = "[" . json_encode(array("result"=>$count[0])) . "]"; echo $json_out; $conn->close(); } catch(Exception $e) { $json_out = "[".json_encode(array("result"=>0))."]"; echo $json_out; } ?> 

I have made sure that the database 'bencoolen' was created with a table called 'profiles' containing both 'userid' and 'password' fields. 我已经确保数据库“ bencoolen”是使用包含“ userid”和“ password”字段的名为“ profiles”的表创建的。

在此处输入图片说明

I have also downloaded 'PHP Development Tool' on Eclipse and all my php codes syntax are highlighted. 我还在Eclipse上下载了“ PHP开发工具”,并且突出显示了我所有的PHP代码语法。 Eclipse could also run the javascript and html codes before the I have added login.php. 在我添加login.php之前,Eclipse还可以运行javascript和html代码。 Eclipse directed me to Line 12, with the Fatal error, which was: Eclipse将我引导至第12行,出现致命错误,该错误是:

$count = $result->fetch_array(MYSQLI_NUM);

Heres my unsupported theory: I think they do not recognise my userid and password fields because the PHP file is not connected to the database due to its file location 这是我不支持的理论:我认为它们无法识别我的用户名和密码字段,因为PHP文件由于其文件位置而未连接到数据库

I think : You have to fetch that one record, it will contain the result of Count() 我认为:您必须提取一条记录,它将包含Count()的结果

$result = $db->query("SELECT COUNT(*) FROM `table`");
$row = $result->fetch_row();
echo '#: ', $row[0];

i am not saying my code bellow is best approach but you can follow and get result i also working same scenario and get success. 我并不是说我的代码波纹管是最好的方法,但是您可以遵循并获得结果,我也在同样的情况下工作并获得成功。

<?php
    $server = "127.0.0.1";
    $user = "root";
    $pass = "root";
    $db = "bencoolen";
    //open connection to mysql db
    $connection = mysqli_connect($server,$user,$pass,$db) or die("Error " . mysqli_error($connection));

    $userid = $_REQUEST['userid'];
    $password = $_REQUEST['password'];

    //fetch table rows from mysql db

    $sql = "select * from profiles WHERE userid= '$userid' AND password = '$password'";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

   // echo json_encode(mysqli_num_rows($result));
    if($result){
        if (mysqli_num_rows($result) >= 1 ) {
            $json=array("status"=>1,"message"=>"done");
            echo json_encode($json);
        }else{
            $json=array("status"=>0,"message"=>"email or password not match!");
            echo json_encode($json);
        }
    }else{
        $json=array("status"=>0,"message"=>"error");
        echo json_encode($json);
    }
    mysqli_close($connection); ?>

You can use " localhost " in place of 127.0.0.1 if you are running on same machine. 如果您在同一台计算机上运行,​​则可以使用“ localhost ”代替127.0.0.1

and you need to call your service like: 并且您需要像这样致电服务:

http://localhost/login.php?userid=test&password=12345 http://localhost/login.php?userid = test&password = 12345

Hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM