这是我的A-level comp sci课程的一些课程。我目前正在使用一种非常陌生的语言进行编码,我需要一些方面的帮助,我现在已经苦苦挣扎了一段时间。下面显示了我到目前为止所做的工作。我想要做的是输出表中对应的所有值都有一个特定的运动ID这是我到目前为止所拥有的,但它并没有返回任何值,尽管填充了表。
<?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';
}
$result = mysql_query("SELECT * FROM fixtureDetails WHERE fixtureDetails.sportID = '$sp'");
if(mysqli_num_rows($result) > 0) {
echo "yes";
}
mysqli_close($link);
?>
答案 0 :(得分:0)
您的代码存在一些问题:
mysql_
和mysqli_
。mysql_*
个功能。它们已被弃用。mysqli_close()
功能。您的代码存在问题,您需要更改此行:
$result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
要填充为表格,请使用结果集和循环。
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);
}
?>