mysqli_query expects at least 2 parameters

前端 未结 5 1035
旧时难觅i
旧时难觅i 2020-11-21 06:53

This mysqli_query command results in the error below

mysqli_query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 

相关标签:
5条回答
  • 2020-11-21 07:23

    From the manual

    Procedural style
    
    mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
    

    You'll notice the $link and $query variables.

    This means that you need to pass the function a valid mysqli link resource as well as the query you wish to perform. This lets the function know which established connection to the server to use.

    A link resource can be created using:

    Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

    and an example of how to do so can be found on the aforementioned manual page.

    0 讨论(0)
  • 2020-11-21 07:28

    mysqli_query excepts 2 parameters, the first variable is mysqli_connect equivalent variable, the second one is the query you have provided

    $name1 = mysqli_connect(localhost,db_username ,db_pswd ,db_name );
    
    $name2 = mysqli_query($name1,"INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)");
    
    0 讨论(0)
  • 2020-11-21 07:34

    You need to specify the connection that you made to your database somewhere earlier in your page. You should put that variable in the query. Suppose you made a variable called $con. Then your code should be like this.

    mysqli_query($con,"INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 
    
    0 讨论(0)
  • 2020-11-21 07:35

    If you get any of the following errors:

    mysqli_query() expects at least 2 parameters, 1 given

    mysqli_select_db() expects exactly 2 parameters

    mysqli_real_escape_string() expects exactly 2 parameters

    It means that you have not passed the mandatory parameter to these functions. MySQLi procedural style functions expect the first parameter to be a valid MySQLi connection link.

    For example, mysqli_query expects a database link as the first argument and the actual SQL query as a second argument.

    Assuming you have this or similar connection code somewhere at the start of you script.

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset ($mysqli, 'utf8mb4');
    

    You can use the connection link saved in $mysqli and pass it as a first argument.

    mysqli_query($mysqli, "INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 
    

    Most of the mysqli procedural functions require the connection to be passed as an argument. However, a simpler option would be to switch to Object-oriented style. In OOP you call the method on the object passing only the SQL as a single argument.

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = new mysqli($host, $user, $pass, $db);
    $mysqli->set_charset('utf8mb4');
    
    $mysqli->query("INSERT INTO `counter`.`hits` (`page_hits`) VALUES (1)"); 
    

    Warning!

    You should be using parameterized prepared statements instead of manually building your queries. mysqli_query() should only be utilized when you are not passing any inputs to your SQL. Whenever you want to pass inputs, as is the case with INSERT for example, you must use parameter binding. You can replace you mysqli_query() call with:

    $stmt = $mysqli->prepare('INSERT INTO `counter`.`hits` (`page_hits`) VALUES (?)');
    $stmt->bind_param('s', $hits);
    $stmt->execute();
    
    0 讨论(0)
  • 2020-11-21 07:40

    It seems you are confusing mysql_query with mysqli_query. The former accepts the sql statement as the first param, while the latter expects a link identifier (created by Mysqli::connect) as the first param and the statement as the second.

    The two extensions are not compatable with each other. I suggest you pick one, read the manual pages on how to connect, execute queries, etc, and forget the other exists. which one you pick is up to you, mysqli is more feature rich but more complicated as a result.

    0 讨论(0)
提交回复
热议问题