【问题标题】:PHP mysqli unexpected parameter QuestionPHP mysqli 意外参数问题
【发布时间】:2023-03-17 06:50:01
【问题描述】:

我觉得自己完全不理解自己做错了什么,但还是这样吧。

我正在编写一个简单的 Web 表单,它将信息存储在 MySQL 数据库中。我收到此错误:

mysqli_stmt_init() expects parameter 1 to be mysqli, null given in /myfile.php

这是我的代码:

$server = 'localhost';
$username = 'myusername';
$password = 'mypassword';
$db = 'mydb';
$link = mysqli_connect($server, $username, $password, $db);
.
.
. 
$stmt = mysqli_stmt_init($link); /*This line throws the error*/

有什么想法吗?提前致谢。

【问题讨论】:

  • 我的问题是我的变量范围(在划分我的函数之后。与 Java 不同:)感谢 Paolo 的 error_reporting(E_ALL);提示!

标签: php mysql mysqli


【解决方案1】:

您是否 100% 确定您已成功连接到数据库?

在脚本顶部添加:

error_reporting(E_ALL);
ini_set('display_errors', 1);

同时在你的连接线下方添加:

if (mysqli_connect_errno()) {
    printf("Connect failed: %s", mysqli_connect_error());
    exit;
}

【讨论】:

  • 是的,我在 $stmt 声明之前运行了一个简单的检查 if ($link) { $stmt = mysqli_stmt_init($link); } 这应该可行,对吧?
【解决方案2】:

如果这是您的开发服务器,您可能应该在 php.ini 文件中而不是在每个脚本中设置以下行。

error_reporting = E_ALL display_errors = on

如果这不能解决您的问题,那么您应该打印我们的 $link 以查看它是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2012-01-02
    • 2015-03-10
    • 2013-09-25
    • 2020-06-15
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多