Results 1 to 5 of 5

Thread: mysql_* to PDO ...pls help.

  1. #1
    Join Date
    Jun 2013

    mysql_* to PDO ...pls help.

    At first i used this code to connect to database.

    <?php // db_connect.php
    $host = 'localhost';
    $user = 'user';
    $pass = 'password';
    function db_connect($db='') {
        global $host, $user, $pass;
        $dbcn = @mysql_connect($host, $user, $pass)
            or die('The site database appears to be down.');
        if ($db!='' and !@mysql_select_db($db))
            die('The site database is unavailable.');
        return $dbcn;
    I used to call the db_connect("DB name") function from other scripts.

    And now i want to shift to PDO.
    so i did this.

    // db_connect.php
    $host = 'localhost';
    $user = 'user';
    $pass = 'password';
    function db_connect($db='') {
        global $host, $user, $pass;
    try {
        $dbh = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
        /*** echo a message saying we have connected ***/
        echo 'Connected to database';
    catch(PDOException $e)
        echo $e->getMessage();
    return $dbh;
    But..i doesnt work..."Database is not selected error".
    Is there any other method to connect to DB using PDO and separate function for DB connection?

  2. #2
    Join Date
    Aug 2004
    Is the "...not selected" error coming from the exception in the function, or later when you try to actually use the returned PDO object?

    Have you double-checked the (case-sensitive) spelling of the DB name where you call this function?

    Have you sacrificed a live chicken to the database gods?

    PS: If you want to change the database selection, I believe you would just do it via PDO::query(), using whatever the MySQL syntax is for selecting a database.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Sep 2013
    Well, i think you have not mentioned your database name and not selected it.
    You can follow the below link to understand your PDO_MYSQL:


    Hope this helps.
    strad solutionswww.stradsolutions.com

  4. #4
    Join Date
    Jun 2013
    Thnks Nogdog and priyankagound.
    now, i'm off to change everything from mysql_* to PDO.
    First register script..
    Now, confusion.
    where to use PDO query and where to use prepared statements?
    In register script, i'm checking if email is already present....so PDO query or Prepared statement?

    and how to use prepared statements with $_POST[] variables?

    right now, i'm doing this for checking entered email against Db.
    $e_check = $_POST['email'];
    $email_query = "SELECT email FROM users WHERE email = '$e_check'";
    $check = $db_con->query($email_query);

    $add_member = $db_con->exec("INSERT INTO --query-- ");
    for registering member...
    should i use prepared statements here...or is this correct???

    I'm finding prepared statements difficult. If i should use them in above code..please tell me how to do it???
    Last edited by dhirajkumar.41; 10-08-2013 at 08:21 AM.

  5. #5
    Join Date
    Feb 2013
    You can use prepared statements for everything and you could do it this way or you could do it the other way. If the other way all you have to make sure you sanitize anything the user can enter.

    PHP Code:
    $e_check $_POST['email'];

    $email_query "SELECT email FROM users WHERE email=:email";

    $check $db_con->prepare($email_query);

    $check->execute(array(':email' => $e_check));

    if (
    $check && $check->rowCount() > 0) { // Check that rows were returned:
          // Continue on with arming or fetching actually data.....
    } else { // Problem!
    throw new Exception('Email not found!');

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center