未定义的变量:c:pathwayfile.php 第 82 行的 con


Undefined variable: con in C:pathwayfile.php on line 82

所以我正在尝试运行一些代码,这些代码将根据数据库查询打开不同的页面,但我最大的问题是注意:未定义的变量:每当我运行调试器时,它都会正确显示,因为它在网络上运行时无法正确显示。

它不会打扰脚本,直到它到达 mysqli_close($con); 它产生以下两个错误:

未定义的变量:c:''pathway''file.php 第 82 行中的 con

警告:mysqli_close() 期望参数 1 是 mysqli,null 在第 82 行的 C:''pathway''file.php 中给出

我不明白为什么 $con 变量没有在第 82 行定义,因为它在第 5 行明确定义。

 ($con = mysqli_connect("database connection info");

任何人都可以帮忙吗?

编辑 1
这是整个代码 -

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

再次定义 ($con = mysqli_connect("database connection info" ) 在第 82 行发生错误的地方。这对我有用,也许这是因为$con变量的范围。

尝试删除 (

$conn=mysql_connect(localhost,"root","root");