dryk50495 2015-02-26 12:15
浏览 8
已采纳

too long

this is my php file

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */
// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['userName']) && isset($_POST['userContact']) && isset($_POST['userAddress']) && isset($_POST['userStore']) && isset($_POST['userRequest'])) {

    $userName = $_POST['userName'];
    $userContact = $_POST['userContact'];
    $userAddress = $_POST['userAddress'];
    $userStore = $_POST['userStore'];
    $userRequest = $_POST['userRequest'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();


    // mysql inserting a new row
    $result = "INSERT INTO userrequests(userName, contactNumber, userAddress, storeList, requestBody) VALUES('$userName', '$userContact', '$userAddress', '$userStore', '$userRequest')";


    // check if row inserted or not
    if (mysqli_query($result,$db)) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
        echo $result;
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "IsitdispllayingthusOops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

And this is my db_connect.php

<?php 
    /**
 * A class file to connect to database
 */
class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        // import database connection variables
        require_once __DIR__ . '/db_config.php';

        // Connecting to mysql database
        $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());

        // Selecing database
        $db = mysqli_select_db($con,DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());

        // returing connection cursor
        return $con;
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection

    }

}



?>

It seems that there's something wrong with my code and this is the error I got mysqli_query() expects parameter 1 to be mysqli, string given in. Anyone knows how to solve this ? I have searched through StackOverFlow regarding this and tried all the solutions but still cant be solved.

  • 写回答

5条回答 默认 最新

  • doupingdiao3546 2015-02-26 12:27
    关注

    Modify your db connect class to maintain the connection:

    class DB_CONNECT {
    
        protected $connection = null;    
    
        // constructor
        function __construct() {
            // connecting to database
            $this->connection = $this->connect();
        }
    
        // destructor
        function __destruct() {
            // closing db connection
            $this->close();
        }
    
        /**
         * Function to connect with database
         */
        function connect() {
            // import database connection variables
            require_once __DIR__ . '/db_config.php';
    
            // Connecting to mysql database
            $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
    
            // Selecing database
            $db = mysqli_select_db($con,DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
    
            // returing connection cursor
            return $con;
        }
    
        public function getConnection() {
            return $this->connection;
        }
    
        /**
         * Function to close db connection
         */
        function close() {
            // closing db connection
    
        }
    
    }
    

    Then modify your query line to:

    if (mysqli_query($db->getConnection(), $result)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算