简体   繁体   English

PHP Mysqli错误:mysqli_query()期望参数1为mysqli,给定布尔值

[英]PHP Mysqli error: mysqli_query() expects parameter 1 to be mysqli, boolean given

this is not a question, I need your help. 这不是问题,我需要您的帮助。 I read similar threads but I couldn't debug the problem in my code. 我读过类似的线程,但无法调试代码中的问题。 Could you plz give the correct solution? 您能给出正确的解决方案吗?

<?php
/*variable declaration*/
$host="localhost";
$user="root";
$pass="";
$dbname='mydatabase';

/*connection to mysql server*/
$connect = mysqli_connect($host,$user,$pass);

/*selecting database*/
$selectdb=mysqli_select_db($connect,$dbname);

if(!$selectdb){
    echo 'Failed to connect. Wrong username or database.';
}else{
    echo 'Connection successful.';
}

/*creating task*/
$query = "SELECT 'Name', 'Password' FROM 'db' ORDER BY 'id'";

if(mysqli_query($selectdb,$query)){
echo 'Success';
}else{
echo '<br>Failed';
}
?>

enter image description here 在此处输入图片说明

Modify 修改

$query = "SELECT 'Name', 'Password' FROM 'db' ORDER BY 'id'";

To

$query = "SELECT `Name`, `Password` FROM db ORDER BY 'id'";

You just cannot use the quotes on column name on the query, either use backticks or nothing at all. 您只是不能在查询的列名上使用引号,或者使用反引号或根本不使用。

if(mysqli_query($connect,$query)){...

在这种情况下,您需要使用$connect而不是$selectdb

<?php
/*variable declaration*/
$host="localhost";
$user="root";
$pass="";
$dbname='mydatabase';
$connect = mysqli_connect($host,$user,$pass,$dbname);

/*I avoided this line & used $connect inside if() statement which worked    perfectly*/
//$selectdb = mysqli_select_db($connect,$dbname);

if(!$connect){
    echo 'Failed to connect. Wrong username or database.';
}else{
    echo 'Connection successful.';
}

/*creating task*/
$query = "SELECT `Name`, `Password` FROM `db` ORDER BY `id`";

if($query_run = mysqli_query($connect,$query)){
echo 'Success';
}else{
echo '<br> Failed';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM