我正在尝试将DB值分配给PHP变量。数据库是SQL。我希望数据库值为$ HaveOfficialPage。我怎么能这样做?
$connection = mysqli_connect('localhost', '', '', '');
if($connection){
echo "Connected";
}
else {
die("Database connection failed");
}
$query = "SELECT HaveOfficialPage FROM vf_Category";
$result = mysqli_query($connection, $query);
if(!$result){
die("Query failed" . mysqli_error());
}
答案 0 :(得分:0)
如果你试图迭代结果:
while ($row = mysqli_fetch_row($result)) {
$haveOfficialPage = $row[0];
// do something with the variable
}
如果您只获得一条记录:
$row = mysqli_fetch_assoc($result);
$haveOfficialPage = $row["HaveOfficialPage"];
要选择具有条件的类别,请使用预准备语句
$paths = explode("/",$_SERVER["REQUEST_URI"]);
$category = end($paths);
$query = "SELECT HaveOfficialPage FROM vf_Category WHERE category_name = ?";
$stmt = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt, "s", $category);
mysqli_stmt_bind_result($stmt, $haveOfficialPage); // here we assign the result to your variable
mysqli_stmt_fetch($stmt); // fetch
echo $haveOfficialPage;