-1

I'v got some code but keep getting error's also I'm new to php so any help would be great :)

Here's my code

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

And these are the PHP error's I'm getting:

  1. Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/a2902119/public_html/test.php on line 24

  2. Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/a2902119/public_html/test.php on line 26

Thanks for the help!

aynber
  • 22,380
  • 8
  • 50
  • 63
Thomas De Marez
  • 668
  • 8
  • 24

2 Answers2

3

After fixing the other issues: Variable scope. $con is not available in the functions. Pass it in as an arg or rework into classes or something.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

Your SQL connection doesn't seem correct. It should have 4 parts.

$con=mysqli_connect(hostaddress,dbuser,dbpass,databasename);
Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58