使用PDO没有数据库选择错误

时间:2014-03-27 23:32:56

标签: php mysql pdo

我正在尝试使用PDO连接到我的数据库,但是我遇到了这个错误:没有选择数据库

我正在阅读如何在http://www.php.net/manual/en/book.pdo.php使用,我认为每件事都是正确的。

用旧方法,一切正常,但是PDO我有这个错误。

那些有PDO经验的人知道我做错了什么?

旧方式:

<?php

define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','db_name');

$connect = mysql_connect(HOST,USER,PASS) or die('Error: <strong>'.mysql_error().'</strong>');
$db = mysql_select_db('db_name') or die('Error<strong>'.mysql_error().'</strong>');
?>

现在我正在尝试使用PDO:

<?php
    $dbHost = 'localhost';
    $dbUser = 'root';
    $dbPass = '';
    $dbName = 'db_name';

    try
      {
        $dbconn = new PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUser, $dbPass);

      }

    catch(PDOException $e)
      {
         echo $e->getMessage(); 
      }

        ?>

在我使用connect.php的php文件中我正在做这样的包含:

include('../crud/connection.php'); **//its the file with the connection**

在这个我使用connection.php的文件中,我正在使用pdo为登录系统做一个select语句。

我可以使用pdo建立连接,然后使用正常的sql语句吗?

我的想法也是将我的sql正常状态转换为PDO,但现在我已经与PDO建立连接,我想测试它是否正常工作,然后我会尝试将我的选择状态传递给pdo select statment!

我的登录代码:

 <?php if(isset($_POST['sendLogin']))
            {
                $f['email'] = mysql_real_escape_string($_POST['email']);
                $f['pass'] = mysql_real_escape_string($_POST['password']);




                if(!$f['email'] || !valMail($f['email']))  
                {
                  echo 'Email empty or invalid';
                }

                else if(strlen($f['pass']) <8 || strlen($f['pass'])>12)
                {
                    echo 'pass must have between 8 and 12 chars!';
                }

                else
                {
                    $autEmail = $f['email'];
                    $autpass = $f['pass'];
                    $query = "SELECT * FROM users where email= '$autEmail'";       

                    $exeqr = mysql_query($query) or die(mysql_error());
                    $assoc = mysql_fetch_assoc($exeqr);

                if(mysql_num_rows($exeqr) == 1 )
                {

                    if($autEmail == $assoc['email'] && $autpass == $assoc['pass'])
                    {
                        $_SESSION['assoc'] = $assoc;
                        header('Location:'.$_SERVER['PHP_SELF']);
                    }

                    else
                    {

                        echo ' wrong answer';
                    }


                }
                else
                {
                    echo 'email does no exist';
                }

                }
            }

我的HTML

<form name="login" action="" method="post">
            <label>
                <span>E-mail:</span>
                <input type="text" class="radius" name="email" value="<?php if(isset($f['email'])) echo $f['email']; ?>" />
            </label>
            <label>
                <span>Pass:</span>
                <input type="password" class="radius" name="senha" value="<?php if(isset($f['senha'])) echo $f['senha']; ?>" />
            </label>
            <input type="submit" value="Login" name="sendLogin" class="btn" />
</form>

4 个答案:

答案 0 :(得分:2)

您不能将mysql_函数与PDO一起使用。如果你想要执行那里的查询,你必须使用这样的东西:

http://www.php.net/manual/en/pdo.prepare.php

您问题的示例:

$query = 'SELECT * FROM users where email = :email';

$sth = $dbh->prepare($query);
$sth->execute(array(':email' => $autEmail));

答案 1 :(得分:0)

试试这个:

$dbconn = new PDO("mysql:dbname={$dbName};host={$dbHost}", $dbUser, $dbPass);

答案 2 :(得分:0)

而不是

  <?php
        $dbHost = 'localhost';
        $dbUser = 'root';
        $dbPass = '';
        $dbName = 'db_name';

        try
          {
            $dbconn = new PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUser, $dbPass);

          }

        catch(PDOException $e)
          {
             echo $e->getMessage(); 
          }

            ?>

试试这个

 <?php 
define('DB_host', 'localhost');
define('DB_username', 'root'); 
define('DB_password','');
define('DB_name','');

         try
           {
             $dbconn = new PDO("mysql:host=".DB_host.";dbname=".DB_name,DB_username,DB_password);

           }

         catch(PDOException $e)
          {
             echo $e->getMessage(); 
           }

            ?>

OK PER PHIL'S声明我建议只确保您的数据库存在..我已经尝试过使用您的代码并且它在我的最终工作.. =)

答案 3 :(得分:-2)

您正在将mysql函数与PDO函数混合使用。您需要执行以下操作:

  1. 保留mysql函数
  2. 更改所有代码以反映PDO功能。这包括所有mysql_ *函数。
  3. 如果这很容易做到,我建议#2,否则保持#1,除非你有其他原因需要切换。