-3

I'm very new to PHP and I'm trying to get some code that someone else has written to work. As the title says it is only showing the first result when it should be displaying many and I have no idea how to sort it (even though I imagine it's quite simple!). The code is below:

$result = mysqli_query($connection, ("SELECT id FROM details where publicposition like '%$trimvar%'  or familyorgname like '%$trimvar%' or commercialorgind like '%$trimvar%' ORDER BY id "));
if (!$result)
{
    die('Could not run query');
    }


echo "<h1>Your staff conflict search on <font color='#ff0000'>'$trimvar'<font color='#000'> returned <font color='#ff0000'>$rows<font color='#000'> results</h1>";



$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
{ 

    echo "<table style='width: 20%; border='0';'>"; 
    echo "<tr>";
    echo "<th> Record ID : " . $row['id'] . "</th><br/>";
    echo "</tr>";

}
echo "</table>";

Any help on this would be hugely appreciated!

Dharman
  • 30,962
  • 25
  • 85
  • 135
MattPH
  • 1
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Oct 05 '22 at 11:42

1 Answers1

0

mysqli_result::fetch_array -- mysqli_fetch_array — Fetch the next row of a result set as an associative, a numeric array, or both

Try using mysqli_fetch_array inside a while loop to fetch one row at a time until the end is reached.

while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    echo "<tr>";
    echo "<th> Record ID : " . $row['id'] . "</th>";
    echo "</tr>";
}