問題描述
我對 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,給定布爾值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!