www.webdeveloper.com
Results 1 to 2 of 2

Thread: PDO connection fetching from DB

  1. #1
    Join Date
    Jan 2014
    Posts
    8

    PDO connection fetching from DB

    Hi

    I'm trying to change my script from mysql connection to PDO and I managed to fetch data but there's an input box and based on the input fetched data is supposed to change but the results just disappear for me when i click 'search the table'
    PHP Code:
    <?php


    $sql 
    "SELECT * FROM apps ";
    try {
        
    $con = new PDODB_DSNDB_USERNAMEDB_PASSWORD ); 
         
    $con->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION );


    } catch ( 
    PDOException $e ) {
        echo 
    "I'm sorry there is a problem with your operation..";
        
    file_put_contents'dbErrors.txt'$e->getMessage(), FILE_APPEND );
    }


    if (isset(
    $_POST['search'])) {

        
    $search_term mysql_real_escape_string($_POST['search_box']);
        
    $sql .= "WHERE app_name = '{$search_term}' ";
        
    $sql .= " OR category = '{$search_term}'";
    }
        
    $stmt $con->prepare$sql );
        
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
        
    $stmt->execute();
    ?>
        <form name="search_form" method="POST" action="apps.php">
        Search: <input type="text" name="search_box" value="" />
        <input type="submit" name="search" value="Search the table" >
        </form>
                    <table width="90%" cellpadding="5" cellspace="5">
                        <tr>
                            <td>Name</td>
                            <td>Category</td>
                            <td>Details</td>
                        </tr>
    <?php     while( $row $stmt->fetch()) { ?>
                        <tr>
                            <td><?php echo $row['app_name']; ?></td>
                            <td><?php echo $row['category']; ?></td>
                            <td>Details</td>
                        </tr>
    <?php ?>
                    </table>

  2. #2
    Join Date
    Jan 2014
    Posts
    8
    ok i fixed the one above but i have a problem with passing a parameter to a template page as it doesn't display anything
    PHP Code:
    <?php



    try {
        
    $con = new PDODB_DSNDB_USERNAMEDB_PASSWORD ); 
         
    $con->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION );



    } catch (
    PDOException $e) {
                  echo 
    $e->getMessage(); //catch and show the error
    }

        
    $stmt $con->prepare"SELECT * FROM apps WHERE appID = :id" );
        
    $stmt->bindParam(":id"$_GET['id']);
        
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
        
    $stmt->execute();

    ?>
    <?php     
    while( $row $stmt->fetch()) { ?>


                    <h2><?php echo $row['app_name']; ?></h2>
                    <img src="<?php echo $row['picture']; ?>" alt="app image" />
                    <p>Category:<?php echo $row['category']; ?></p>
                    <p id="description"><?php echo $row['description']; ?></p>
                    <a href="<?php echo $row['link']; ?>">Download</a>
    <?php ?>

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