MySQL problem in PHP
Posted: Wed Aug 03, 2011 2:05 am
I am trying to create a search bar which searches for users in the database, however as I have limited experience of MySQL in PHP and have never used foreach before (I am learning), I cannot see what the error is below:
Code: Select all
<?php
ob_start();
include("database.php");
echo <<<END
<div>
<FORM NAME ="form1" METHOD ="POST" ACTION = "search.php">
<INPUT TYPE = "Text" VALUE ="" NAME = "search">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search">
</div>
END;
if (isset($_POST['Submit1'])) {
$term = $_POST['search'];
echo $term;
echo "Hello";
$search = mysql_query("SELECT Username FROM 'notes_notes' WHERE Username LIKE '$term%'");
$search2 = mysql_fetch_array($search); # An error message complains about this line, saying 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 20
$search4 = $search2->Username;
echo $term;
echo $search4;
foreach ($search2 as &$value) { # And here, with 'Warning: Invalid argument supplied for foreach() on line 24'
$search3 = $value->Username;
echo <<<END
<div>
$search3
</div>
END;
}
unset($value);
} else {
echo "Failed";
}
?>