user1571757
user1571757

Reputation: 3

mssql_fetch_array only displays one row if columns are put into variables

I'm still a PHP noob, so I apologize if this is something simple.

I am creating a fairly basic search facility for a website using PHP and mySQL. I have connected to the database, selected the database, queried the table and have fetched the table columns;

$k = htmlspecialchars($_GET['k']); // Get search query

$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");

if( mssql_num_rows($select) < 1) {

$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';

} else {

while ($results = mssql_fetch_array($select)) {

    $title = $results['Title'];
    $link = $results['Link'];
    $description = $results['Description'];

}

}

When I put the $results[''] columns into variables and then try to echo out each variable like so;

             if( isset($noResults)) {

                echo $noResults;

            } else { 

                echo '<li>' . '<h2>' . '<a href="' . $link . '" title="' . $title . '">' . $title . '</a>' . '</h2>' . '<p>' . $link . '</p>' . '<p>' . $description . '</p>' . '</li>'; 

             }

it only echo's out one row matching that query however, If I was to just simple echo out the columns like so;

    echo $results['Title'];
    echo $results['Link'];
    echo $results['Description'];

all rows matching the query will be displayed..

I'm not sure why this is happening. If someone could help me out that would be great!

Upvotes: 0

Views: 4372

Answers (3)

nTony
nTony

Reputation: 21

a few things are not clear from your question, but i am assuming that you are echo'ing the variables outside the loop since you are checking isset($noResults). that means you are reassigning the variables with new values in each loop of while. so ultimately you get the last one assigned to the variables. you have to either use an array to hold the values or echo it with in the loop.

Upvotes: 1

Adder
Adder

Reputation: 5868

You need to use a loop:

$k = mysql_real_escape_string($_GET['k']); // Get search query

$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");

if( mssql_num_rows($select) < 1) {

    $noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';

} else {
    $results= array();
    while ($result = mssql_fetch_array($select)) {
        $results[]= $result;
    }

}

if( isset($noResults)) {

    echo $noResults;

} else { 
    echo "<ul>";
    foreach($results as $result){
        echo '<li>' . '<h2>' . '<a href="' . $result['link'] . '" title="' . $result['title'] . '">' . $result['title'] . '</a>' . '</h2>' . '<p>' . $result['link'] . '</p>' . '<p>' . $result['description'] . '</p>' . '</li>'; 
    }
    echo "</ul>";
}

Upvotes: 2

Hayo
Hayo

Reputation: 240

Do you execute the output in the while-loop?

If you execute the while-loop and call the echo after that, each resultset will overwrite the previous, and the echo will output the last resultset which was fetched.

If you call the echo in the Loop, every result set will generate "his own" output line.

If you want to hold every resultset in a variable you can use an array, which is declared in front of the loop and gets filled in the loop.

Upvotes: 2

Related Questions