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 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I need help preparing PDO SELECT.

    Code:
    if (isset($_GET['Country'])) {
    if ($_GET['Country']) {
    	
    $nr = $db->query("SELECT count(*)from test WHERE Country = '$_GET[Country]'")->fetchColumn();
    }
    }
    How would I go about preparing a statement like this where the value I am selecting is dynamic (As in user chosen)?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    The value, as in country?
    PHP Code:
    if ($stmt $pdo->prepare('SELECT count(*) FROM test WHERE Country = :country'))
    {
        
    $stmt->execute(array(':country' => $_GET['Country']));
        
    $count $stmt->fetchAll();

    Unless you mean dynamic userchosen as the properties to fetch, then the answer is no you cannot use binding for that (you cannot create a dynamic structure on a prepared statement).

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    The value, as in country?
    PHP Code:
    if ($stmt $pdo->prepare('SELECT count(*) FROM test WHERE Country = :country'))
    {
        
    $stmt->execute(array(':country' => $_GET['Country']));
        
    $count $stmt->fetchAll();

    Unless you mean dynamic userchosen as the properties to fetch, then the answer is no you cannot use binding for that (you cannot create a dynamic structure on a prepared statement).
    I mean the value in the Country field is user chosen.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    In the WHERE clause? Then yep that would be fine.

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    In the WHERE clause? Then yep that would be fine.
    Yes, thank you.


  •  

    Tags for this Thread

    Posting Permissions

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