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

2
  • Could you give me feedback on my answer? :) Also really consider using singleton approach. Commented Jul 18, 2015 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. Commented Jul 18, 2015 at 13:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.