0

Error is showing up in my code :-

<?php
session_start();
$page = 'index.php';
$connection = mysqli_connect("localhost","root","","cart");
if(mysqli_connect_errno())
{
    die("not connected to db ".mysqli_connect_error());
}
function product()
{
     $sql = "select id,name,description,price from products where quantity > 0 order by id DESC" ;
     $result = mysqli_query($connection,$sql);
     if(mysqli_num_rows($result))
     {
         echo 'no products to display';
     }
     else
     {
         while($row = mysqli_fetch_assoc($result))
         {
             echo '<div class="boxed">'.$row['name'].'<br>'.$row['price'].'<br>'.$row['description'].'<br>'.'</div>';
         }
     }
}
?>
<html>
<head>
<title>
</title>
<script>
.boxed {
  border: 1px solid green ;
}
</script>
</head>
<body>
<?php
product();
?>
</body>
</html>

Errors are :

Notice: Undefined variable: connection in /Applications/XAMPP/xamppfiles/htdocs/cart.php on line 11

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/XAMPP/xamppfiles/htdocs/cart.php on line 11

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/cart.php on line 12

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/cart.php on line 18

fsacer
  • 1,382
  • 1
  • 15
  • 23
tannishk
  • 19
  • 4
  • Could you give me feedback on my answer? :) Also really consider using singleton approach. – fsacer Jul 18 '15 at 12:40
  • Could you please upvote an answer if you find it useful and also mark it as accepted with the tick. If you don't know how look at the [tour](http://stackoverflow.com/tour). – fsacer Jul 18 '15 at 13:37

2 Answers2

0

Well the error is correct the $connection is not accessible from the function. To make it accessible within a function you must use a global keyword like this:

function product()
{
    global $connection;
    ...
}

The other errors are appering because of the first one. This is also answered in this question.

But this is not considered best practice as you can connect to database too many times for no good reason. I suggest you use a singleton class which makes sure you have only one connection open to the database. This is described in this answer.

Community
  • 1
  • 1
fsacer
  • 1,382
  • 1
  • 15
  • 23
0

First answer is correct, or you could pass $connection to your function like so:

<?php
session_start();
$page = 'index.php';
$connection = mysqli_connect("localhost","root","","cart");
function product($connection){
    $sql = "select id,name,description,price from products where quantity > 0 order by id DESC" ;
    $result = mysqli_query($connection,$sql);
    if(mysqli_num_rows($result))
    {
        echo 'no products to display';
    }
    else
    {
        while($row = mysqli_fetch_assoc($result))
        {
         echo '<div class="boxed">'.$row['name'].'<br>'.$row['price'].'<br>'.$row['description'].'<br>'.'</div>';
        }
    }
}

...

product($connection);
toro
  • 46
  • 2