sql查询获取错误

时间:2013-10-08 20:08:13

标签: php mysql sql mysqli

我得到了一些代码,但一直有错误,我也是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

  3. 感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

修复其他问题后:变量范围。函数中没有$ con。将它作为arg或rework传递给类或其他东西。

答案 1 :(得分:1)

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

$con=mysqli_connect(hostaddress,dbuser,dbpass,databasename);