无法在 PHP 网页中进行 MySQL 查询
Can't make MySQL queries in PHP webpage
所以我在一个网站上工作,我需要从 MySQL 服务器中提取数据并将其显示在网页上。我写了一个简单的 PHP 脚本来根据 URL 中传递的参数从数据库中读取数据,它工作得很好。
这是脚本:
<?php
function updator($item)
{
$servername = "localhost";
$username = "yaddvirus";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$table = "inventory";
//$item = "Rose Almonds";
$sql = "SELECT * FROM $table WHERE item = '$item'";
$result = $conn->query($sql);
while($data=$result->fetch_assoc()){
echo "<h1>{$data['item']}</h1><br>";
echo "<h1>{$data['item_desc']}</h1><br>";
echo "<h1>{$data['price125']}</h1><br>";
echo "<h1>{$data['price250']}</h1><br>";
}
//echo "0 results";
$conn->close();
}
if (defined('STDIN')) {
$item = $argv[1];
} else {
$item = $_GET['item'];
}
//$item = "Cherry";
updator($item);
?>
这个脚本完全符合预期。我使用 http://nutsnboltz.com/tester.php?item=itemname 调用它,它可以很好地提取和显示数据。
P.S You can test it out by using Cherry or Blueberry as items.
问题是,当我试图将这些数据放入我的 productpage.php 文件时,我无法显示这些数据。文件层次结构如下:
<php
*Exact same php script as above*
?>
<html>
<head>
Header and navbar come here
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-4">
<h1> RANDOM TEXT BEFORE </h1>
<?php
while($data=$result->fetch_assoc()){
echo "<h1>{$data['item']}</h1><br>";
echo "<h1>{$data['item_desc']}</h1><br>";
echo "<h1>{$data['price125']}</h1><br>";
echo "<h1>{$data['price250']}</h1><br>";
}
?>
</div>
<div class="col-8">
<H!> MORE RANDOM TEXT</h1>
</div>
</div>
</div>
</body>
<footer>
footer here
scripts etc
</footer>
</html>
所以页脚上方的脚本打印一切正常。但是,在 HTML 所在的下方,在 PHP 代码之后没有打印任何内容。它只显示我的导航栏和 H1 标签说 "RANDOM TEXT BEFORE",仅此而已。我的页脚和其他所有东西都不见了。
这里到底是什么问题,我该如何解决?
问题似乎是您在 updator
函数中声明了 $result
,因此当您稍后尝试调用它时它不可用。
最好的办法可能是从函数中 return $result
并将其分配给变量 - 像这样:
function updator($item)
{
// ... some code ...
$sql = "SELECT * FROM $table WHERE item = '$item'";
$result = $conn->query($sql);
// ... some more code ...
return $result;
}
<-- HTML CODE HERE -->
<?php
$item = !empty($_GET['item']) ? $_GET['item'] : false;
// yes I know it's a bit hacky to assign the variable
// within the 'if' condition...
if($item && $result = updator($item)) {
while($data=$result->fetch_assoc()){
echo "<h1>{$data['item']}</h1><br>";
echo "<h1>{$data['item_desc']}</h1><br>";
echo "<h1>{$data['price125']}</h1><br>";
echo "<h1>{$data['price250']}</h1><br>";
}
}
?>
所以我在一个网站上工作,我需要从 MySQL 服务器中提取数据并将其显示在网页上。我写了一个简单的 PHP 脚本来根据 URL 中传递的参数从数据库中读取数据,它工作得很好。
这是脚本:
<?php
function updator($item)
{
$servername = "localhost";
$username = "yaddvirus";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$table = "inventory";
//$item = "Rose Almonds";
$sql = "SELECT * FROM $table WHERE item = '$item'";
$result = $conn->query($sql);
while($data=$result->fetch_assoc()){
echo "<h1>{$data['item']}</h1><br>";
echo "<h1>{$data['item_desc']}</h1><br>";
echo "<h1>{$data['price125']}</h1><br>";
echo "<h1>{$data['price250']}</h1><br>";
}
//echo "0 results";
$conn->close();
}
if (defined('STDIN')) {
$item = $argv[1];
} else {
$item = $_GET['item'];
}
//$item = "Cherry";
updator($item);
?>
这个脚本完全符合预期。我使用 http://nutsnboltz.com/tester.php?item=itemname 调用它,它可以很好地提取和显示数据。
P.S You can test it out by using Cherry or Blueberry as items.
问题是,当我试图将这些数据放入我的 productpage.php 文件时,我无法显示这些数据。文件层次结构如下:
<php
*Exact same php script as above*
?>
<html>
<head>
Header and navbar come here
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-4">
<h1> RANDOM TEXT BEFORE </h1>
<?php
while($data=$result->fetch_assoc()){
echo "<h1>{$data['item']}</h1><br>";
echo "<h1>{$data['item_desc']}</h1><br>";
echo "<h1>{$data['price125']}</h1><br>";
echo "<h1>{$data['price250']}</h1><br>";
}
?>
</div>
<div class="col-8">
<H!> MORE RANDOM TEXT</h1>
</div>
</div>
</div>
</body>
<footer>
footer here
scripts etc
</footer>
</html>
所以页脚上方的脚本打印一切正常。但是,在 HTML 所在的下方,在 PHP 代码之后没有打印任何内容。它只显示我的导航栏和 H1 标签说 "RANDOM TEXT BEFORE",仅此而已。我的页脚和其他所有东西都不见了。
这里到底是什么问题,我该如何解决?
问题似乎是您在 updator
函数中声明了 $result
,因此当您稍后尝试调用它时它不可用。
最好的办法可能是从函数中 return $result
并将其分配给变量 - 像这样:
function updator($item)
{
// ... some code ...
$sql = "SELECT * FROM $table WHERE item = '$item'";
$result = $conn->query($sql);
// ... some more code ...
return $result;
}
<-- HTML CODE HERE -->
<?php
$item = !empty($_GET['item']) ? $_GET['item'] : false;
// yes I know it's a bit hacky to assign the variable
// within the 'if' condition...
if($item && $result = updator($item)) {
while($data=$result->fetch_assoc()){
echo "<h1>{$data['item']}</h1><br>";
echo "<h1>{$data['item_desc']}</h1><br>";
echo "<h1>{$data['price125']}</h1><br>";
echo "<h1>{$data['price250']}</h1><br>";
}
}
?>