«Если рабочий хочет хорошо выполнять свою работу, он должен сначала заточить свои инструменты» — Конфуций, «Аналитики Конфуция. Лу Лингун»
титульная страница > программирование > input: Why Does "Warning: mysqli_query() expects parameter 1 to be mysqli, resource given" Error Occur and How to Fix It? output: 解决“Warning: mysqli_query() 参数应为 mysqli 而非 resource”错误的解析与修复方法

input: Why Does "Warning: mysqli_query() expects parameter 1 to be mysqli, resource given" Error Occur and How to Fix It? output: 解决“Warning: mysqli_query() 参数应为 mysqli 而非 resource”错误的解析与修复方法

Опубликовано в 2025-05-01
Просматривать:198

Why Does

mysqli_query() Expects Parameter 1 to be mysqli, Resource Given

When attempting to execute a MySQL query using the mysqli_query() function, the "Warning: mysqli_query() expects parameter 1 to be mysqli, resource given" error can occur. This error indicates a mismatch between the type of the first parameter and the expected resource type.

Root Cause:

The root cause of this error is typically related to mixing the mysqli and mysql extensions within your code. These two extensions are not interchangeable and cannot be used together.

Solution:

To resolve this issue, ensure that you are using the same extension throughout your code. If you are using the mysqli extension, all database-related functions must use the mysqli prefix, such as mysqli_connect(), mysqli_select_db(), and mysqli_query().

In the example provided, the code contains mixings of mysql and mysqli functions. To fix it, replace the following lines:

$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("mrmagicadam") or die ("no database");

with:

$myConnection= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");

By making this change, you are ensuring that all database-related functions are using the correct extension, which will eliminate the error and allow the MySQL query to execute successfully.

Последний учебник Более>

Изучайте китайский

Отказ от ответственности: Все предоставленные ресурсы частично взяты из Интернета. В случае нарушения ваших авторских прав или других прав и интересов, пожалуйста, объясните подробные причины и предоставьте доказательства авторских прав или прав и интересов, а затем отправьте их по электронной почте: [email protected]. Мы сделаем это за вас как можно скорее.

Copyright© 2022 湘ICP备2022001581号-3