dti3914 2013-12-26 17:44
浏览 126

“没有选择数据库”问题

My developing environment is based on EasyPHP 12 and PHPdesigner on windows XP SP3. I am working on a registration / login solution, and I successfully completed the "sign up" part of it with a very basic script that used to work fine. For some reason I had to switch machine and, when I copied all the pertinent files back to my first station, I have got the "No database selected" message. I have tried everything to fix the issue, including creation of a brand new DB with only one table so far (users). My Connection file (db_connection.php) is as follows:

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;

and it seems to work fine.

Then, going back to my index.php file, it looks like the following validation lines are creating issues that I am not able to fix:

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; 
            }

it looks like the script is stuck at the "$query" step, where I get the error message.

I have tried several approaches to pinpoint the issue but still not able to understand where the problem is. I am also able to connect to the database (hf_databank) without a problem, database structure looks ok. Any help would be really appreciated !

  • 写回答

3条回答 默认 最新

  • dongshipang8094 2013-12-26 17:47
    关注

    in the second part you are using mysql_ and to connect you use mysqli_

    use mysqli_ also for your query

    $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);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?