View Full Version : Help PHP!
okies i keep gettin error message when am searching something.. here the link.. www.litelyrics.com/search.php (http://www.litelyrics.com/search.php) and here the code..
<?php
require("db.php");
if(isset($_POST['search'])){
$term = $_POST['term'];
$type = $_POST['type'];
if(!isset($term)){ echo("Invalid search term specified!"); exit; }
if($type == "artist"){
$query = mysql_query("SELECT * FROM `lyrics` WHERE `artist` = '$term' ;");
}elseif($type == "song"){
$query = mysql_query("SELECT * FROM `lyrics` WHERE `song` = '$term' ;");
}
$rows = mysql_fetch_array($query);
if(!$rows[id]){ echo("Your search for <b>$term</b> returned no results in the LiteLyrics database."); exit; }
echo("You where searching for <b>$term</b> in <b>$type</b>'s:<br />");
while($rows = mysql_fetch_array($query)){
echo("<a href=\"view.php?id=$rows[id]\">$rows[song] - $rows[artist]</a><br />");
}
}else{
echo("
Please note that the LiteLyrics Search currently only supports exact matches.
<br />
<form method=\"POST\">
Search: <input type=\"text\" name=\"term\" size=\"30\"> in <select name=\"type\"> <option value=\"artist\">Artists</option>
<option value=\"song\">Tracks</option>
</select> <input type=\"submit\" name=\"search\" value=\"Search\">
</form>");
}
?>
+REP for all helpers
dp_124
20-11-2007, 01:53 PM
$query = mysql_query("SELECT * FROM `lyrics` WHERE `song` = '$term' ;");
$query = mysql_query("SELECT * FROM `lyrics` WHERE `artist` = '$term' ;");
remove the first ";" on each of those lines, so it looks like.
$query = mysql_query("SELECT * FROM `lyrics` WHERE `song` = '$term' ");
$query = mysql_query("SELECT * FROM `lyrics` WHERE `artist` = '$term' ");
Invent
20-11-2007, 04:22 PM
remove the first ";" on each of those lines, so it looks like.Makes no difference. MySQL notices a ';' as a line breaker (end of query) so it wont prevent it from running.
Run:
<?php
require("db.php");
if(isset($_POST['search'])){
$term = $_POST['term'];
$type = $_POST['type'];
if(!isset($term)){ echo("Invalid search term specified!"); exit; }
if($type == "artist"){
$query = mysql_query("SELECT * FROM `lyrics` WHERE `artist` = '$term' ;")or die mysql_error();
}elseif($type == "song"){
$query = mysql_query("SELECT * FROM `lyrics` WHERE `song` = '$term' ;") or die mysql_error();
}
$rows = mysql_fetch_array($query);
if(!$rows[id]){ echo("Your search for <b>$term</b> returned no results in the LiteLyrics database."); exit; }
echo("You where searching for <b>$term</b> in <b>$type</b>'s:<br />");
while($rows = mysql_fetch_array($query)){
echo("<a href=\"view.php?id=$rows[id]\">$rows[song] - $rows[artist]</a><br />");
}
}else{
echo("
Please note that the LiteLyrics Search currently only supports exact matches.
<br />
<form method=\"POST\">
Search: <input type=\"text\" name=\"term\" size=\"30\"> in <select name=\"type\"> <option value=\"artist\">Artists</option>
<option value=\"song\">Tracks</option>
</select> <input type=\"submit\" name=\"search\" value=\"Search\">
</form>");
}
?>
Then tell me the error (if any). Oh and don't use this script for practical use until it's made secure.
</span>
dp_124
20-11-2007, 04:27 PM
Sorry I apologise, I have been doing Coldfusion for the last couple of months, and the query format is completely different.
Florx
20-11-2007, 08:13 PM
Also use LIKE so it doesn't only match exact :)
Baving
20-11-2007, 08:39 PM
<?php
include("db.php");
$avaliable_terms = array('artist','song');
if(isset($_POST['search'])) {
$term = addslashes($_POST['term']);
$type = addslashes($_POST['type']);
if(!in_array($type,$avaliable_terms)) $oh_no[] = "The search term doesn't match one of ours!";
$query = mysql_query("select * from lyrics where ".$type." like '%".$term."%' order by ".$type." asc");
if(mysql_num_rows($query)==0) $oh_no[] = "No results were found.";
if(isset($oh_no)) {
$i=0;
while($i<count($oh_no)) {
$oh_no_no .= $oh_no[$i]."<BR>";
$i++;
}
}
echo "You searched for ".$term."<BR><BR>";
if(isset($oh_no_no)) {
echo $oh_no_no;
die();
}
while($row = mysql_fetch_array($query)) {
echo "<a href='view.php?id=$row[id]'>$row[song] - $row[artist]</a><BR>";
}
die();
}
?>
<form method="POST">
Search: <input type="text" name="term" size="30"> in <select name="type"> <option value="artist">Artists</option>
<option value="song">Tracks</option>
</select> <input type="submit" name="search" value="Search">
</form>
Tom H
20-11-2007, 08:41 PM
<?php
include("db.php");
$avaliable_terms = array('artist','song');
if(isset($_POST['search'])) {
$term = addslashes($_POST['term']);
$type = addslashes($_POST['type']);
if(!in_array($type,$avaliable_terms)) $oh_no[] = "The search term doesn't match one of ours!";
$query = mysql_query("select * from lyrics where ".$type." like '%".$term."%' order by ".$type." asc");
if(mysql_num_rows($query)==0) $oh_no[] = "No results were found.";
if(isset($oh_no)) {
$i=0;
while($i<count($oh_no)) {
$oh_no_no .= $oh_no[$i]."<BR>";
$i++;
}
}
echo "You searched for ".$term."<BR><BR>";
if(isset($oh_no_no)) {
echo $oh_no_no;
die();
}
while($row = mysql_fetch_array($query)) {
echo "<a href='view.php?id=$row[id]'>$row[song] - $row[artist]</a><BR>";
}
die();
}
?>
<form method="POST">
Search: <input type="text" name="term" size="30"> in <select name="type"> <option value="artist">Artists</option>
<option value="song">Tracks</option>
</select> <input type="submit" name="search" value="Search">
</form>
That's called neat efficient programming..anyone else heard of that round here?
That's called neat efficient programming..anyone else heard of that round here?
Except that the queries aren't in upper case, which is a personal pet peeve of mine :P Breaks it up from everything else in a script by having them upper case imo.
Tom H
20-11-2007, 08:53 PM
Except that the queries aren't in upper case, which is a personal pet peeve of mine :P Breaks it up from everything else in a script by having them upper case imo.
I rest my case.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.