windows 尝试使用 PhP、MySQL 使用 WAMP 对外部文件进行函数调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/984164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to make an function call to an external file with PhP, MySQL using WAMP
提问by
I'm sure there is a simple answer to this, but I have been fumbling with everything for almost a week now and surrender. I am trying to build a shopping cart app and every coding solution I build will work when I include the code on the same page, but when I try to use an external page to run the function it does not seem to return the data. I have tried various monitoring techniques to determine what it is happening.
我确信对此有一个简单的答案,但是我已经将近一个星期的时间摸索着一切并投降了。我正在尝试构建一个购物车应用程序,当我将代码包含在同一页面上时,我构建的每个编码解决方案都将起作用,但是当我尝试使用外部页面运行该函数时,它似乎没有返回数据。我尝试了各种监控技术来确定正在发生的事情。
Here is the code for the main page:
这是主页面的代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cart Connection</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Will this display?</p>
<p><a href='<?php echo "showCart.php?PHPSESSID=" . session_id() ?>'>View Cart</a></p>
<?php
$Database = "L3TtL2B5DdY";
$Table = "tblProds";
if (isset($_SESSION['curCart']))
$Cart = unserialize($_SESSION['curCart']);
else
{
if (class_exists("shoppingCart"))
{
$Cart = new shoppingCart();
$Cart->setDatabase($Database);
echo ("<p>If statement ran successfully</p>");
}
else
exit("<p>The shoppingCart class is not available!");
}
$Cart->setTable($Table);
$Cart->getProductList();
$_SESSION['curCart'] = serialize($Cart);
?>
<p><a href='<?php echo "showCart.php?PHPSESSID=" . session_id() ?>'>View Cart</a></p>
</body>
</html>
Here is the relevant code on the "shoppingCart.php" page:
这是“shoppingCart.php”页面上的相关代码:
<?php
class shoppingCart
{
private $dbConn = "";
private $dbName = "";
private $tableName = "";
private $orders = array();
private $orderTable = array();
function _construct()
{
$this->dbConn = @new mysqli("localhost", "root", "");
if (mysqli_connect_errno())
die("<p>Unable to connect to the database server.</p>" . "<p>Error Code " .
mysqli_connect_errno() . ": " . mysqli_connect_error() . "</p>");
}
public function setDatabase($Database)
{
$this->dbName = $Database;
@$this->dbConn->select_db($this->dbName)
Or die("<p>Unable to select the database.</p>" . "<p>Error code " . mysqli_errno($this->dbConn) .
": " . mysqli_error($this->dbConn) . "</p>");
}
public function setTable($Table)
{
$this->tableName = $Table;
}
public function getProductList()
{
$sqlString = "SELECT prodID, prodName, prodPrice FROM $this->tableName";
@$qryResult = $this->dbConn->query($sqlString)
Or die("<p>Unable to perform the query.</p>" . "<p>Error code " . mysqli_errno($this->dbConn) .
": " . mysqli_error($this->dbConn) . "</p>");
echo "<table width='100%' border='1'>";
echo "<tr><th>Product ID</th><th>Product Name</th><th>Product Price</th><th>Select Item</th></tr>";
$row = $qryResult->fetch_row();
do
{
echo "<tr><td>{$row[0]}</td>";
echo "<td>{$row[1]}</td>";
echo "<td>{$row[2]}</td>";
echo "<td><a href='showCart.php?PHPSESSID=" . session_id() . "&operation=addItem&productID=" . $row[0] .
"'>Add</a></td></tr>";
$row = $qryResult->fetch_row();
} while ($row);
echo "</table>";
}
......
?>
When I try to load the main page it will display the two
当我尝试加载主页时,它将显示两个
线,就是这样。我在第一次创建代码时调试了所有错误,并认为它会起作用。当我写这个页面的原始版本时,我将“连接”代码放在同一页面上,表格显示正常,所以我不知道还有什么可能。我在 Windows XP 机器上安装了 WAMP,它似乎工作正常。我还没有触及任何程序的配置文件,我所有的其他测试代码似乎都运行良好。只是当我尝试联系外部文件时。任何帮助将不胜感激,因为我认为我的大脑正在变成糊状。谢谢回答by Tom Haigh
You probably need to include the ShoppingCart.php file in your main page, so it has the definition of the ShoppingCart class. Try putting at the top of your main page:
您可能需要在主页中包含 ShoppingCart.php 文件,因此它具有 ShoppingCart 类的定义。尝试放在主页的顶部:
<?php require('ShoppingCart.php'); ?>
What I think might be happening is that the cart object is getting unserialised from the Session, but there is no class definition, so it becomes an instance of an incomplete class. When you then call a method on it you are getting a fatal error. What probably doesn't help is that you may not be displaying errors, so the script will just end. You could also try putting at the top of the main page:
我认为可能会发生的是,购物车对象正在从会话中反序列化,但没有类定义,因此它成为不完整类的实例。然后当你调用一个方法时,你会得到一个致命的错误。可能没有帮助的是您可能不会显示错误,因此脚本将结束。您也可以尝试放在主页的顶部:
<?php ini_set('display_errors', true); ?>
This should make PHP errors get shown.
这应该会显示 PHP 错误。
Edit
编辑
It might be worth pointing out that you can't store a database connection in the session. You need to connect to the server / select the database etc. on every request. I don't think your script is currently doing that.
值得指出的是,您不能在会话中存储数据库连接。您需要在每次请求时连接到服务器/选择数据库等。我不认为你的脚本目前正在这样做。
Also, you can store objects in the session without worrying about the serialisation yourself, here is a quick example:
此外,您可以在会话中存储对象而不必担心自己的序列化,这是一个快速示例:
<?php
//include class definition before starting session.
require('ShoppingCart.php');
session_start();
if (!isset($session['cart'])) {
$session['cart'] = new ShoppingCart();
}
$cart = $session['cart'];
//do stuff to cart
$cart->doSomething();
//changes are now saved back to the session when the script is terminated, without you having to do anything.
回答by Byron Whitlock
You need to
你需要
include_once("ShoppingCart.php");
Read up on the different ways to include files
阅读包含文件的不同方法