Reputation: 165
I am new to coding and keep getting this error and I am not sure what the reason is... I am trying to search/retrieve data from a Mysql database... Idea is that someone selects a category of search (such as first name) and inputs a first name and then code retrieves all relevant matches from database table Customers
.
I am getting the following error:
Fatal error: Call to undefined function query() in ..../search.php on line 36
Can anyone help.
<html>
<head>
<title>pageName</title>
<style type="text/css">
table {
background-color: #ADD8E6;
border: 1px solid black;
font-family: Arial; font-size: 14px;
}
th {
text-align: left;
}
</style>
</head>
<body>
<h1>MEMBERS SEARCH</h1>
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>
<label> Search Category:
<select name="category">
<option value="firstname">First NAME</option>
<option value="lastname">Last NAME</option>
</select>
</label>
<label> Search Criteria:<input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
// connect to the DB
include('connect.php');
$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria ."%'";
$result = $query ($con, $query) or die ('error getting data from database');
$num_rows = mysql_num_rows ($result);
echo "$num_rows results found";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table>
<tr>
<td width="300" ><font face="Arial Black" size="2"><?php echo $row['firstname']?> <?php echo $row['lastname']?></font></td>
</tr>
</table>
<table>
<tr>
?>
</body>
</html>
Upvotes: 1
Views: 9569
Reputation: 744
Try changing your code to look like this.
<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true"/>
<label> Search Category:
<select name="category">
<option value="firstname">First NAME</option>
<option value="lastname">Last NAME</option>
</select>
</label>
<label> Search Criteria:<input type="text" name="criteria" /></label>
<input type="submit" />
</form>
<?php
if (isset($_POST['submitted'])){
// connect to the DB
include('connect.php');
$category = mysqli_real_escape_string($con, $_POST['category']);
$criteria = mysqli_real_escape_string($con, $_POST['criteria']);
$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria ."%'";
$result = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($result);
echo "$num_rows results found";
Brief explanation of changes:
Upvotes: 2
Reputation: 1348
Assuming you are using php mysql extension, below is the corrected solution to your problem.
<?php
if (isset($_POST['submitted'])){
include('connect.php');
$category = $_POST['category'];//I guess it is search category(e.g. firstname)
$criteria = strtolower($_POST['criteria']);
$query = "SELECT * FROM Customers WHERE LOWER({$category}) LIKE '%{$criteria}%'";
$result = mysql_query($query) or die (mysql_error());
$num_rows = mysql_num_rows($result);
echo "{$num_rows} results found";
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td width="300" >
<font face="Arial Black" size="2"><?php echo "{$row['firstname']} {$row['lastname']}"?></font>
</td>
</tr>
<?php } echo '</table>'; ?>
Upvotes: 0
Reputation: 187
The line
$result = $query ($con, $query) or die ('error getting data from database');
doesn't make sense at all. $query is a variable containing the SQL instructions, not a function. And the variable $con has not been defined, at least not in the code you've shown. Also, you should use the function mysqli_query() - http://php.net/manual/en/mysqli.query.php
So the line should be:
$result = mysqli_query($query) or die ('error getting data from database');
If you are using procedural style and $con is the link identifier returned by mysqli_connect() or mysqli_init(), use:
$result = mysqli_query($con,$query) or die ('error getting data from database');
Upvotes: 0
Reputation: 223
I think what you are looking for is mysqli_query($con, $query).
Try replacing your query() function call with mysqli_query()
Note that mysql_query() is depricated in favor of mysqli_query() for security reasons.
See the documentation for it here: http://php.net/manual/en/mysqli.query.php
If that doesn't help then I'd suggest examining the code in your 'connect.php' that you are including. Also it would be helpful to get the exact text of the error, it would help us debug your code.
Upvotes: 1
Reputation: 753
Also you have query mistake:
$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria ."%'";
Should be:
$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria ."%'";
Should be firstname and not $firstname.
Also
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
While loop doesnt have a closing brackets It should be like:
<table>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td width="300" ><font face="Arial Black" size="2"><?php echo $row['firstname']?> <?php echo $row['lastname']?></font></td>
</tr>
<table> <---- This is html mistake also remove it
<tr> <----- This is html mistake also remove it
<?php
}
?>
</table>
I moved your table tag around loop because this is logical way to every row from loop generates row in table, but in some examples you can loop also whole tables just you will get lots of tables in your html code.
Upvotes: 1
Reputation: 1437
You have $query instead of query, it should be a function not a variable. That may fix your problem, if there is more let me know.
$result = $query ($con, $query) or die ('error getting data from database');
should be:
$result = query($con, $query) or die('error getting data from database');
Upvotes: 1