1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Search Result Problem

Discussion in 'PHP' started by sykes3d, Feb 8, 2013.

  1. #1
    I created a simple search system, but when it goes show the search results, it doesn’t work because it keeps in infinite loop and show only the first line the table. What it takes to show several results using the structure below (PDO):

    if(!empty($_POST['nome']))
        {   
        $query = "
                SELECT
                    *
                FROM np_outdoor
                WHERE
                    nome LIKE :nome ORDER BY nome DESC
            ";
     
            $query_params = array(
                ':nome' => '%'.$_POST['nome'].'%'
            );
     
    try
            {
     
                $stmt = $db->prepare($query);
                $result = $stmt->execute($query_params);
            }
            catch(PDOException $ex)
            {
            die("Failed to run query: " . $ex->getMessage());
            }
     
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
    }
     
    if ($row == '0')
            {
                        echo("nothing");
            }
                else
                while ($line = $row) {
                echo $line['nome'];
            }
    PHP:
    I did it !!!
    SEMrush
     
    Solved! View solution.
    Last edited: Feb 8, 2013
    sykes3d, Feb 8, 2013 IP
    SEMrush
  2. #2
    Uhm... you're not fetching a count, you're fetching an entire row as an ARRAY, so that ==0 makes no sense... nor does your WHILE statement make any sense since you're just copying the already copied row to $line -- that shouldn't even do anything.

    What I THINK you are trying to do is this:
    
    if (!empty($_POST['nome'])) try {
    
    	$statement = $db->prepare('
    		SELECT * from np_outdoor
    		WHERE nome LIKE :nome
    		ORDER BY nome DESC
    	');
    
    	$result = $statement->execute(array(
    		':nome' => '%' . $_POST['nome'] . '%'
    	));
    
    } catch(PDOException $e) {
    	die('Failed to run query: ' . $e->getMessage());
    }
    
    if ($row = $statement->fetch()) {
    
    	do {
    		echo $row['nome'];
    	} while ($row = $statement->fetch());
    	
    } else echo 'nothing';
    
    Code (markup):
    Notice I ditched all the unnecessary variables which just wasted memory for nothing.
     
    deathshadow, Feb 8, 2013 IP
  3. sykes3d

    sykes3d Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I can't believe I didn't realize it. Thanks for your time!!!
     
    sykes3d, Feb 10, 2013 IP