Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未获得查询mysql php的匹配结果_Php_Mysql - Fatal编程技术网

未获得查询mysql php的匹配结果

未获得查询mysql php的匹配结果,php,mysql,Php,Mysql,我试图在数据库中找到与我的查询匹配的结果,并将其放入我制作的表格中,但没有得到匹配结果。我在phpmyadmin中运行了查询,得到了所需的输出。我在$result变量上使用了var_dump,得到了以下内容:类型为(mysql result)的资源(4)。这是我的密码: <?php function renderSearchForm($search, $search_by, $search_by, $error){ ?> <!DOCTYPE HTML5> <head

我试图在数据库中找到与我的查询匹配的结果,并将其放入我制作的表格中,但没有得到匹配结果。我在phpmyadmin中运行了查询,得到了所需的输出。我在$result变量上使用了var_dump,得到了以下内容:类型为(mysql result)的资源(4)。这是我的密码:

<?php
function renderSearchForm($search, $search_by, $search_by, $error){
?>
<!DOCTYPE HTML5>
<head>
    <title>Search Query</title>
</head>
<body>
<?php
//display errors
    if($error != ''){
        echo '<div style="padding:4px; border:1px solid red; color:red;">' . $error . '</div>';
    }
?>

<form action="" method="post">
<div>
<p>Search For:</p>
<strong>Name: *</strong> <input type="text" name="search" value="<?php echo $search; ?>" /><br/>
<p>Search by:</p>
<input type="radio" name="search_by"
<?php if (isset($search_by) && $search_by=="firstname") echo "checked";?>
value="firstname"/>Firstname*
<input type="radio" name="search_by"
<?php if (isset($search_by) && $search_by=="surname") echo "checked";?>
value="surname"/>Surname*
<br><br>
<input type="submit" name="submit" value="Submit">
<p>* required</p>
</div>
</form>
</body>
</html>
<?php
}

//connect to db
include('connect_db.php');

//check if submitted
if (isset($_POST['submit'])) {
    $search = mysql_real_escape_string(htmlspecialchars($_POST['search']));
    $search_by = mysql_real_escape_string(htmlspecialchars($_POST['search_by']));

    //check if search is empty
    if (empty($_POST["search"])) {
         $error = "Error: Name is required";

         //error, display form
         renderSearchForm($search, $search_by, $search_by, $error);
         } 

         elseif
         // check if name only contains letters and whitespace
          (!preg_match("/^[a-zA-Z ]*$/",$search)) {
           $error = "Error: Only letters and white space allowed";

         //error, display form
         renderSearchForm($search, $search_by, $search_by, $error);
         }

         //check if radio button selected
         elseif (empty($_POST["search_by"])) {
         $error = "Error: Search_by is required";

         //error, display form
         renderSearchForm($search, $search_by, $search_by, $error);
         }else{
            //save data
            $query =  "SELECT * FROM members WHERE '$search_by' LIKE '%$search%'";
            $result = mysql_query($query)
            or die(mysql_error());
            var_dump($result);

            //display data from db
            echo "<table border='1' cellpadding='10'>";
            echo "<tr> <th>ID</th> <th>Firstname</th> <th>Surname</th> <th>Telephone</th> <th>Cell</th> <th>Address</th> <th></th> <th></th> </tr>";

            //loop through results of db and display in table
            while ($row = mysql_fetch_array($result)) {
                //echo contents in table
                echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['firstname'] . "</td>";
                echo "<td>" . $row['surname'] . "</td>";
                echo "<td>" . $row['telephone'] . "</td>";
                echo "<td>" . $row['cellphone'] . "</td>";
                echo "<td>" . $row['address'] . "</td>";
                echo "<td><a href='edit.php?id=" . $row['id'] . "'>Edit</a></td>";
                echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
                echo "</tr>";
             }

             echo "</table>";

             //This counts the number or results
             $anymatches=mysql_num_rows($result);  
             if ($anymatches == 0)  {  
             echo "Sorry, but we can not find an entry to match your query<br><br>";  
             }
             //And we remind them what they searched for
             echo "<b>Searched For:</b> " .$search. "<b> In: </b>" .$search_by;   
             }

}else{
    //if form not submitted, display form
    renderSearchForm('','','','');
}
?>

搜索查询
搜索:


名称:您需要删除$search\u by之间的单引号。试试这个:

"SELECT * FROM members WHERE $search_by LIKE '%$search%'"

谁投了这个票?这个错误是一个明显的报价问题。