0

OVERVIEW:

So, I'm working on a sort of "search script" to help users find data based upon 2 different types of data. They can search by Category or by Encoding grabbed from my database.

Problem:

The array only returns 1 row, while there are 3 (exact) duplicates in my database, problem still occurs if the entries are different.

Code Snippet:

<?php
$catagory = mysql_real_escape_string($_GET['c']);
$encode = mysql_real_escape_string($_GET['e']);
$query = "SELECT * FROM posts WHERE ";
if(!empty($catagory)) {
$query =$query . "catagory = '$catagory'" ;
}
if(!empty($encode)) {
    if(!empty($catagory)) {
        $query = $query . " AND " ;
    }
    $query = $query. "urlencode = '$encode'" ;
}

$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
if (empty($row)) {
    $message = "Sorry, there were no results";
    echo $message;
} ELSE {
    echo '<table>';
    echo '<thead>';
    echo '<th>Title</th><th>Posted</th><th>Address</th><th>Views</th>';
    echo '</thead>';
    echo '<tbody>';
    $now = date('Y-m-d H:i:s');
    while($row = mysqli_fetch_array($result)) {
        $title = htmlspecialchars($row['titlepre']);
        echo $row['titlepre'];
        $posted = dateDiff($row['date'],$now);
        $address = $row['address'];
        $views = $row['views'];
        $url = $row['url'];
        echo '<tr><td><a href ="'.$url.'">'.$title.'</a></td><td>'.$posted.'</td><td>'.$address.'</td><td>'.$views.'</td></tr>';
    }
    echo '</tbody>';
    echo '</table>';
}
?>

Database Information

| ID | address  | title | titlepre | content |   Catagory     |  url  | urlencode | views |  date
--------------------------------------------------------------------------------------------------
| 3  |[WITHHELD]| title |  title   | content | Rants - Things | HDyLkl | Scheme   |   1   |2013-11-28 |
| 5  |[WITHHELD]| title |  title   | content | Rants - Things | HDyLkl | Scheme   |   1   |2013-11-28 |

Now, I'm fairly new to PHP, so please bear with me if my code isn't 100% efficient.

1
  • Side note: Why do you have both a database connection with mysql and mysqli? Why don't you use prepared statements instead if using mysqli? Commented Nov 30, 2013 at 21:38

1 Answer 1

1

Every use of *mysqli_fetch_array* shifts off the record from the results set.

You have checking for results:

$row = mysqli_fetch_array($result);

So that first record of $results is being "deleted".

do{
    $title = htmlspecialchars($row['titlepre']);
    echo $row['titlepre'];
    $posted = dateDiff($row['date'],$now);
    $address = $row['address'];
    $views = $row['views'];
    $url = $row['url'];
    echo '<tr><td><a href ="'.$url.'">'.$title.'</a></td><td>'.$posted.'</td><td>'.$address.'</td><td>'.$views.'</td></tr>';
}while($row = mysqli_fetch_array($result)) 

Should be working.

But move to the PDO/mysqli objective. They have an iterator.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.