Forum Moderators: coopster
Extreme newbie here, only me second day of mucking around with PHP and MySQL.
I'm trying to build a search form that will look through a MySQL database.
Using a bit of code that Jatar gave to someone else, I tried modifying it to my own needs, but keep coming up with the same results:
"select * from releases where id= and artist=
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/website/search.php on line 13"
Here's the PHP code I'm using:
<?
$host = "localhost";
$user = "username";
$pass = "password";
$dbname = "databasename";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql = "select * from tablename where id=" . $_POST['artist'] . " and artist=" . $_POST['artist'] . ""; echo $sql;
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['id'],": ",$row['artist'];
}
?>
Any ideas on what I'm doing wrong here?
Thanks in advance!
Todd
Here's what I've got in the form:
<form name="form1" method="post" action="search.php">
<input type="text" name="textfieldName" size="24"><input type="submit" name="submitButtonName">
</select>
</form>
Where search.php is the script that I posted earlier.
Could name="textfieldName" be the problem?
Thanks!
Line 13:
while ($row = mysql_fetch_array($query)) {
Which I'm guessing ir referring to:
Line 10:
$sql = "select * from releases where artist=" . $_POST['artist'] . ""; echo $sql;
So perhaps it's my syntax in the query? I'm not quite sure WHY I'm using " . $_POST['artist'] . " (again, I swiped this code from a previous post by jatar) so perhaps that's it?
Thanks for your help!
Todd
Here's what I've got:
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql = "select * from releases where artist='" . $_POST['artist'] . "'"; echo $sql;
$query = mysql_query($sql);
echo "<table border=1>\n";
echo "<tr><td><b>ARTIST</b></td><td><b>RELEASE</b><td><b>DISC</b></tr>\n";
while ($row = mysql_fetch_array($query)) {
printf("<tr><td>%s</td><td>%s</td><td>%s</tr>\n",
$row[0], $row[1], $row[2], $row[3]);
}
echo "</table>\n";
?>
The resulting page simply says:
select * from releases where artist=''
ARTISTRELEASEDISC
(this last row is the table)
I'm guessing it has something to do with printf?
Thanks in advance!
Todd
You can put this at the top to see all the post variables:
print_r($_POST);
If that does nothing then you still have problems getting the form variables into your script. Must be a typo somewhere or something.
Tim
Perhaps it WAS working before then. Using that bit of code you gave me:
print_r($_POST);
I was able to see the results unformatted. It then spit out the results of:
echo "<tr><td><b>ARTIST</b></td><td><b>RELEASE</b><td><b>DISC</b></tr>\n";
However, after that it's blank, so perhaps it was working all along and I just have everyhting below that wrong. Here's what I've got:
while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['artist'],$row['release'],$row['disc'];
}
echo "</table>\n";
I'm guessing that it's the $row['etc'] parts that are wrong here...
Thanks, Tim.
while ($row = mysql_fetch_array($query)) {
echo "<p>",$row['artist'],$row['release'],$row['disc'],$row['genre'],$row['format'];
seems to all run together, like
ArtistReleaseDisc
Instead of
Artist <table column> Release <table column> Disc
So what should the syntax there be?
Now I want the search form to have a pulldown, i.e. search for _____ in pulldown menu (artist/release/disc)
I've named the pulldown list 'criteria' and changed by query to:
$sql = "select * from releases where '" . $_POST['criteria'] . "'='" . $_POST['submission'] . "'";
my form looks like this:
<form name="form1" method="POST" action="searchNEW.php">
<input type="text" name="submission" size="24"><select name="criteria" size="1">
<option value="artist">Artist</option>
<option value="release">Release</option>
<option value="disc">Disc</option>
</select><input type="submit" name="submitButtonName">
and sadly, no results are passed back to me, save for the table header and "Array ( [submission] => Radiohead [criteria] => artist [submitButtonName] => )
(Radiohead is what I searched for...)