www.webdeveloper.com
Results 1 to 5 of 5

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

  1. #1
    Join Date
    Jun 2013
    Posts
    33

    mysql_* to PDO ...pls help.

    At first i used this code to connect to database.

    Code:
    <?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.

    Code:
    <?php
    
    // 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?
    Help.
    Thnks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2013
    Posts
    221
    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:

    http://wiki.hashphp.org/PDO_Tutorial...SQL_Developers

    Hope this helps.
    strad solutionswww.stradsolutions.com

  4. #4
    Join Date
    Jun 2013
    Posts
    33
    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.
    Code:
    $e_check = $_POST['email'];
    
    $email_query = "SELECT email FROM users WHERE email = '$e_check'";
    
    $check = $db_con->query($email_query);
    and

    Code:
    $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 07:21 AM.

  5. #5
    Join Date
    Feb 2013
    Posts
    64
    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



Recent Articles