-2

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);
Community
  • 1
  • 1
Lauren
  • 21
  • 2
  • 8
  • Connect into db = fail? – Jan Czarny Nov 18 '13 at 11:32
  • 2 `(` and 1 `)`. Hmmmm – juergen d Nov 18 '13 at 11:32
  • 2
    I'm sorry, but we don't have enough information to be able to help you. Show us the **relevant code**. My guess is that you're trying to use `$con` in a function. If so, then it's a scope-related issue. See [this question](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) for how to resolve it. – Amal Murali Nov 18 '13 at 11:33
  • 2
    http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and – Your Common Sense Nov 18 '13 at 11:33
  • Is the `$con = mysqli_connect ...` enclosed in a if-statement perhaps? – Jite Nov 18 '13 at 11:35
  • You have 77 lines of code between the definition and the usage of that variable. 77 lines in which a lot can happen. How are we supposed to answer this? – deceze Nov 18 '13 at 11:36
  • I have edited so you can now see the whole script – Lauren Nov 18 '13 at 11:40
  • The supplied script would not be able to run at all, cause you have an unexpected close bracket `}` at the second to last line. – Jite Nov 18 '13 at 12:01
  • Im suspecting that this is not the full script, cause you are using variables that are clearly not declared in the script (`$u`, `$s` for example). And the bracket issue I stated above. – Jite Nov 18 '13 at 12:08
  • that is the full script - I've just been delagated the site from another developer so I even though I am now in charge I haven't wrote any of it. I'm just in charge of hoping it doesnt go wrong lol. – Lauren Nov 18 '13 at 12:13
  • Then it should not 'go through the script no bother', it should just show you a blank page or a error message saying that there is a undefined close bracket. And possibly let you know that there is a bunch of undefined variables. – Jite Nov 18 '13 at 12:16
  • I know it should - It was doing that earlier and fixed it. Thats why I came on here for some advice as to why I was getting the message :( . – Lauren Nov 18 '13 at 12:41
  • Well, either it can not be the same script as you are trying to run, or the script you are running is not showing anything but a error message. The code is not valid in the example you show, it cant run. – Jite Nov 18 '13 at 12:45

2 Answers2

1

Again define ($con = mysqli_connect("database connection info") in line 82 where the error occurred. This worked for me, maybe this was because of the scope of the $con variable.

ebo
  • 2,717
  • 1
  • 27
  • 22
-1

try to remove the (

$conn=mysql_connect(localhost,"root","root");
miholzi
  • 922
  • 1
  • 14
  • 36