我只是想通过要求另一个带有连接功能的文件来连接我的数据库。我尝试了很多方法,但我总是得到一个空白页面或我的消息“执行失败”。希望有人指出我哪里出错了。感谢。
config.php
define('DB_HOST','host'); //DATABASE HOST
define('DB_USERNAME','username'); //DATABASE USERNAME
define('DB_PASSWORD','password'); //DATABSE PASSWORD
define('DB_NAME','name'); //DATABASE NAME
function connectDB(){
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(!$mysqli){
trigger_error ('Avon Maitland Schools Canada could not establish a connection to the database at this time. Please try again later or contact info@yourschoolsincanada.com to inquire about this issue.'.mysqli_connect_error());
}else{
return $mysqli;
}
}
然后我的注册文件
register.php
require_once('includes/config.php');
$db_connection = connectDB();
if(isset($_POST['register'])){
/*ALL MY POSTING OF VARIABLE AND VALIDATION IS HERE BUT NOT THE ISSUE*/
$sql = ("INSERT INTO members (username, email, password_1) VALUES (?,?,?)");
//Prepare our query
$stmt = $db_connection->prepare($sql) or die("Failed Execution");
//Can not proceed if we can not prepare the query
if(false===$stmt){
die('prepare() failed: ' . htmlspecialchars($db_connection->error));
}
//Bind the fields and there paramters to our query in our testing variable $next_step
$next_step = $stmt->bind_param('sss', $username, $email, $db_pass);
//If next_step is false then it didn't work and there is no sense of proceeding
if($false===$next_step){
die('bind_param() failed: ' . htmlspecialchars($db_connection->error));
}
//Place the Execute into a variable and test if it executed or not
$next_step = $stmt->execute();
//If next_step is false then it didn't work and there is no sense of proceeding
if(false===$next_step){
die('execute() failed: ' . htmlspecialchars($db_connection->error));
}
header('Location: redirect somewhere');
exit();
//Close the STMT Connection
$stmt->close();
}
我已经尝试将$ mysqli变量传递给函数但这不起作用。并尝试将Defines添加到函数中,并将它们传递给变量本身也没有运气。如果我没有得到一个空白页面它只是在准备就绪并且说“执行失败”。我很难过。
答案 0 :(得分:1)
使用以下代码替换die("Failed Execution");
..
$stmt = $db_connection->prepare($sql) or die($db_connection->error); //<-- Assuming $db_connection is a valid MYSQLi connection..
如果您提供自己的自定义错误消息,则很难找到错误。
答案 1 :(得分:0)
好的我明白了,我正在通过wordpress子主题构建这个应用程序。问题是我定义的大多数变量都与wp-config.php中的变量名称相同,所以它总是用错误的凭据等查找错误的地方。改变那些,它工作正常。感谢您的帮助,尤其是错误报告,这些都是我救的。再次感谢