php mysqli_error() 需要 1 个参数,0 给定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2262988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:47:05  来源:igfitidea点击:

mysqli_error() expects exactly 1 parameter, 0 given

phpmysqlmysqli

提问by Michael

I am trying to get my head around mysql. Can someone tell my why this mysql query is not working? I am getting the following error:

我正在尝试了解 mysql。有人能告诉我为什么这个 mysql 查询不起作用吗?我收到以下错误:

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/freebet2/public_html/test.php on line 11

警告:mysqli_error() 需要 1 个参数,0 在第 11 行的 /home/freebet2/public_html/test.php 中给出

test.php

测试文件

<?php
        require_once($_SERVER['DOCUMENT_ROOT'].'/includes/db.php');
        $conn = db_connect();
        $result = $conn->query("ALTER TABLE users ADD COLUMN refer_old INT(10) AFTER refer_id");

              if(!$result){
                 echo "Error with MySQL Query: ".mysqli_error();
             }
        ?>

db.php

数据库文件

 <?php

    function db_connect() {
       $result = new mysqli('localhost', 'user', 'password', 'db');
       if (!$result) {
         throw new Exception('Could not connect to database server');
       } else {
         return $result;
       }
    }

    ?>

If I change the alter string to something like : $result = $conn->query("SELECT * FROM users refer_id");I get no error for some reason.

如果我将更改字符串更改为类似 :$result = $conn->query("SELECT * FROM users refer_id");由于某种原因,我没有收到任何错误。

采纳答案by jeroen

As far as the sql error is concerned, does 'user' have permissions to alter the table?

就 sql 错误而言,“用户”是否有权更改表?

回答by Pascal MARTIN

You are mixing the object-oriented and the procedural styles of the mysqli API :

您正在混合 mysqli API 的面向对象和程序风格:

You are using object-oriented :

您正在使用面向对象:

$result = new mysqli('localhost', 'user', 'password', 'db');

And, then, procedural :

然后,程序:

echo "Error with MySQL Query: ".mysqli_error();


You should use either OO, or procedural -- but not both ; and if you choose procedural, the functions expect the link identifier passed as a parameter.


您应该使用 OO 或程序性——但不能同时使用两者;如果您选择程序化,则函数需要将链接标识符作为参数传递。

For instance, mysqli_errorshould be called either using the object-oriented API :

例如,mysqli_error应该使用面向对象的 API 调用:

$link = new mysqli(...);
echo $link->error;

Or the procedural API :

或程序 API :

$link = mysqli_connect(...);
echo mysqli_error($link);


(Of course, it will not change the fact that you are having an error in your SQL query, but it'll allow you to get the error message, which should help finding the cause of that error)


(当然,它不会改变您在 SQL 查询中出现错误的事实,但它会允许您获取错误消息,这应该有助于找到该错误的原因)

回答by waiwai933

Use mysqli_error($result)as mysqli_error expects the connection to be passed as a parameter.

使用mysqli_error($result)mysqli_error 期望连接作为参数传递。