我正在创建一个由数据库组成的Web服务器,当输入代码时,它将检索数据库中的一行。
这是我的代码为什么输出这个:
“。$ conn->错误;}}?php>
我的代码: -
<php
echo "HI";
$Hog_ID=filter_input(INPUT_POST, 'Hog_ID');
$host = "localhost";
$dbusername = "teambaboy";
$dbpassword = "pigislove";
$dbname = "teambaboy";
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('.mysqli_connect_errno().') '
. mysqli_connect_error());
}
else{
$sql = "SELECT (Animal_Number, Farm_code, Dam, Sire, Breed, Birth_date, Birth_weight, Date_worn, Wearing_weight, Feeds_given, Amount) FROM pig_registration WHERE Animal_Number = $Hog_ID";
if (mysqli_query($conn, $sql)){
echo $boom["Animal_Number"]. $boom["Farm_code"]. $boom["Dam"]. $boom["Sire"]. $boom["Breed"]. $boom["Breed"]. $boom["Birth_date"]. $boom["Birth_weight"]. $boom["Date_worn"]. $boom["Wearing_weight"]. $boom["Feeds_given"]. $boom["Amount"].;
}
else{
echo "Error: ". $sqL ."<br>". $conn->error;
}
}
?php>
答案 0 :(得分:0)
您错过了while循环以及$boom
变量未声明
$sql = "SELECT (Animal_Number, Farm_code, Dam, Sire, Breed, Birth_date, Birth_weight, Date_worn, Wearing_weight, Feeds_given, Amount) FROM pig_registration WHERE Animal_Number = $Hog_ID";
$result =mysqli_query($conn, $sql);
while($boom = mysqli_fetch_assoc($result)){ // mysqli_fetch_assoc() Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
echo $boom["Animal_Number"]. $boom["Farm_code"]. $boom["Dam"]. $boom["Sire"]. $boom["Breed"]. $boom["Breed"]. $boom["Birth_date"]. $boom["Birth_weight"]. $boom["Date_worn"]. $boom["Wearing_weight"]. $boom["Feeds_given"]. $boom["Amount"];
}
}
另外,我建议你使用prepared queries or PDO来帮助你防止sql注入
答案 1 :(得分:0)
你应该试试这个 -
<?php
$Hog_ID=filter_input(INPUT_POST, 'Hog_ID');
//$Hog_ID=2; for test pass static Hog_ID
$host = "localhost";
$dbusername = "teambaboy";
$dbpassword = "pigislove";
$dbname = "teambaboy";
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('.mysqli_connect_errno().') '
. mysqli_connect_error());
}else{
$sql = "SELECT Animal_Number, Farm_code, Dam, Sire, Breed, Birth_date, Birth_weight, Date_worn, Wearing_weight, Feeds_given, Amount FROM pig_registration WHERE Animal_Number = $Hog_ID";
$result = mysqli_query($conn, $sql);
$boom = mysqli_fetch_assoc($result);
if(!empty($boom)){
//print_r($boom);
echo $boom['Animal_Number'].$boom['Farm_code'].$boom['Dam'].$boom['Sire'].$boom['Breed'].$boom['Birth_date'].$boom['Birth_weight'].$boom['Date_worn'].$boom['Wearing_weight'].$boom['Feeds_given'].$boom['Amount'];
}else{
echo "Error: ". $sqL ."<br>". $conn->error;
}
}
?>