代碼有幾個問題:
- 不要混用多個庫多。您一起使用
mysql_
和mysqli_
。
- 請勿使用
mysql_*
功能。他們已被棄用。
- 不需要
mysqli_close()
功能。
- 您不需要重複該表格。
- 您不打印查詢結果中的任何內容。
與您的代碼的問題是,你需要改變這一行:
$result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
要填充的表,使用ResultSet和循環。
if (mysqli_num_rows($result)) {
while (false != ($data = mysqli_fetch_assoc($result))) {
// Do whatever with your data.
var_dump($data);
}
} else {
echo "No records.";
}
終極密碼
<?php
$link = mysqli_connect("localhost", "root", "pizza","fixtures");
if ($_POST['SPORT'] == "Football") {
$sp = '1';
}
if ($_POST['SPORT'] == "Tennis") {
$sp = '2';
}
if ($_POST['SPORT'] == "Swimming") {
$sp = '3';
}
// Execute the query and save the resultset.
$result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
// Check if there are any rows returned.
if (mysqli_num_rows($result)) {
// If there are rows returned, save every row to $data.
while (false != ($data = mysqli_fetch_assoc($result))) {
// Do whatever with your data.
var_dump($data);
}
} else {
// If there are no records, display a message.
echo "No records.";
}
?>
如果你想有一個函數來發送計數的響應,你可以有這樣的事情:
<?php
function getCount() {
$link = mysqli_connect("localhost", "root", "pizza","fixtures");
if ($_POST['SPORT'] == "Football") {
$sp = '1';
}
if ($_POST['SPORT'] == "Tennis") {
$sp = '2';
}
if ($_POST['SPORT'] == "Swimming") {
$sp = '3';
}
// Execute the query and save the resultset.
$result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
// Check if there are any rows returned.
return mysqli_num_rows($result);
}
?>
如果你想只是一個true
或false
,你可以這樣做:
<?php
function getCount() {
$link = mysqli_connect("localhost", "root", "pizza","fixtures");
if ($_POST['SPORT'] == "Football") {
$sp = '1';
}
if ($_POST['SPORT'] == "Tennis") {
$sp = '2';
}
if ($_POST['SPORT'] == "Swimming") {
$sp = '3';
}
// Execute the query and save the resultset.
$result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
// Check if there are any rows returned.
return (mysqli_num_rows($result) > 0);
}
?>
是。對不起拼寫錯誤 –
檢查我的答案。 –