-1

我有一些代码,但不断出错,我也是 php 新手,所以任何帮助都会很棒:)

这是我的代码

<?php
// Create connection
$con = mysqli_connect("host","database","pswd");

// Database Connection Check
if (mysqli_connect_errno($con)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    break;
}
echo "<table>";
echo nextweek("heren1kalender","Heren 1e Provinciale");
echo "</table>";

function nextweek($table, $ploegNaam) {
    // Get this weeks dates (monday and sunday and month)
    $current_day = date("N")
    $days_to_sunday = 7 - $current_day;
    $days_from_monday = $current_day - 1;
    $monday = date("d", strtotime("- {$days_from_monday} Days"));
    $sunday = date("d", strtotime("+ {$days_to_sunday} Days"));
    $month = date("m", strtotime("+ {$days_to_sunday} Days"));

    // SQL query
    $result = mysqli_query($con,"SELECT datum, hoofd, thuisploeg, bezoekers FROM " . $table . " WHERE thuisploeg LIKE '%Lochristi%' OR bezoekers LIKE '%Lochristi%'");

    while($row = mysqli_fetch_array($result)) {
        $string ="";
        // Get day and month from array
        $dag = substr($row['datum'], 4, -6);
        $maand = substr($row['datum'], 7, -3);

        if ($dag >= $monday AND $dag <= $sunday AND $maand == $month) {
            if (strpos($row['thuisploeg'],'Lochristi') !== false) {
                $string .= "<tr>";
                if (substr($row['hoofd'], 0, 1) >= 3) {
                    $string .= '<td class="win">' . $row['hoofd'] . "</td>";
                }
                else {
                    $string .= '<td class="loss">' . $row['hoofd'] . "</td>";
                }
                $string .= '<td>' . $ploegNaam . '</td>';
                $string .= "<td><strong>VS</strong></td>";
                $string .= '<td>' . $row['bezoekers'] . '</td>';
            }
            elseif (strpos($row['bezoekers'],'Lochristi') !== false) {
                $string .= "<tr>";
                if (substr($row['hoofd'], 0, 1) >= 3) {
                    $string .= '<td class="loss">' . $row['hoofd'] . "</td>";
                }
                else {
                    $string .= '<td class="win">' . $row['hoofd'] . "</td>";
                }
                $string .= '<td>' . $row['thuisploeg'] . '</td>';
                $string .= "<td><strong>VS</strong></td>";
                $string .= '<td>' . $ploegNaam . '</td>';
            }
            $string .= "</tr>";
        }
    }
    return $string;
}
?>

这些是我得到的 PHP 错误:

  1. 警告:mysqli_query() 期望参数 1 为 mysqli,在第 24 行的 /home/a2902119/public_html/test.php 中给出 null

  2. 警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,在第 26 行的 /home/a2902119/public_html/test.php 中给出 null

谢谢您的帮助!

4

2 回答 2

3

修复其他问题后:变量范围。$con 在函数中不可用。将其作为 arg 传递或返工到类或其他东西中。

于 2013-10-08T20:13:10.383 回答
1

您的 SQL 连接似乎不正确。它应该有 4 个部分。

$con=mysqli_connect(hostaddress,dbuser,dbpass,databasename);
于 2013-10-08T20:12:49.713 回答