Results 1 to 7 of 7

Thread: mysql deprecated to pdo query issues

  1. #1
    Join Date
    Aug 2013
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default mysql deprecated to pdo query issues

    Hello everyone, this mysql_deprecated works very fine and retrieves information but when i tried
    moving it to PDO it displays nothing. can someone help me

    thank you

    working mysql_deprecated.php


    PHP Code:
    <?php
    require('config.php');
        
    $user $_SESSION['log']['username'];
        
    $sql  "SELECT * FROM friends WHERE username='$user' OR friend='$user' AND status = 'accepted'";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_assoc($result))
    {

    echo 
    'fine';

    if (
    $row['friend'] != $user) { $friend $row['friend']; } else { $friend $row['username']; }

    $query  "SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE 
    (username = '
    $friend' OR username = '$user') ORDER BY post_id DESC LIMIT 5";
    $result mysql_query($query);

    while(
    $row mysql_fetch_assoc($result))

    {    

    echo 
    'data retrieved successfully';
    }
    }

    ?>

    pdo.php

    PHP Code:
    <?php
    $db 
    = new PDO (
        
    'mysql:host=localhost;dbname=testing'
        
    'root'// username
        
    '' // password
    );
    $user $_SESSION['log']['username'];
    $sql $db->prepare('
                SELECT * FROM users
                WHERE username= ?  OR friend = ? and status = accepted'
    );
     

    $sql->bindParam(1$userPDO::PARAM_STR);
    $sql->bindParam(2$userPDO::PARAM_STR);
    $sql->execute();

     while(
    $row $sql->fetch()) 
    {

    echo 
    'fine';

    if (
    $row['friend'] != $user) { 
    $friend $row['friend']; 
    } else { 
    $friend $row['username']; 
    }


    $query $db->prepare('
        SELECT *,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM post WHERE 
    (username = :username OR username = :username1) ORDER BY post_id DESC LIMIT 5

    '
    );
            
    $query->execute(array(
                
    ':username' => $friend,
                              
    ':username1' => $user ));    
                     
    $result $query->fetch();

                      while (
    $row $result->fetch()) 
                     
                           {
    echo 
    'data retrieved successfully';
    }
    }

    ?>
    Last edited by traq; 10-08-2013 at 08:17 PM.

  2. #2
    Join Date
    Aug 2013
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the table is users not friends

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by mutago View Post
    the table is users not friends
    Is that your solution? Or do you still have a problem?

    If it's still not working, please explain a little more - what result are you getting, and how is it wrong? what errors are you getting from PHP? from MySQL?

  4. #4
    Join Date
    Aug 2013
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the problem still remain. I think there is a mis conversion somewhere.
    The PDO code is not working. i need help please

  5. #5
    Join Date
    Aug 2013
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    PDO is supposed to query database records or better displays " querry successful" on like the working mysql deprecated counter part. I think where i have problem is in sql statement
    $sql = "SELECT * FROM friends WHERE username='$user' OR friend='$user' AND status = 'accepted'"; as variable $user is used for username and friend simultaneously

    and here also while($row = mysql_fetch_assoc($result)) during PDO conversion

  6. #6
    Join Date
    Aug 2013
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Resolved. Thank you. I used ? instead on name parameters. again i quoted accepted in the sql statement

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    what about errors? Do you have error reporting enabled? Once you enable error reporting, are you getting errors, or is everything running fine and you're just getting an unexpected result?

    This:
    PHP Code:
    while ($row $result->fetch()) 
    {
         echo 
    'data retrieved successfully';

    suggests that—since you're not seeing that message—your query was successful and simply matched no rows.

    Edit: whoops, didn't see your reply before I posted mine. Glad you solved it!

    If your question has been answered, please mark your thread "resolved":
    • On your original post (post #1), click [edit], then click [go advanced].
    • In the "thread prefix" box, select "Resolved".
    • Click [save changes].
    Last edited by traq; 10-08-2013 at 08:28 PM.

Similar Threads

  1. Replies: 16
    Last Post: 07-24-2013, 09:55 PM
  2. Replies: 2
    Last Post: 10-29-2009, 03:57 PM
  3. Need help with MySQL query...
    By vern1271 in forum MySQL and other databases
    Replies: 0
    Last Post: 08-06-2008, 04:51 PM
  4. pls help me on MySQL query......
    By Rai_87 in forum MySQL and other databases
    Replies: 3
    Last Post: 05-26-2008, 05:34 AM
  5. mySQL query?!
    By nikomou in forum MySQL and other databases
    Replies: 3
    Last Post: 03-11-2007, 04:47 PM

Bookmarks

Posting Permissions

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