1

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>
k0ner
  • 1,086
  • 7
  • 20
mush
  • 165
  • 1
  • 8
  • code is not complete, can you provide us full code and also more detials of error – Standej Dec 03 '15 at 22:26
  • 1
    Update the code to show the changes you've made based on answers already given.. You are open to SQL injections as well. Knowing what is in `connect.php` or what `$con` is would also be useful.. – chris85 Dec 03 '15 at 22:44

6 Answers6

2

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:

  • Added 'real_escape_string' function to your user submitted variables to help prevent sql injection
  • Changed '$firstname' in your query to 'firstname'
  • Took out 'or die()' part after query and used mysqli_ functions to handle the data.
DiddleDot
  • 745
  • 5
  • 16
  • This worked brilliantly. Also others that suggested changes helped a lot. With my beginner 'ignorance', I forgot to change settings in the connect.php file from $con = mysql_connect(DB_HOST, DB_USER,DB_PSWD,DB_NAME); to $con = mysqli_connect(DB_HOST, DB_USER,DB_PSWD,DB_NAME); – mush Dec 03 '15 at 23:06
  • I forgot to mention that when switching from mysql_ to mysqli_ – DiddleDot Dec 03 '15 at 23:11
1

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');
Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20
  • Thanks for help... Changing $query to query hasn't solved the problem unfortunately... – mush Dec 03 '15 at 22:33
  • @mush Change it to `mysql_query($query)` , but I have to warn you that mysql_ is deprecated and no longer supported. I would suggest using mysqli_ functions. – DiddleDot Dec 03 '15 at 22:39
  • @DiddleDot. This seems to lead to: Fatal error: Can't use function return value in write context in ../search.php on line 35 – mush Dec 03 '15 at 22:41
  • @Chris Trudeau, thanks for that. The Fatal error: Call to undefined function query() in ..../search.php on line 36 still occurs. – mush Dec 03 '15 at 22:42
1

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.

Standej
  • 749
  • 1
  • 4
  • 11
1

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.

metamilo
  • 213
  • 2
  • 5
  • That's the bottom line for OP's complaint, IMJ. Other fixes may also be needed, but the main thing is saying $query when you mean $mysqli_query. – Topological Sort Dec 03 '15 at 23:20
0

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');
  • thanks for your comment. I have included the connect.php file which has $con = mysql_connect(DB_HOST, DB_USER,DB_PSWD,DB_NAME); – mush Dec 03 '15 at 22:44
  • Yes, that's what metamilo and me both have guessed already. :-D Use the second line, then. – Gabriel machts Dec 03 '15 at 22:46
  • I am getting this error after... Is it because the other code is mysql and not mysqli? Do i have to change them all to mysqli? Thanks again. Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /search.php on line 36 error getting data from database – mush Dec 03 '15 at 22:58
  • mysql_query is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. (http://www.php.net/manual/en/function.mysql-query.php). But for now it still works (unless you already use PHP 7). So either just use function mysql_query or change everything to mysqli. Compare here: http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php – Gabriel machts Dec 03 '15 at 23:03
  • Please mark as answered, if your problem is solved. Thanks! :-) – Gabriel machts Dec 05 '15 at 21:49
0

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>'; ?>
Sandeep Giri
  • 1,318
  • 1
  • 9
  • 4