我正在尝试从我的数据库返回结果。我在主页上有两个选项,一个显示全部,一个搜索。该节目一切正常,但当我搜索记录时,我知道它在那里返回没有匹配。
我已经尝试在清理后回显输入变量并将它们返回到页面以便输入它们并尝试在mysql中运行sql语句并且它可以工作。
我输了,帮忙。
我的索引页面,它选择输入
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>Music Search</title>
</head>
<body>
<div class="container">
<div class="header"><a href="index.php"><img src="IMAGES/LOGO.png" alt="Insert Logo Here" name="Insert_logo" width="" height="90" id="Insert_logo" style="background-color: #; display:block;" /></a>
<!-- end .header --></div>
<div class="content">
<h1 class="top-text"> Search for music you like!</h1>
<!--Search Form-->
<form action="searchMusic.php" method="get" class="searchform cf">
<input type="text" id="searchMusic" name="searchMusic" placeholder="Search by Name/Genre/Year/Label" required>
<button type="submit" id="searchBTN" name="searchBTN">Search</button>
</form>
<div class="btnContainer">
<form action="searchAllMusic.PHP" method="get" >
<button class="viewALL" type="submit" id="viewAll" name="viewAll">View All</button>
</form>
<form action="adminLogin.PHP" method="get" >
<button class="login" type="submit" id="login" name="login">Login</button>
</form>
</div>
<!--end search form-->
<!--Results Box-->
<div class="result-box">
</div>
<!--end results box-->
<!-- end .content --></div>
<div class="footer">
<p>©Derek Sweeney </p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
PHP代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>Music Search</title>
</head>
<body>
<div class="container">
<div class="header"><a href="index.php"><img src="IMAGES/LOGO.png" alt="Insert Logo Here" name="Insert_logo" width="" height="90" id="Insert_logo" style="background-color: #; display:block;" /></a>
<!-- end .header --></div>
<div class="content">
<h1 class="top-text"> Search for music you like!</h1>
<!--Search Form-->
<form action="searchMusic.PHP" method="get" class="searchform cf">
<input type="text" id="searchMusic" name="searchMusic" placeholder="Search by Name/Genre/Year/Label" required>
<button type="submit" id="searchBTN" name="searchBTN">Search</button>
</form>
<div class="btnContainer">
<form action="searchAllMusic.PHP" method="get" >
<button class="viewALL" type="submit" id="viewAll" name="viewAll">View All</button>
</form>
<form action="adminLogin.PHP" method="get" >
<button class="login" type="submit" id="login" name="login">Login</button>
</form>
</div>
<!--end search form-->
<!--Results Box-->
<div class="result-box">
<?php
//stores the search data
$musicQuery = $_REQUEST['searchMusic'];
//checks to see if it is empty or null
if(isset($musicQuery) && !empty($musicQuery)){
require('includes/dbconxMusic.php');
//escapes all special characters that could break database
$searchq = mysqli_real_escape_string($con, $musicQuery);
//searchq stores the cleaned up search data
//create a variable to store a wildcard SQL statement
$sql = mysqli_query($con, "SELECT * FROM music WHERE artist LIKE '%".$searchq."%' or year LIKE '%".$searchq."%' or album LIKE '%".$searchq."%' or genre LIKE '%".$searchq."%'");
}
//end statement
//if no data is inserted it will putput this
else{
echo("Please include search data");
//this will kill the connection
die;
}
//end else
//if it finds no matching data it informs the user and kills the DB connextion
if(mysqli_num_rows($sql) == 0){
echo("<p>No matches found! </p>");
die;
}
echo'<table><tr><th>Artist</th><th>Album</th><th>Year</th><th>Genre</th> <th>Record Label</th></tr>';
//while there are rows to return it will populate the table below
while($row = mysqli_fetch_array($sql)){
echo '<tr>';
echo'<td>' .$row['artist'].'</td>';
echo'<td>' .$row['album'].'</td>';
echo'<td>' .$row['year'].'</td>';
echo'<td>' .$row['genre'].'</td>';
echo'<td>' .'<img src="'.$row['artwork'].'">'.'</td>';
echo'</tr>';
}//end while loop
echo'</table>';
//closes connection when no more rows
mysqli_close($con);
?>
</div><!--end results box-->
</div> <!-- end .content -->
<div class="footer">
<p>©Derek Sweeney </p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
我的连接
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
//connects to my music database
$con=mysqli_connect("localhost","root","root","music");
//if it fails to connect it outputs this with an error number
if(mysqli_connect_errno()) {
echo "failed to connet to MYSQL:".mysqli_connect_error();
}
?>
</body>
</html>
添加了错误报告后,我看到了此消息
警告:mysqli_num_rows()要求参数1为mysqli_result,布尔值在第70行的H:\ UniServerZ \ www \ music.search \ searchMusic.php中给出
找不到匹配项!
这是我的第70行
if(mysqli_num_rows($sql) == 0)
答案 0 :(得分:0)
当您收到converts_time = time.strftime("%I").lstrip('0') + str(":") + str(stop_time.split(":")[0]) + time.strftime("%p")
消息时,很可能是与数据库的连接失败或查询无法运行。
您还可以在构建查询时简化所有字符串连接,如下面的代码所示,可以更轻松地查看树木。
在调用No matches found!
mysqli_query()
同样由于php调用连接脚本来做一些php工作,它不需要将所有HTML包裹起来。它有点像调用函数或子程序。
所以改成它
$sql = mysqli_query($con, "SELECT *
FROM `music`
WHERE `artist` LIKE '%$searchq%'
OR `year` LIKE '%$searchq%'
OR `album` LIKE '%$searchq%'
OR `genre` LIKE '%$searchq%'");
if ( $sql === FALSE ) {
echo mysqli_error($con);
exit;
}