1

我正在编写我的第一个服务器端 api,我的代码给了我错误......

这是php代码(我使用的是get方法)

<?php 

//Check if there is a function
if(function_exists($_GET['function'])) {

    //If it equals loadAll
    if ($_GET['function']=='loadAll'){

        //Then call the function and pass it the parameter it needs
        $_GET['function']($_GET['entity']);
    }

    //If found, call the function with value as a parameter
   $_GET['function']($_GET['value']);
}

/**
 * This method loads all of an object from the DB
 */
function loadAll($entity){

    //Variables for connecting to database.
    //These variable values come from hosting account.
    $hostname = "------";
    $username = "-----";
    $dbname = "-----";

    //These variable values need to be changed by you before deploying
    $password = "-----";

    //Connecting to your database
    mysql_connect($hostname, $username, $password) OR DIE ("Unable to 
    connect to database! Please try again later.");
    mysql_select_db($dbname);

    //Fetching from your database table.
    $query = "SELECT * FROM $entity";
    $result = mysql_query($query);

    $myJson=array();
    //Parse to array
    while($row = mysqli_fetch_array($result)){
        $myJson[]=$row;
    }

    //Close connection
    mysqli_close($con);

    //Encode and send response
    echo json_encode($myJson);
}
?>

我的获取网址是

http://someURL.com/api/index.php?function=loadAll&entity=School

我的错误是:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, resource given in D:\hosting\somenumber\html\api\index.php on line 42

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in D:\hosting\somenumber\html\api\index.php on line 47
[]
Fatal error: Function name must be a string in D:\hosting\somenumber\html\api\index.php on line 14

感谢您的帮助

4

2 回答 2

3

mysql 和mysqli函数不可互换,您需要使用所有 mysql 或所有 mysqli 函数。

例如

//Connecting to your database
mysqli_connect($hostname, $username, $password) OR DIE ("Unable to 
connect to database! Please try again later.");//I changed this
mysqli_select_db($dbname);//I changed this
//Fetching from your database table.
$query = "SELECT * FROM $entity";
$result = mysqli_query($query);//I changed this

$myJson=array();
//Parse to array
while($row = mysqli_fetch_array($result)){  
    $myJson[]=$row;
}
于 2013-04-09T00:51:59.570 回答
1

你混淆了 mysql_query 和 mysqli_fetch_array。只选择 mysqli,重写查询和连接。mysql_query 自 PHP 5.5.0 起已弃用,将来将被删除。

于 2013-04-09T00:56:09.983 回答