-4

I will do where I can search in my database

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

My codes

require_once ('.. / config.php');
$ conn = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die (mysql_error ());
mysql_select_db ("DB_DATABASE", $ conn);
$ search = $ _POST ['search'];
$ sql = mysql_query ("select * from opi where model numbers like '% $ search%' or name like '% $ search%'", $ conn);
while ($ row = mysql_fetch_array ($ sql))**strong text**
{
echo "<tr>";

           echo "<td width='60px' height='10px' valign='top' align='left' class='borderdb'>". $ row ['number']. "</ td>";
       echo "</ tr>";
    }
Community
  • 1
  • 1
peter
  • 1
  • 2
    This code calls for a visit of [Little Bobby Tables](http://xkcd.com/327/) – MisterBla Sep 04 '13 at 13:50
  • There seems to be quite a lot of spacing in your code - in the config path and after `$` in your variables. Is that how you have your code? – halfer Sep 04 '13 at 13:50
  • 1
    Don't use `mysql_` functions. They are deprecated. Use [`PDO`](http://php.net/manual/fr/book.pdo.php) or [`mysqli`](http://php.net/manual/fr/book.mysqli.php) instead – Brewal Sep 04 '13 at 13:50
  • See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. – John Conde Sep 04 '13 at 13:50
  • Had you used google and typed in `Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in` you'd instantly know what's wrong. -1 for 0 effort to research. – N.B. Sep 04 '13 at 13:51
  • why do you have `where model numbers like`? What is the name of the column you are trying to search? There shouldn't be a space between `model numbers` – Nolan Knill Sep 04 '13 at 13:51
  • 1
    select * from opi where **model numbers** is likely to be the problem. – MisterBla Sep 04 '13 at 13:52
  • 1
    @halfer sorry about that, rollbacked. – Bora Sep 04 '13 at 13:52
  • @RichardA `% $ search%` also is – Brewal Sep 04 '13 at 13:53
  • @Brewal And every other `$ varname`. – MisterBla Sep 04 '13 at 14:12

1 Answers1

1
<?php
$conn = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die (mysql_error ());
mysql_select_db ("DB_DATABASE", $conn);
$search = $_POST ['search'];
$sql = mysql_query ("select * from opi where `model numbers` like '%$search%' or `name` like '%$search%'" ,$conn);
while ($row = mysql_fetch_array ($sql))
{
echo "<tr>";

           echo "<td width='60px' height='10px' valign='top' align='left' class='borderdb'>". $row ['number']. "</ td>";
       echo "</ tr>";
    }
?>
  1. Shouldn't be any spaces between dollar sign and php variable name.
  2. If your column name has space in it, enclose your column names properly
  3. Do NOT use mysql_* function. they're deprecated and no longer maintained. Use Mysqli or PDO.
  4. You're SQL statement has SQL injection. use mysql_real_escape_string or PDO.
undone
  • 7,787
  • 4
  • 43
  • 69