2017-01-02 37 views
-3

這是針對我的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

檢查我的答案。 –

回答

0

代碼有幾個問題:

  1. 不要混用多個庫多。您一起使用mysql_mysqli_
  2. 請勿使用mysql_*功能。他們已被棄用。
  3. 不需要mysqli_close()功能。
  4. 您不需要重複該表格。
  5. 您不打印查詢結果中的任何內容。

與您的代碼的問題是,你需要改變這一行:

$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); 
    } 
?> 

如果你想只是一個truefalse,你可以這樣做:

<?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); 
    } 
?> 
+0

謝謝你的幫助,但我想echo(返回)這些行以及選擇它們? (對不起,我從來沒有用這種語言編碼過)謝謝 –

+0

@FaayoBedrudin將函數全部包含在函數中,並在if和echo中返回。 –

+0

如果你不介意,請你可以告訴我。我也勾選了你的答案。 –

相關問題