Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using MySQL and PHP to extract from a DB

    I really hope someone can help me because its driving me mad trying to understand what I need to do here.

    I have read the PDO and prepare statements manual, but just cant work out whats wrong with my code below:

    PHP Code:
    $dsn "mysql:host=localhost;dbname=maindb";
    $user "root";
    $password "mypass";
     try {
        
    $pdo = new PDO($dsn$user$password);
    } catch (
    PDOException $e) {
        echo 
    'Connection failed: ' $e->getMessage();
    }
    // Database  
    $page = isset($_GET['page']) ? $_GET['page'] : 'home';
    #$page = $pdo->real_escape_string($page);
    #$sql = $mysqli->query("SELECT * FROM url WHERE page = '" . $page . "' LIMIT 1");
    $stmt $pdo->prepare("SELECT * FROM url WHERE page = ? LIMIT 1");
    $stmt->execute(array($page));

    if (
    $pdo->connect_errno) {
        
    printf("Connect failed: %s\n"$pdo->connect_error);
        exit();
    }

    if (
    $stmt->num_rows 0) {
        while (
    $row $stmt->fetch_object())
        {
            
    $title $row->title;
            
    $description $row->descr;
            
    $keywords $row->keywords;

        }

    } else {

        
    $title 'Set Default Title';
        
    $description 'Set Default Description';
        
    $keywords 'Set Default Keywords';


    I would be really glad for any assistance

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,285
    Thanks
    12
    Thanked 343 Times in 339 Posts
    there is neither a PDO->connect_error nor a PDOStatement->num_rows nor a PDOStatement->fetch_object() property/method in PDO (these are all MySQLi properties/methods, PDO uses CamelCase).

    additionally, there is no need to use a loop for a single result set. and the catch() statement should be placed after all of the DB code.

    btw. you don’t even need to test the number of results. if there is no (more) result available PDOStatement->fetchObject() returns false, and since you can only have one result at most ...
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    This is what I use for row count and fetch data.

    Row count

    $stmt->rowCount();

    For fetch assoc

    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
    echo $row['yourVar'];
    }

    This might help you figure out what's wrong.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,285
    Thanks
    12
    Thanked 343 Times in 339 Posts
    Quote Originally Posted by tomharto View Post
    Row count
    $stmt->rowCount();
    this might not work since
    PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

    If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.
    (colouring by me)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    tomharto (05-14-2013)

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by Dormilich View Post
    this might not work since

    (colouring by me)
    Ahh I did not know that . I'm also new to PDO haha.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,285
    Thanks
    12
    Thanked 343 Times in 339 Posts
    Actually, with a little bit of sensible logic, the number of results in a SELECT statement do not need to be fetched separately. for most of the other cases, there is SQL’s COUNT() function.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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