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

时间:2013-11-18 11:30:28

标签: php mysql mysqli

所以我试图运行一些代码,根据数据库查询打开一个不同的页面,但我最大的问题是注意:未定义的变量:con每当我运行调试器时,因为它没有正确显示它是在网上直播。

它通过脚本没有麻烦,直到它到达mysqli_close($ con);它产生这两个错误:

  

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

     

警告:mysqli_close()要求参数1为mysqli,在第82行的C:\ pathway \ file.php中给出null

我无法理解为什么$ 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);

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

尝试删除(

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