PHP数据库连接失败
我有一个php文件,其中包括两个函数,一个用于连接数据库,另一个用于为购物车设置cookied。这是文件:PHP数据库连接失败,php,mysql,Php,Mysql,我有一个php文件,其中包括两个函数,一个用于连接数据库,另一个用于为购物车设置cookied。这是文件: <?php $dbServer="localhost"; $dbName="test"; function ConnectToDb($server, $database){ $s=@mysql_connect($server); $d=@mysql_select_db($database, $s); if(!$s || !$d) return fals
<?php
$dbServer="localhost";
$dbName="test";
function ConnectToDb($server, $database){
$s=@mysql_connect($server);
$d=@mysql_select_db($database, $s);
if(!$s || !$d)
return false;
else
return true;
}
function GetCartId(){
if(isset($_COOKIE["cartId"])){
return $_COOKIE["cartId"];
}
else {
session_start();
setcookie("cartId", session_id(), time()+((3600*24)*30));
return session_id();
}
}
?>
用于连接数据库的函数在这个特定程序的另一个php文件中运行良好。我在此文件中遇到问题:
<?php
include("db.php");
switch($_GET["action"]) {
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item": {
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item": {
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default: {
ShowCart();
}
}
function AddItem($itemId, $qty) {
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
$cxn = @ConnectToDb($dbServer, $dbName);
// Check if this item already exists in the users cart table
$result = mysql_query("select count(*) from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0) {
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("insert into cs368_cart(cookieID, itemId, qty) values('" . GetCartID() . "', $itemId, $qty)");
}
else {
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}
function UpdateItem($itemId, $qty) {
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
$cxn = @ConnectToDb($dbServer, $dbName);
if($qty == 0) {
// Remove the item from the users cart
RemoveItem($itemId);
}
else {
mysql_query("update cs368_cart set qty = $qty where cookieID = '" . GetCartID() . "' and itemId = $itemId");
}
}
function RemoveItem($itemId) {
// Uses an SQL delete statement to remove an item from
// the users cart
$cxn = @ConnectToDb($dbServer, $dbName);
mysql_query("delete from cs368_cart where cookieID = '" . GetCartID() . "' and itemId = $itemId");
}
function ShowCart() {
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
$cxn = @ConnectToDb($dbServer, $dbName);
$result = mysql_query("select * from cs368_cart inner join cs368_products on cart.itemId =
items.itemId where cart.cookieID = '" . GetCartID() . "' order by items.itemName asc")
or die("Query to get test in function ShowCart failed with error: ".mysql_error());
?>
首先:丢失@,并在其中放入一些正确的错误处理(当出现错误时,这些函数返回false,您可以使用mysql\u error和mysql\u errno来记录它)
第二:mysql_real_escape_string和intval在那些$_GET参数上,在有人通过URL偷偷输入一些额外的代码之前
第三:将$dbServer和$dbName作为函数UpdateItem的局部变量访问,而不是作为脚本的全局变量访问。您应该只连接到数据库一次(在原始db.php文件中),并让查询函数处理其余部分(因为只有一个连接,它们都默认为该连接)。删除@符号,它会抑制错误,这样它会告诉您出了什么问题。避免使用jurassicmysql.*
,你知道为什么在PHP中使用@符号吗?!它'抑制错误消息!诊断您的问题的一个很好的起点是删除这些!到底是什么问题?您可能不会因为禁止而收到错误消息。删除代码中的所有@
,然后发布您收到的错误消息。您完全可以接受,我如何进行一次连接,以及查询文件如何访问数据库?使用mysql\u select\u db()
每个使用数据库的文件调用require\u一次('db.php')
而不是include
db.php
应该以主脚本中的mysql\u connect
和mysql\u select\u db
开头,而不是在另一个connect
函数中。这样,脚本一启动,就可以使用数据库连接。