So I am trying to run a bit of code that will open a different page depending on the database query but my biggest problem is the Notice: Undefined variable: con whenever I run the debugger, as it isn't displaying properly when it is live on the web.
It goes through the script no bother until it reaches mysqli_close($con); where it produces these two errors:
Undefined variable: con in C:\pathway\file.php on line 82
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\pathway\file.php on line 82
I can't understand why the $con variable is not defined on line 82 as it is clearly defined on line 5.
 ($con = mysqli_connect("database connection info");
Can anyone please help with this?
EDIT 1
Here is the whole code -
   $con = mysqli_connect("DB connection");
    if (mysqli_connect_errno($con)) {
        echo "<p class='sect'>Could not connect to DB</p>";
  
    } else {
 
    $q = "SELECT DISTINCT newsitem.niid,newstitle,newssnippet,sitename,newsimage FROM newsitem,newsbusiness
          INNER JOIN newsbusiness ON newsitem.niid=newsbusiness.niid
          LEFT JOIN newsimage ON newsitem.niid=newsimage.niid
          WHERE newsitem.niid=newsbusiness.niid AND newsstatus='enabled' ";
    
    if ("$u" == "Any") {
        
    } else {
        
        $q = $q . "AND sitename='$u' ";
        
    }
    
    if ("$s" == "Any") {
        
    } else {
        
        $parts = explode('-', $s);
        $y = $parts[0];
        $m = $parts[1];
        $q = $q . "AND YEAR(newsdate) = $y AND MONTH(newsdate) = $m ";
        
    }
    
    $q = $q . "ORDER BY newsdate DESC LIMIT 0,10";
       
    $result = mysqli_query($con, $q);
    
    while ($tnrow = mysqli_fetch_array($result)) {
        
        echo "
            <div class='newssummary'>";
        
        if ("" . $tnrow ['newsimage'] .  ""== "none") {
            
            echo "<p class = 'newsmore' > < a href = 'newsitem.php?i=" . $tnrow['niid'] . "' > Read More</a > </p>";
        }else
            if ($tnrow ['imageposition'] == 'Centre') {
                
                    echo "<p class='newsmore'> <a href='newsitem3.php?i=" . $tnrow['niid'] . "'>Read More</a></p>";
                
                }else if ($tnrow ['imageposition'] == 'Right') {
                    
                    echo "<p class='newsmore'> <a href='newsitem2.php?i=" . $tnrow['niid'] . "'>Read More</a></p>";
                    
                }else if ($tnrow ['imageposition'] == 'Left') {
                    
                    echo "<p class='newsmore'> <a href='newsitem.php?i=" . $tnrow['niid'] . "'>Read More</a></p>";
                    
                }else if ($tnrow ['imageposition'] == 'none') {
                    
                    echo "<p class='newsmore'> <a href='newsitem.php?i=" . $tnrow['niid'] . "'>Read More</a></p>";
                    
                }
            }
            echo "<div class='newspic'><img src='http://www.nortech.org.uk/news/" . $tnrow['newsimage'] . "' alt='" . $tnrow['newstitle'] . "' /></div>";
        }
        echo "
                    <p><strong>" . $tnrow['newstitle'] . "</strong></p>
                    <p class='news'>" . $tnrow['newssnippet'] . "</p>
                    </div>
                    <div class='padder1'></div>
                    <div class='rtgreengp'></div>";
    }
   
mysqli_close($con);
 
     
     
     
    