drcomwc134525 2013-04-09 00:48
浏览 41
已采纳

使用GET [重复]我的第一个PHP代码出错

I am writing my first server side api, and my code is giving me errors...

Here is the php code (I am using a get method)

<?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);
}
?>

My get URL is

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

My errors are:

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

I appreciate all your help

</div>
  • 写回答

2条回答 默认 最新

  • douyin4875 2013-04-09 00:51
    关注

    mysql and mysqli functions are not interchangeable you need to use all mysql or all mysqli functions.

    eg

    //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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题