错误出现在我的代码中:-
<?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>错误是:
注意:未定义变量:第11行/Applications/XAMPP/xamppfiles/htdocs/cart.php中的连接 警告: /Applications/XAMPP/xamppfiles/htdocs/cart.php ()期望参数1为mysqli,在第11行的mysqli_query中为null 警告: /Applications/XAMPP/xamppfiles/htdocs/cart.php ()要求参数1为mysqli_result,在第12行的mysqli_num_rows中为null 警告:在第18行的/Applications/XAMPP/xamppfiles/htdocs/cart.php中,mysqli_fetch_assoc()期望参数1为mysqli_result,null
发布于 2015-07-18 12:14:11
发布于 2015-07-20 19:10:37
第一个答案是正确的,或者您可以将$connection传递给您的函数如下:
<?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);https://stackoverflow.com/questions/31490962
复制相似问题