Ryan
Ryan

Reputation: 61

Pull rows with only certain data in them PHP & Mysql

I'm trying to pull information and display it in a table form on a php page i have a mysql DB i have something like below which list everything in the table but i would like to have it only show certain information if a certain data is present

Columns i would like to display on the page

object_id, Name and, wish.

but i only want rows from the DB to be displayed if a column

wishState is ("pending")

this is what i have scoured around to find so far wich brings everything in.

$query="SELECT * FROM MY_TABLE";
$results = mysql_query($query);

while ($row = mysql_fetch_array($results)) {
echo '<tr>';
    foreach($row as $field) {
        echo '<td>' . htmlspecialchars($field) . '</td>';
    }
    echo '</tr>';
}

Thanks Ryan

Upvotes: 1

Views: 1455

Answers (2)

John Robertson Nocos
John Robertson Nocos

Reputation: 1485

The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So please don't use mysql_* anymore.

//selects data where wish is pending 
$query="SELECT * FROM `MY_TABLE` WHERE `wish`='pending'";
$results = mysqli_query($your_db_connection, $query) or exit(mysqli_error());

while ($row = mysqli_fetch_array($results)) {

//show only object_id, name and wish
echo '<tr>';
    echo '<td>' . $row['object_id'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['wish']. '</td>';
echo '</tr>';
}

Upvotes: 1

Bioto
Bioto

Reputation: 1117

$query="SELECT * FROM MY_TABLE WHERE `wish`='pending'";

That should work based on the information you gave us. WHERE tells the database to search for rows WHERE the column WISH is equal to "pending".

Upvotes: 0

Related Questions