嘿,我正在试图找出我的sql查询是否失败。我想要它,如果它使用下面的代码失败重定向到表单页面:
$checkconnection = mysql_connect('localhost', $dbuser, $dbpass)
or die();
if(!$checkconnection)
{
$_SESSION['errormsg'] = "<div style='padding-left: 50px;color:#FF0000'>Cannot connect to specfied database!</div>";
header("Location: install.php");
}else{
echo('Connection Successful!');
}
使用它只是说:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'nzcraftn_admin'@'localhost' (using password: YES) in /home/nzcraftn/public_html/phishnet/install/install_submit.php on line 17
答案 0 :(得分:4)
试试这个
$checkconnection = @mysql_connect('localhost', $dbuser, $dbpass)
它会隐藏默认错误并触发您自己的
答案 1 :(得分:3)
mysql_connect
的返回值为false仅表示失败。如果它返回FALSE
,or die()
表达式将退出php脚本。这就是为什么你不输出它的任何输出的原因。
删除or die()
命令,并在if( !$checkconnection )
子句中显示实际错误。可以使用mysql_error()
检索报告的错误。
答案 2 :(得分:2)
它只显示警告,因为or die()
没有输出任何内容(空参数列表)。试试这个:
<?php
//Start the session
session_start();
//Do the conntection
$checkconnection = @mysql_connect('localhost', $dbuser, $dbpass);
//Check if it's valid
if(!$checkconnection) {
//Add it up to the session, and redirect
$_SESSION['errormsg'] = "<div style='padding-left: 50px;color:#FF0000'>Cannot connect to specfied database!</div>";
session_write_close();
header("Location: install.php");
exit();
} else{
//Yay
echo('Connection Successful!');
}
?>
答案 3 :(得分:1)
创世记的答案只是压制了警告,但仍然有效
如果你想要'干净',你可以尝试/捕捉错误:
(直接来自php.net/mysql_connect上的评论:
// Assign variables
global $db_connection, $db_server, $db_database, $db_username, $db_password;
$db_server = $server;
$db_database = $database;
$db_username = $username;
$db_password = $password;
// Attempt connection
try
{
// Create connection to MYSQL database
// Fourth true parameter will allow for multiple connections to be made
$db_connection = mysql_connect ($server, $username, $password, true);
mysql_select_db ($database);
if (!$db_connection)
{
throw new Exception('MySQL Connection Database Error: ' . mysql_error());
}
else
{
$CONNECTED = true;
}
}
catch (Exception $e)
{
echo $e->getMessage();
}