gpt4 book ai didi

php - "No database selected"问题

转载 作者:行者123 更新时间:2023-11-29 13:21:29 24 4
gpt4 key购买 nike

我的开发环境是基于windows XP SP3上的EasyPHP 12和PHPdesigner。我正在开发一个注册/登录解决方案,并且我使用一个非常基本的脚本成功完成了它的“注册”部分,该脚本曾经运行良好。由于某种原因,我不得不切换机器,当我将所有相关文件复制回我的第一个工作站时,我收到“未选择数据库”消息。我已尝试一切方法来解决该问题,包括创建一个迄今为止只有一张表(用户)的全新数据库。我的连接文件(db_connection.php)如下:

DEFINE ('DB_USER', 'superuser'); 
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'hf_databank');

// Make the connection:

$db_connect = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Check connection
if (mysqli_connect_errno($db_connect)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Let us select the database (just in case you want to change it):

$db_select = mysqli_select_db ($db_connect, DB_NAME) OR die;

而且看起来效果很好。

然后,回到我的 index.php 文件,看起来以下验证行正在创建我无法修复的问题:

include_once '../includes/db_connection.php';
include_once '../includes/functions.php';

$action = array();
$action['result'] = null;
$text = array();


//check if the form has been submitted

if(isset($_POST['signup'])){

//cleanup the variables and prevent mysql injection (this uses the function "clean" defined in functions.php file)

$username = clean($_POST['username']);
$password = clean($_POST['password']);
$email = clean($_POST['email']);


//Validation 1: Let’s say the user submits the form without a username. Our statement is going to run the
//code below.
if(empty($username)){ $action['result'] = 'error'; array_push($text,'Please set a valid username'); }
if(empty($password)){ $action['result'] = 'error'; array_push($text,'Please define a password'); }
if(empty($email)){ $action['result'] = 'error'; array_push($text,'Please set a valid email address'); }




$query = mysql_query("SELECT * FROM `users` WHERE username = '$username' OR email = '$email'") or die (mysql_error()); // mySQL Query
$row = mysql_num_rows($query); // Checks to see if anything is in the db.


if ($row > 0) {
$action['result'] = 'error'; array_push($text,'existing username/email;
}

看起来脚本停留在“$query”步骤,我在其中收到错误消息。

我尝试了多种方法来查明问题,但仍然无法理解问题出在哪里。我还能够毫无问题地连接到数据库(hf_databank),数据库结构看起来不错。任何帮助将非常感激 !

最佳答案

在第二部分中,您使用 mysql_ 并使用 mysqli_ 进行连接

也使用 mysqli_ 进行查询

$query = mysqli_query($db_connect,"SELECT * FROM `users` WHERE username = '$username' OR email = '$email'") or die (mysqli_error()); // mySQL Query    
$row = mysqli_num_rows($query);

关于php - "No database selected"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20788891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com