-5

I have a query that takes inputs from the user and searches through the database.

Now depending on the inputs the result size can vary.

Query

$sql = "SELECT * FROM `$drop` WHERE `Stop Name` = '$input'";
$result = mysqli_query($conn, $sql);

Result

$rows = $result->num_rows; 

for ($j = 0 ; $j < $rows ; ++$j) {
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);

    //Display Results

    for ($j = 0 ; $j <= 124 ; ++$j)  echo "<td>$row[$j]</td>"; 
}

this gives me an Undefined offset error for when their is less than 124 variables (Most that could possibly be displayed) to show.

Where in the code do I add the @ to ignore these errors.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
  • 4
    Why not just use a `foreach` loop instead? Also, never ignore errors, solve them. – Epodax May 13 '16 at 12:53
  • 1
    Don't use @ to hide error messages, try to solve the errors as first! – pes502 May 13 '16 at 12:53
  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 13 '16 at 13:02

1 Answers1

4

You should not simply suppress errors like that.

Instead, stop looping when you have reached the end of the array:

for ($j = 0 ; $j < count($row) ; ++$j)  echo "<td>$row[$j]</td>";

Side note: you are using the same iterator $j for both loops. That won't work as expected.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94