SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PDO: Prepare multiple variables

    Hi I am new to PDO, and am learning its structure.
    How can I prepare a string with multiple choices from query strings to perform search on mysql? I understand the prepare statement and binding the string, but not sure how to create the string when there is nothing to bind ($_GET is empty) to list all data.

    This is my current working code.


    // Start Listing
    $query = "SELECT * FROM cars WHERE active='1' ";

    if($_GET['make']){ $query .= " AND `make`='".$_GET['make']."' "; }
    if($_GET['model']){ $query .= " AND `model`='".$_GET['model']."' "; }

    switch($_GET['view']){
    case "low": $query .= " ORDER BY value"; break;
    case "high": $query .= " ORDER BY value DESC"; break;
    default: break;
    }

    $query .= " LIMIT ".$shownum.", 10;";

    $result = mysqli_query($mysqli, $query);

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    702
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    Pretty much the same with pdo:

    PHP Code:
    $params = array('active' => 1);

    $sql =  'SELECT * FROM cars WHERE active=:active';

    if (isset(
    $_GET('make')))
    {
        
    $sql .= ' AND make = :make';
        
    $params['make'] = $_GET['make'];
    }
    if (isset(
    $_GET('model')))
    {
        
    $sql .= ' AND model = :model';
        
    $params['model'] = $_GET['model'];
    }
    $sql .= ' LIMIT :shownum,10';
    $params['shownum'] = $shownum;

    $stmt $pdo->prepare($sql);
    $rows $stmt->execute($params)->fetchAll(); 


Tags for this Thread

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
  •