调用未定义的函数php-mysql

调用未定义的函数php-mysql,php,mysql,Php,Mysql,我是新的编码和不断得到这个错误,我不知道原因是什么。。。我正在尝试从Mysql数据库中搜索/检索数据。。。其思想是,有人选择一个搜索类别,比如名字,输入名字,然后代码从数据库表Customers检索所有相关匹配项 我得到以下错误: 致命错误:在第36行的..../search.php中调用未定义的函数查询 有人能帮忙吗 <html> <head> <title>pageName</title> <style type="text

我是新的编码和不断得到这个错误,我不知道原因是什么。。。我正在尝试从Mysql数据库中搜索/检索数据。。。其思想是,有人选择一个搜索类别,比如名字,输入名字,然后代码从数据库表Customers检索所有相关匹配项

我得到以下错误:

致命错误:在第36行的..../search.php中调用未定义的函数查询

有人能帮忙吗

       <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>

您使用$query而不是query,它应该是函数而不是变量。这可能会解决你的问题,如果有更多的让我知道

$result = $query ($con, $query) or die ('error getting data from database');
应该是:

$result = query($con, $query) or die('error getting data from database');
$result = mysqli_query($query) or die ('error getting data from database');

您使用$query而不是query,它应该是函数而不是变量。这可能会解决你的问题,如果有更多的让我知道

$result = $query ($con, $query) or die ('error getting data from database');
应该是:

$result = query($con, $query) or die('error getting data from database');
$result = mysqli_query($query) or die ('error getting data from database');

您还有一个查询错误:

$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria  ."%'";
应该是:

$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria  ."%'";
应该是firstname,而不是$firstname

而循环没有结束括号 应该是这样的:

<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>

我在循环中移动了您的表标记,因为这是从循环生成表中的每一行的逻辑方式,但在某些示例中,您也可以循环整个表,因为您的html代码中会有很多表。

此外,您还有查询错误:

$query = "SELECT * FROM Customers WHERE $firstname LIKE '%" . $criteria  ."%'";
应该是:

$query = "SELECT * FROM Customers WHERE firstname LIKE '%" . $criteria  ."%'";
应该是firstname,而不是$firstname

而循环没有结束括号 应该是这样的:

<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>
我在循环中移动了您的表标记,因为这是从循环生成表中的每一行的逻辑方式,但在某些示例中,您也可以循环整个表,只是您将在html代码中获得许多表。

$result = $query ($con, $query) or die ('error getting data from database');
完全没有道理$查询是包含SQL指令的变量,而不是函数。变量$con还没有定义,至少在您展示的代码中没有定义。 此外,还应使用函数mysqli_query-

因此,这条线应该是:

$result = query($con, $query) or die('error getting data from database');
$result = mysqli_query($query) or die ('error getting data from database');
如果您使用的是过程样式,$con是mysqli_connect或mysqli_init返回的链接标识符,请使用:

$result = mysqli_query($con,$query) or die ('error getting data from database');
线路

$result = $query ($con, $query) or die ('error getting data from database');
完全没有道理$查询是包含SQL指令的变量,而不是函数。变量$con还没有定义,至少在您展示的代码中没有定义。 此外,还应使用函数mysqli_query-

因此,这条线应该是:

$result = query($con, $query) or die('error getting data from database');
$result = mysqli_query($query) or die ('error getting data from database');
如果您使用的是过程样式,$con是mysqli_connect或mysqli_init返回的链接标识符,请使用:

$result = mysqli_query($con,$query) or die ('error getting data from database');

我想你要找的是mysqli_query$con$query

尝试用mysqli\u查询替换查询函数调用

请注意,出于安全原因,mysqli_查询是为了支持mysqli_查询而进行的

请参见此处的文档:


如果这没有帮助,那么我建议检查您包含的“connect.php”中的代码。另外,获取错误的确切文本也会很有帮助,它将帮助我们调试您的代码。

我想您要查找的是mysqli\u query$con$query

尝试用mysqli\u查询替换查询函数调用

请注意,出于安全原因,mysqli_查询是为了支持mysqli_查询而进行的

请参见此处的文档:


如果这没有帮助,那么我建议检查您包含的“connect.php”中的代码。另外,获得错误的确切文本也会很有帮助,这将帮助我们调试您的代码。

假设您使用的是php mysql扩展,下面是解决问题的正确方法

<?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>'; ?>

假设您使用的是php mysql扩展,下面是问题的正确解决方案

<?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>'; ?>

尝试更改代码,使其看起来像这样

<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";
更改的简要说明:

在用户提交的变量中添加了“real\u escape\u string”函数 帮助防止sql注入 将查询中的“$firstname”更改为“firstname” 查询后取出“或死亡”部分,并使用mysqli_uuu函数 处理数据。
尝试更改代码,使其看起来像这样

<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";
更改的简要说明:

在用户提交的变量中添加了“real\u escape\u string”函数 帮助防止sql注入 将查询中的“$firstname”更改为“firstname” 查询后取出“或死亡”部分,并使用mysqli_uuu函数 处理数据。
代码不完整,能否提供完整代码和更多错误详细信息更新代码以显示您根据已给出的答案所做的更改。。您还可以接受SQL注入。了解connect.php中的内容或$con也是很有用的。代码不完整,能否提供完整的代码以及更多的错误细节?更新代码以显示您根据已给出的答案所做的更改。。您还可以接受SQL注入。了解connect.php中的内容或$con也是很有用的。感谢您的帮助。。。将$query更改为query并没有解决问题,不幸的是…@mush将其更改为mysql\u query$query,但我必须警告您mysql\u已被弃用,不再受支持。我建议使用mysqli_uu函数。@Diddl

埃多特。这似乎会导致:致命错误:无法在../search.php联机中的写入上下文中使用函数返回值35@Chris特鲁多,谢谢你。致命错误:在第36行的..../search.php中调用未定义的函数查询仍然发生。感谢您的帮助。。。将$query更改为query并没有解决问题,不幸的是…@mush将其更改为mysql\u query$query,但我必须警告您mysql\u已被弃用,不再受支持。我建议使用mysqli_uu函数。@DiddleDot。这似乎会导致:致命错误:无法在../search.php联机中的写入上下文中使用函数返回值35@Chris特鲁多,谢谢你。致命错误:在第36行的..../search.php中调用未定义的函数查询仍然发生。感谢您的评论。我已经包含了connect.php文件,其中包含$con=mysql\u connectDB\u HOST、DB\u USER、DB\u PSWD、DB\u NAME;是的,梅塔米洛和我都已经猜到了-我用第二行,然后。我在。。。是不是因为另一个代码是mysql而不是mysqli?我必须把它们全部改成mysqli吗?再次感谢。警告:mysqli_查询要求参数1为mysqli,第36行/search.php中给出的资源从数据库获取数据时出错mysql_查询在php 5.5.0中被弃用,在php 7.0.0中被删除。但现在它仍然有效,除非您已经使用PHP7。因此,要么使用函数mysql\u query,要么将所有内容都更改为mysqli。在此处比较:如果您的问题已解决,请标记为已回答。谢谢-谢谢你的评论。我已经包含了connect.php文件,其中包含$con=mysql\u connectDB\u HOST、DB\u USER、DB\u PSWD、DB\u NAME;是的,梅塔米洛和我都已经猜到了-我用第二行,然后。我在。。。是不是因为另一个代码是mysql而不是mysqli?我必须把它们全部改成mysqli吗?再次感谢。警告:mysqli_查询要求参数1为mysqli,第36行/search.php中给出的资源从数据库获取数据时出错mysql_查询在php 5.5.0中被弃用,在php 7.0.0中被删除。但现在它仍然有效,除非您已经使用PHP7。因此,要么使用函数mysql\u query,要么将所有内容都更改为mysqli。在此处比较:如果您的问题已解决,请标记为已回答。谢谢-这项工作做得很出色。还有一些建议改变的人也帮了大忙。由于我的初学者“无知”,我忘记了更改connect.php文件中$con=mysql\u connectDB\u HOST、DB\u USER、DB\u PSWD、DB\u NAME的设置;to$con=mysqli\u connectDB\u主机、DB\u用户、DB\u PSWD、DB\u名称;我忘了提到,当从mysql切换到mysqli时,它工作得非常出色。还有一些建议改变的人也帮了大忙。由于我的初学者“无知”,我忘记了更改connect.php文件中$con=mysql\u connectDB\u HOST、DB\u USER、DB\u PSWD、DB\u NAME的设置;to$con=mysqli\u connectDB\u主机、DB\u用户、DB\u PSWD、DB\u名称;我忘了提到,当从mysql切换到mysqli时,OP抱怨的底线是IMJ。可能还需要其他修复,但最主要的是说$query,而你指的是$mysqli_query。这是OP投诉的底线,IMJ。可能还需要其他修复,但最主要的是说$query,而不是说$mysqli\u query。