我开始创建与数据库的连接,我将数据输出到HTML TABLE中,所有代码都在“所有代码中的所有代码 - 这一项工作”下。正如它在标题中所说的那样,它的工作方式完全正常。
我现在要做的是基本上获得相同的输出,但这次将代码分成三个文件,一个用于连接,一个用于类和函数,一个用于HTML。
问题:我无法将“product_name”列传递给HTML表格。
我得到的错误:
“注意:未定义的变量:第22行/ testTwo.php中的v”
和
“注意:尝试在第22行的/testTwo.php中获取非对象的属性”
以下是所有代码:
1 - 一个文件中的所有代码 - 这一个工作
(同一个项目,但有三个文件)
1 - configTest2.php(数据库连接)
2 - classTwo.php(类和函数)
3 - testTwo.php(HTML)
一个文件中的所有代码 - 这一个工作
<html>
<body>
<?php
echo "<table style='border: solid 2px black; text-align:left;'>";
echo "<tr><th>Name</th><th>Price(From-To)</th><th>Stock</th></tr>";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "name";
echo "<td>";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT product_name, product_quantity, product_id, product_price_lowest, product_price_highest FROM table ORDER BY product_price_lowest ASC, product_price_highest DESC ");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($result as $v) {
echo "<td style='width:150px;border:1px solid black;'>";
echo "</td>";
echo '<tr>';
echo '<td>' .$v->product_name. "</td>";
echo '<td>' .$v->product_price_lowest. '-' .$v->product_price_highest. "</td>";
echo '<td>' .$v->product_quantity. "</td>";
echo '</tr>';
}
echo "</td>";
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
</body>
</html>
1 - configTest2.php(数据库连接)
<?php
session_start();
/* DATABASE CONFIGURATION */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'name');
define("BASE_URL", "http://localhost/"); // Eg. http://yourwebsite.com
function getTestdb()
{
$servername=DB_SERVER;
$username=DB_USERNAME;
$password=DB_PASSWORD;
$dbname=DB_DATABASE;
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
?>
2 - classTwo.php(类和函数)
<?php
class classTwo
{
public function twoFunction(
$productName,
$productQty,
$productId,
$productLow,
$productHigh)
{
try {
$conn = getTestdb();
$stmt = $conn->prepare(
"SELECT
product_name = :productName,
product_quantity = :productQty,
product_id = :productId,
product_price_lowest = :productLow,
product_price_highest = :productHigh
FROM table
ORDER BY product_price_lowest ASC, product_price_highest DESC
");
$stmt->bindParam("productName", $productName, PDO::PARAM_STR);
$stmt->bindParam("productQty", $productQty, PDO::PARAM_STR);
$stmt->bindParam("productId", $productId, PDO::PARAM_STR);
$stmt->bindParam("productLow", $productLow, PDO::PARAM_STR);
$stmt->bindParam("productHigh", $productHigh, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach ($result as $v) {
echo $v->product_name;
echo $v->product_price_lowest;
echo $v->product_price_highest;
echo $v->product_quantity;
}
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
}
$conn = null;
?>
3 - testTwo.php(HTML)
<?php
include("configTest2.php");
include("classTwo.php");
$classTwo = new classTwo();
?>
<html>
<body>
<br>
<br>
<table style='border: solid 2px black; text-align:left;'>
<tr><th>Name</th><th>Price(From-To)</th><th>Stock</th></tr>
<td>
<td style='width:150px;border:1px solid black;'>
</td>
<tr>
<td> <?php echo $v->product_name;?> </td>
<td> </td>
<td> </td>
<td> <button type="submit" onclick="askForSell"> Sell </button> </td>
<td> <button type="submit" onclick="askForBuy"> Buy </button> </td>
</tr>
</td>
</table>
<br>
</body>
</html>