[RESOLVED] another post about search + pagination - Page 5
Page 5 of 5 FirstFirst ... 345
Results 61 to 75 of 75

Thread: [RESOLVED] another post about search + pagination

  1. #61
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,619
    When I copy paste your code, and search for $search_result, I get to this part
    PHP Code:
    if (!$stmt->execute($params)) {
        
    $errors[] = 'There was a database error. Please contact the Webmaster of this site via our contact page if this problem persists,';  
    } else {
        
    # This returns all selected rows into an associative array
        
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC);        # $search_result is defined here? I dont't get it


        # lots of lines of code here... which is why indentation is so important...
    }

    # $search_result CAN be defined here, but it MUST not be
    # solution: only use $search_result where it MUST be defined (hint, inside the else block above)
    #            or define it as an empty array in the if (!...) block 

  2. #62
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    ok, so $search_result is not defined... worked with the hint you gave me I found the code block your are speaking about.

    correct?

    PHP Code:
     } else {
            
    # This returns all selected rows into an associative array
            
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC);
        
            
            
    $count_stmt->execute($params);
            
    # Whereas fetchAll fetches all rows at once, fetch fetches one row at the time
            # and here we only have one row anyway
            
    $row $count_stmt->fetch(PDO::FETCH_ASSOC);
            
    $rowcount $row['rowcount'];
            
            
    # Add search result number independently if that was 0 or any other number
            
    $feedback[] = sprintf('Your search found %d results'$numrows);
            
            if (
    $numrows == 0) {
                
    $feedback[] = sprintf('Sorry, your search: "%s" returned zero results. Please try again. If you need help please <a href="%s">click here.</a></p>'$sBASE HELPDIR 'search-help.php'); 
    where i am getting stuck is HOW to define it to be used in the output any read material you could recommend?



    something like this? Guessing at this point.


    which is why indentation is so important...
    is this better and the correct way ?
    PHP Code:

         <?php


    # I like collecting errors throughout the page to provide feedback
    # Same with other feedback, although I keep them separate
    $errors   = array();
    $feedback = array();

    /* First off, I'd try to get as many checks as possible into one single
     * For example, instead of checking both "no input" and "less than 3 characters" and informing
     * user of each one separately, just check string length and inform user: has to be at least 3 chars
     *
     * Also note that $_GET['SearchTerms'] will always be set if the form was posted properly,
     * even if the field was left empty. Thus, in the original case of !isset($_GET['SearchTerm'])
     * it would have been better with !empty($_GET['SearchTerm'])
     */

    # To make sure the array element is set, using the ternary operator ?:
    # Works like this
    # (is this value true) ? (then return this value) : (else return this value)
    $_GET['SearchTerms'] = !isset($_GET['SearchTerms']) ? '' trim($_GET['SearchTerms']);

    # Now that we've trimmed the search, check for string length using mb_strlen (you are using utf-8 aren't you?)
    if (mb_strlen($_GET['SearchTerms']) < 2) {
            
    $errors[] = "<p>Search terms must be longer than 2 characters. Please try your search again. ";
            
            
    } else {
            
    $s $_GET['SearchTerms'];
            
            
    // Checking SearchTerm for disallowed search terms
            # Easier way to check for these values in a more condensed form
            
    $word_check = array(
                    
    'bread',
                    
    'white',
                    
    'wheat',
                    
    'rye'
            
    );
            if (
    in_array($s$word_check)) {
                    
    $feedback[] = "<p>Your search term is too broad. Please enter a more specific search term. ";
            }
            
            
    $limit 10// rows to return
            
            
            // Build SQL Query
            
            
            # Connecting to the DB using PDO 
            
    $host   'mysite.com';
            
    $dbname 'nfacts';
            
    $dsn    "mysql:dbname=$dbname;host=$host";
            
    $db     = new PDO($dsn'user''password');
            
            
            
    $count_stmt $db->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
            
    # The query fetching the product links will also need "LIMIT offset, count"
            
    $stmt       $db->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count');
            
            
    # search here will replace :search in the query
            
    $count_params = array(
                    
    'search' => $s
            
    );
            
    # I put in fixed values for offset and count, leaving for you to deal with the pagination :)
            
    $params       = array(
                    
    'search' => $s,
                    
    'offset' => 10,
                    
    'count' => 20
            
    );
            
            
    # Then you use matching name, other_name (with or without :) as array keys to pass the data
            
    if (!$stmt->execute($params)) {
                    
    $errors[] = 'There was a database error. Please contact the Webmaster of this site via our contact page if this problem persists';
                    
                    
            } else {
                    
    # This returns all selected rows into an associative array
                    
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC);
                    
                    
                    
    ///// define $search_result here??? /////
                    
    $rows = array(
                            
    $search_results
                    
    );
                    
                    
                    
                    
    $count_stmt->execute($params);
                    
    # Whereas fetchAll fetches all rows at once, fetch fetches one row at the time
                    # and here we only have one row anyway
                    
    $row      $count_stmt->fetch(PDO::FETCH_ASSOC);
                    
    $rowcount $row['rowcount'];
                    
                    
    # Add search result number independently if that was 0 or any other number
                    
    $feedback[] = sprintf('Your search found %d results'$numrows);
                    
                    if (
    $numrows == 0) {
                            
    $feedback[] = sprintf('Sorry, your search: "%s" returned zero results. Please try again. If you need help please <a href="%s">click here.</a></p>'$sBASE HELPDIR 'search-help.php');
                            
                            
                            
                            
                    }
            }
    }

    # But before you get to actual content, let's give the user errors and feedback, if any
    # Which ought to start with errors
    foreach ($errors as $err) {
            echo 
    $err "<br />";
    }
    foreach (
    $feedback as $f) {
            echo 
    $f "<br />";
    }


    // now you can display the results returned


    foreach ($rows as $row) {
            
    printf($row['link']);
    }



    #  followed by pagination 
    # echo "here..." ;



    ?>

  3. #63
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    still same results? Any guides you can suggest to help me learn this?
    Last edited by darkangel2001lv; 11-30-2012 at 03:03 PM.

  4. #64
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,621
    Google is your friend?

    Here's the result with about 6 minutes of work and a fairly decent, but basic, text editor:
    PHP Code:
    <?php 

    # I like collecting errors throughout the page to provide feedback 
    # Same with other feedback, although I keep them separate 

    $errors   = array(); 
    $feedback = array(); 

    /* First off, I'd try to get as many checks as possible into one single 
     * For example, instead of checking both "no input" and "less than 3 characters" and informing 
     * user of each one separately, just check string length and inform user: has to be at least 3 chars 
     * 
     * Also note that $_GET['SearchTerms'] will always be set if the form was posted properly, 
     * even if the field was left empty. Thus, in the original case of !isset($_GET['SearchTerm']) 
     * it would have been better with !empty($_GET['SearchTerm']) 
     */ 

    # To make sure the array element is set, using the ternary operator ?: 
    # Works like this 
    # (is this value true) ? (then return this value) : (else return this value) 

    $_GET['SearchTerms'] = !isset($_GET['SearchTerms']) ? '' trim($_GET['SearchTerms']); 

    # Now that we've trimmed the search, check for string length using mb_strlen (you are using utf-8 aren't you?) 

    if (mb_strlen($_GET['SearchTerms']) < 2) { 

       
    $errors[] = "<p>Search terms must be longer than 2 characters. Please try your search again. ";         

    } else { 

       
    $s $_GET['SearchTerms']; 
             
       
    // Checking SearchTerm for disallowed search terms 
       # Easier way to check for these values in a more condensed form 
       
    $word_check = array( 
          
    'bread'
          
    'white'
          
    'wheat'
          
    'rye' 
       
    ); 

       if (
    in_array($s$word_check)) { 
          
    $feedback[] = "<p>Your search term is too broad. Please enter a more specific search term. "
       } 

       
    $limit 10// rows to return 

       // Build SQL Query 
       # Connecting to the DB using PDO  
       
    $host   'mysite.com'
       
    $dbname 'nfacts'
       
    $dsn    "mysql:dbname=$dbname;host=$host"
       
    $db     = new PDO($dsn'user''password'); 

       
    $count_stmt $db->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search'); 
       
    # The query fetching the product links will also need "LIMIT offset, count" 

       
    $stmt   $db->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count'); 

       
    # search here will replace :search in the query 

       
    $count_params = array( 
             
    'search' => $s 
       
    ); 
     
       
    # I put in fixed values for offset and count, leaving for you to deal with the pagination :) 

       
    $params       = array( 
          
    'search' => $s
          
    'offset' => 10
          
    'count' => 20 
       
    ); 
             
       
    # Then you use matching name, other_name (with or without :) as array keys to pass the data 
       
       
    if (!$stmt->execute($params)) { 

          
    $errors[] = 'There was a database error. Please contact the Webmaster of this site via our contact page if this problem persists'

       } else { 
     
          
    # This returns all selected rows into an associative array 

          
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC); 

          
    ///// define $search_result here??? ///// 

          
    $rows = array( 
             
    $search_results 
          
    ); 
                     
          
    $count_stmt->execute($params); 

          
    # Whereas fetchAll fetches all rows at once, fetch fetches one row at the time 
          # and here we only have one row anyway 

          
    $row      $count_stmt->fetch(PDO::FETCH_ASSOC); 
          
    $rowcount $row['rowcount']; 
                     
          
    # Add search result number independently if that was 0 or any other number 

          
    $feedback[] = sprintf('Your search found %d results'$numrows); 
                     
          if (
    $numrows == 0) { 
     
             
    $feedback[] = sprintf('Sorry, your search: "%s" returned zero results. Please try again. If you need help please <a href="%s">click here.</a></p>'$sBASE HELPDIR 'search-help.php'); 
       
          } 
    //end if "numrows" 
          
       
    //end else statement executed succesfully 

    //end else searchterms > 2 

    # But before you get to actual content, let's give the user errors and feedback, if any 
    # Which ought to start with errors 

    foreach ($errors as $err) { 
       echo 
    $err "<br />"

    foreach (
    $feedback as $f) { 
       echo 
    $f "<br />"


    // now you can display the results returned 

    foreach ($rows as $row) { 
       
    printf($row['link']); 


    #  followed by pagination  
    # echo "here..." ; 

    ?>
    Last edited by dalecosp; 11-30-2012 at 03:44 PM.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  5. #65
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,619
    PHP Code:
    # This returns all selected rows into an associative array
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC);
                    
                    
    ///// define $search_result here??? /////
    $rows = array(
        
    $search_results
    ); 
    Well, if $search_result may end up not being defined, then obviously neither will $row. There are only 3 ways to go:
    1. Do everything you need to with the var where it is is defined
    2. Define it as something resonable in the other if block (or before both blocks), such as empty array, null, false
    3. Check if it's set when you need to use it at a later point (if isset(...))


    And just to be certain, I hope you realize that if and else are always mutually exclusive
    PHP Code:
    if ($something)
    {
        
    $var true;
    }
    else
    {
        
    # $var not defined
    }

    /* We don't have to know the value of $something to know several things about the above code:
    - Exactly one of the above blocks was sexecuted
    - We cannot know if $var has been defined (without knowing the value of $something)

    Truthfully, those two statements aren't "several things", they are one since they are actually equivalent.
    And because of that, the two following statements are also equivalent
    */
    # once again checking if $something is true
    if ($something)
    {
        
    # and then user $var
        
    echo $var;
    }

    # checking if $var is set
    if (isset($var))
    {
        echo 
    $var;

    But if you want to echo $var in case it's been defined like we do in the last block, for reasons of code readability, do not use if ($something) ONLY to ceck if $var is set. Instead, use if isset($var) - This tells YOU exactly what is going on.

    With the code structure I proposed in my code, I'd probably proceed the first if check (that is, at the very top) with $search_result = array(); Now you know it's defined, everywhere. Once you get to the file dealing with output, it'll somewhere contain
    PHP Code:
    foreach ($search_result as $row
    and that works perfectly fine with an empty array. This can either mean no search was done, no results found or perhaps a database error. Then the feedback and error variables are used for additional feedback: 0 rows found, "An error occurred" etc.

  6. #66
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by dalecosp View Post
    Google is your friend?

    Here's the result with about 6 minutes of work and a fairly decent, but basic, text editor:
    thanks, I did search on google and just didn't understand it completely. looking at your verision and mine i can see what i did wrong with indentation. Thanks now that i see the right way to do it i will work on my messy codes lol. so much easier to follow tho

  7. #67
    Senior Member
    Join Date
    Feb 2012
    Posts
    125

    PDO Database connection problem

    Hey guys, sorry be reading up on a few things, got most of the issues fixed I think. Only is error that is coming up is this

    Error displayed is this: added comments so you know that message is from two different outputs

    Connected to database <--- Connection Good, This message is from Connection file script
    There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists
    ^-------- From results.php page. page We are working on
    PHP Code:
    if (!$stmt->execute($params)) {
            
    $errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists'
    I tested connection by running connection file nfacts.php by itself and seems to connect based on message displayed "Connected To Database".

    [/PHP]

    Here is the results.php page that is having the problem Full reworked code
    PHP Code:
    #
    #Script Ver 13 12/3/12
    #
    #
    # I like collecting errors throughout the page to provide feedback
    # Same with other feedback, although I keep them separate
    $errors = array();
    $feedback = array();
    $search_result = array();

    /* First off, I'd try to get as many checks as possible into one single
     * For example, instead of checking both "no input" and "less than 3 characters" and informing
     * user of each one separately, just check string length and inform user: has to be at least 3 chars
     *
     * Also note that $_GET['SearchTerms'] will always be set if the form was posted properly,
     * even if the field was left empty. Thus, in the original case of !isset($_GET['SearchTerm'])
     * it would have been better with !empty($_GET['SearchTerm'])
     */

    # To make sure the array element is set, using the ternary operator ?:
    # Works like this
    # (is this value true) ? (then return this value) : (else return this value)
    $_GET['SearchTerms'] = !isset($_GET['SearchTerms']) ? '' trim($_GET['SearchTerms']);

    # Now that we've trimmed the search, check for string length using mb_strlen (you are using utf-8 aren't you?)
    if (mb_strlen($_GET['SearchTerms']) < 2) {
        
    $errors[] = "<p>Search terms must be longer than 2 characters. Please try your search again. ";
        
        
    } else {
        
    $s $_GET['SearchTerms'];
        
        
    // Checking SearchTerm for disallowed search terms
        # Easier way to check for these values in a more condensed form
        
    $word_check = array(
            
    'bread',
            
    'white',
            
    'wheat',
            
    'rye'
        
    );
        if (
    in_array($s$word_check)) {
            
    $feedback[] = "<p>Your search term is too broad. Please enter a more specific search term. ";
        }
        
        
    $limit 10// rows to return
        
        
        // Build SQL Query
        
        
        # Connecting to the DB using PDO 
    include('includes/connections/nfacts.php'); 
        
        
    // Display message if successfully connect, otherwise retains and outputs the potential error
    try {
      
    $conn = new PDO("mysql:host=$hostdb; dbname=$namedb"$userdb$passdb);
      echo 
    'Connected to database';
    }
    catch(
    PDOException $e) {
      echo 
    $e->getMessage();
    }
        
        
    $count_stmt $conn->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
        
    # The query fetching the product links will also need "LIMIT offset, count"
        
    $stmt $conn->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count');
        
        
    # search here will replace :search in the query
        
    $count_params = array(
            
    'search' => $s
        
    );
        
    # I put in fixed values for offset and count, leaving for you to deal with the pagination :)
        
    $params = array(
            
    'search' => $s,
            
    'offset' => 10,
            
    'count' => 20
        
    );
        
        
        
        
    # Then you use matching name, other_name (with or without :) as array keys to pass the data
        
        
        
        
    if (!$stmt->execute($params)) {
            
    $errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists';
            
            
        } else {
            
    # This returns all selected rows into an associative array
            
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            
    $count_stmt->execute($params);
            
    # Whereas fetchAll fetches all rows at once, fetch fetches one row at the time
            # and here we only have one row anyway
            
    $row $count_stmt->fetch(PDO::FETCH_ASSOC);
            
    $rowcount $row['rowcount']; 
            
            
    # Add search result number independently if that was 0 or any other number
            
    $feedback[] = sprintf('Your search found %d results'$numrows);
            
                echo 
    'Database query passed however isset($search-result) was empty. Please look at that section of code';

            
            if (
    $numrows == 0) {
                
    $feedback[] = sprintf('Sorry, your search: "%s" returned zero results. Please try again. If you need help please <a href="%s">click here.</a></p>'$sBASE HELPDIR 'search-help.php');
                
                
                
                
            }
        }
    }

    # But before you get to actual content, let's give the user errors and feedback, if any
    # Which ought to start with errors
    foreach ($errors as $err) {
        echo 
    $err "<br />";
    }
    foreach (
    $feedback as $f) {
        echo 
    $f "<br />";
    }


    // now you can display the results returned


    foreach($search_result as $row) {
          echo 
    $row['link']. '<br />';

    }


    #  followed by pagination 
    # echo "here..." ; 
    I know issue is here but I am missing it

    PHP Code:
    include('includes/connections/nfacts.php'); 
        
        
    $count_stmt $conn->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
        
    # The query fetching the product links will also need "LIMIT offset, count"
        
    $stmt $conn->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count');
        
        
    # search here will replace :search in the query
        
    $count_params = array(
            
    'search' => $s
        
    );
        
    # I put in fixed values for offset and count, leaving for you to deal with the pagination :)
        
    $params = array(
            
    'search' => $s,
            
    'offset' => 10,
            
    'count' => 20
        
    ); 
    And Connection File

    PHP Code:
    // Connection data (server_address, database, username, password)
    $hostdb 'mysite.com';
    $namedb 'mydatabase';
    $userdb 'myusername';
    $passdb 'mypassword';

        
    // Display message if successfully connect, otherwise retains and outputs the potential error
    try {
      
    $conn = new PDO("mysql:host=$hostdb; dbname=$namedb"$userdb$passdb);
      echo 
    'Connected to database';
    }
    catch(
    PDOException $e) {
      echo 
    $e->getMessage();

    Last edited by darkangel2001lv; 12-03-2012 at 03:02 PM.

  8. #68
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,771
    For debugging you can use the PDOStatement's errorInfo() method to get a description of what the DBMS didn't like.
    PHP Code:
           $errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists';
    // only for debugging...
          
    $errorInfo $stmt->errorInfo();
          
    $errors[] = $errorInfo[2]; 
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  9. #69
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by Weedpacket View Post
    For debugging you can use the PDOStatement's errorInfo() method to get a description of what the DBMS didn't like.
    PHP Code:
           $errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists';
    // only for debugging...
          
    $errorInfo $stmt->errorInfo();
          
    $errors[] = $errorInfo[2]; 
    Thanks, Weedpacket

    this is the results that were reported

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''10', '20'' at line 1


    my MySQL version is 5.0 best hosting company offers - sucks I know but don't have a choice.
    tried using phpmyadmin to troubleshoot query but it will not accept it

    syntax error is here

    PHP Code:
    $count_stmt $conn->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
        
    # The query fetching the product links will also need "LIMIT offset, count"
        
    $stmt $conn->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count');
        
        
    # search here will replace :search in the query
        
    $count_params = array(
            
    'search' => $s
        
    );
        
    # I put in fixed values for offset and count
        
    $params = array(
            
    'search' => $s,
            
    'offset' => 10,
            
    'count' => 20); 

  10. #70
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    The PDOStatement::execute() method (man: pdostatement.execute) has a default behavior for the input parameters argument that will prevent you from simply passing in your parameters:

    Quote Originally Posted by PHP Manual
    All values are treated as PDO::PARAM_STR.
    The last two arguments, offset and count, are not strings - they are integers.

    As such, you'll have to replace this:
    PHP Code:
        # I put in fixed values for offset and count 
        
    $params = array( 
            
    'search' => $s
            
    'offset' => 10
            
    'count' => 20); 
    with three calls to PDOStatement::bindparam() (man: pdostatement.bindparam) to set each parameter equal to the appropriate value using the appropriate type (which, for the last two, will be PDO::PARAM_INT).

  11. #71
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by bradgrafelman View Post
    The PDOStatement::execute() method (man: pdostatement.execute) has a default behavior for the input parameters argument that will prevent you from simply passing in your parameters:

    The last two arguments, offset and count, are not strings - they are integers.

    As such, you'll have to replace this:
    PHP Code:
        # I put in fixed values for offset and count 
        
    $params = array( 
            
    'search' => $s
            
    'offset' => 10
            
    'count' => 20); 
    with three calls to PDOStatement::bindparam() (man: pdostatement.bindparam) to set each parameter equal to the appropriate value using the appropriate type (which, for the last two, will be PDO::PARAM_INT).
    ok I don't really understand, but its starting point to know what should be looking for. If possible can you explain this a little more Thanks bradgrafelman.

  12. #72
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Quote Originally Posted by bradgrafelman View Post
    The PDOStatement::execute() method (man: pdostatement.execute) has a default behavior for the input parameters argument that will prevent you from simply passing in your parameters:

    The last two arguments, offset and count, are not strings - they are integers.

    As such, you'll have to replace this:
    PHP Code:
        # I put in fixed values for offset and count 
        
    $params = array( 
            
    'search' => $s
            
    'offset' => 10
            
    'count' => 20); 
    with three calls to PDOStatement::bindparam() (man: pdostatement.bindparam) to set each parameter equal to the appropriate value using the appropriate type (which, for the last two, will be PDO::PARAM_INT).
    ok I don't really understand, but its starting point to know what should be looking for. If possible can you explain this a little more Thanks bradgrafelman.

  13. #73
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    Ok work in progress I know code is not finished, got stuck
    this is what i have so far

    PHP Code:
        # Connecting to the DB using PDO 
    include('includes/connections/nfacts.php'); 

        
    $offset 10;
        
    $count 20 ;

        
    $count_stmt $conn->prepare('SELECT COUNT(*) AS rowcount FROM products WHERE keywords like :search');
        
    # The query fetching the product links will also need "LIMIT offset, count"
        
    $stmt $conn->prepare('SELECT link, keywords FROM products WHERE keywords like :search ORDER BY keywords LIMIT :offset, :count');
        
        
    $stmt->bindValue(':search'$sPDO::PARAM_STR);
        
    $stmt->bindValue(':offset', (int) $offsetPDO::PARAM_INT);
        
    $stmt->bindValue(':count', (int) $countPDO::PARAM_INT);  

        
        
    $count_params = array(
            
    'search' => $s
        
    );
        
        
        
        if (!
    $stmt->execute($params); {
            
    $errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists';
            
    // only for debugging...
          
    $errorInfo $stmt->errorInfo();
          
    $errors[] = $errorInfo[2];  
            
            
        } else {
            
    # This returns all selected rows into an associative array
            
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC); 
    this part is left from previous version don't know how to correct it to work with the changes that were made.
    PHP Code:
    $count_params = array(
            
    'search' => $s
        
    );
        
        
        
        if (!
    $stmt->execute($params); {
            
    $errors[] = 'There was a database error, (!$stmt->execute($params)) did not execute. Please contact the Webmaster of this site via our contact page if this problem persists';
            
    // only for debugging...
          
    $errorInfo $stmt->errorInfo();
          
    $errors[] = $errorInfo[2];  
            
            
        } else {
            
    # This returns all selected rows into an associative array
            
    $search_result $stmt->fetchAll(PDO::FETCH_ASSOC); 
    Are the changes right or did I completely not get what bradgrafelman was saying ?
    Last edited by darkangel2001lv; 12-04-2012 at 03:38 PM.

  14. #74
    Senior Member
    Join Date
    Feb 2012
    Posts
    125
    bump

  15. #75
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,619
    Yes, that's what he meant. Although, after binding, you don't need to send params to execute(), i.e.
    $db>execute();

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
  •