此购物车代码中的 php 代码错误
Error in php code in this cart 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>
错误是:
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
嗯,错误是正确的,$connection
无法从该函数访问。要使其在函数中可访问,您必须使用 global
关键字,如下所示:
function product()
{
global $connection;
...
}
由于第一个错误,出现了其他错误。这也在 this 问题中得到了回答。
但这不是最佳实践,因为您可能无缘无故多次连接到数据库。我建议您使用单例 class 以确保您只有一个打开的数据库连接。 this 回答中对此进行了描述。
第一个答案是正确的,或者您可以像这样将 $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);
我的代码中出现错误:-
<?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>
错误是:
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
嗯,错误是正确的,$connection
无法从该函数访问。要使其在函数中可访问,您必须使用 global
关键字,如下所示:
function product()
{
global $connection;
...
}
由于第一个错误,出现了其他错误。这也在 this 问题中得到了回答。
但这不是最佳实践,因为您可能无缘无故多次连接到数据库。我建议您使用单例 class 以确保您只有一个打开的数据库连接。 this 回答中对此进行了描述。
第一个答案是正确的,或者您可以像这样将 $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);