mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given(mysqli_fetch_assoc() 期望参数 1 是 mysqli_result,给定布尔值)
问题描述
我对 PHP 和 MySQL 还是很陌生,我就是想不通.我在论坛上到处搜索,但没有找到我能理解的答案.我最初使用 mysql_fetch_assoc() 但我只能搜索数字并且在搜索字母时也收到错误.我希望我在这里走在正确的轨道上.在此先感谢您的帮助!
I am pretty new to PHP and MySQL and I just can't figure this one out. I have searched all around the forum but haven't found an answer I can make sense of. I originally was using mysql_fetch_assoc() but I could only search numbers and I received errors when searching for letters as well. I hope I am on the right track here. Thank you in advance for all your help!
$con = mysqli_connect($hostname,$username,$password) or die ("<script language='javascript'>alert('Unable to connect to database')</script>");
mysqli_select_db($con, $dbname);
if (isset($_GET['part'])){
    $partid = $_GET['part'];
    $sql = 'SELECT * 
        FROM $usertable 
        WHERE PartNumber = $partid';
    $result = mysqli_query($con, $sql);
    $row = mysqli_fetch_assoc($result);
    $partnumber = $partid;
    $nsn = $row["NSN"];
    $description = $row["Description"];
    $quantity = $row["Quantity"];
    $condition = $row["Conditio"];
}
推荐答案
当您的结果不是结果(而是假")时会发生这种情况.你应该改变这一行
This happens when your result is not a result (but a "false" instead). You should change this line
$sql = 'SELECT * FROM $usertable WHERE PartNumber = $partid';
为此:
$sql = "SELECT * FROM $usertable WHERE PartNumber = $partid";
因为 " 可以解释 $variables 而 ' 不能.
because the " can interprete $variables while ' cannot.
适用于整数(数字),对于字符串,您需要将 $variable 放在单引号中,例如
Works fine with integers (numbers), for strings you need to put the $variable in single quotes, like
$sql = "SELECT * FROM $usertable WHERE PartNumber = '$partid' ";
如果你想/必须使用单引号,那么php不能解释变量,你必须这样做:
If you want / have to work with single quotes, then php CAN NOT interprete the variables, you will have to do it like this:
 $sql = 'SELECT * FROM '.$usertable.' WHERE string_column = "'.$string.'" AND integer_column = '.$number.';
                        这篇关于mysqli_fetch_assoc() 期望参数 1 是 mysqli_result,给定布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysqli_fetch_assoc() 期望参数 1 是 mysqli_result,给定布
				
        
 
            
        基础教程推荐
- 将变量从树枝传递给 js 2022-01-01
 - php中的foreach复选框POST 2021-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - php中的PDF导出 2022-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				