Jimi_l Posted October 29, 2007 Share Posted October 29, 2007 Hi All, Below is a search form I am trying to build. My DB (parts) has 1 table (all) and 5 fields (f0 to f5). The script errors out with the following- failed Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\Abyss Web Server\htdocs\search.php on line 56 failed 2 The entire code is as follows- <html> <head><title>Database Search</title></head> <form name="form" action="search.php" method="get"> <p> make (ex.Acura) <input type="text" name="x" /> <br> model (ex.Inegra) <input type="text" name="y" /> <br> part (ex.brakes) <input type="text" name="z" /> <input type="submit" name="Submit" value="Search" /> </p> </form> <?php // get variables $var = @$_GET['x'] ; $var = @$_GET['y'] ; $var = @$_GET['z'] ; /* connection information */ $hostname = "localhost"; $username = "root"; $password = "admin"; $dbName = "parts"; $table = "all"; /* make connection to database */ MYSQL_CONNECT($hostname, $username, $password) OR DIE( "Unable to connect to database"); @mysql_select_db("$dbName") or die( "Unable to select database"); if ($make == "x") {$make = '%';} if ($model == "y") {$model = '%';} if ($part == "z") {$part == '%';} $query = ("SELECT * FROM $table WHERE make LIKE '$make%' or model LIKE '$model%' or part LIKE '$part%'"); $result = MYSQL_QUERY($query); if (!$result) print ("failed\n"); /* Determine the number of records returned */ WHILE ($row=MYSQL_FETCH_ROW($result)) $number = mysql_numrows($result); if (!$number) print("failed 2 \n"); /* Print the relevant information */ $i = 0; PRINT "<font face=\"Verdana, Arial, Helvetica, sans-serif\"> <b>There are $number records in the inventory:</b></font><p>"; PRINT "<table cellpadding=5>"; PRINT " <TR bgcolor=black> <td><font face=\"Verdana, Arial,Helvetica, sans-serif\" size=\"-1\" color=white><b>make </b></font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\" color=white><b>model </b></font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\" color=white><b>part</b></font></td></tr>"; WHILE ($i < $number): $make = mysql_result($result, $i,"make"); $model = mysql_result($result, $i,"model"); $part = mysql_result($result,$i,"part"); if ($i%2 == 0) { PRINT "<tr bgcolor=lightgrey> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\">$make</font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\"> $model</font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\"> $part</font></td></tr>"; } else { PRINT "<tr bgcolor=lightgreen> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\">$LastName</font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\"> $FirstName</font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\"> $Address</font></td></tr>"; } $i++; ENDWHILE; PRINT "</table>"; /* Close the database connection */ MYSQL_CLOSE(); ?> </body> </html> It seems as if the form is submitted before any data is entered. Any ideas would be greatly appreciated. I have really struggling with this whole project for a while now and it is learn as I go. I have tried breaking this into two, the form and the .php with identical results. Thanks, Jim Quote Link to comment Share on other sites More sharing options...
Orio Posted October 29, 2007 Share Posted October 29, 2007 You have many mistakes here: 1) You dont want the script to run before someone submits a search. So in the beginning (of the php code) you need to add a if that checks if the submit button was pressed: if(isset($_GET['Submit'])) {... 2) You are inserting all of the data into the same variable $var, what's the logic in that? This way it's value would be $_GET['z'] and the other data will just go to waste. 3) After you are selecting a database you have three if's, but the variables $make,$model,$part weren't defined anywhere. Also, inside that last if you used == instead of =, so that doesn't make sense either. 4) The line where you build the $query. Why do you have brackets around the string? Remove them. 5) You've used a while loop (the first one), without any braces after it, this will not give you the result you're wanting. Let's start with this... After you fix these problems show the code again. Orio. Quote Link to comment Share on other sites More sharing options...
Jimi_l Posted October 29, 2007 Author Share Posted October 29, 2007 Hi Orio, Yes I know there are many mistakes. I turned onn error displaying and found many undefined variables (which I suspected). I do appreciate you taking the time to help. I have to go to work now but will return tonight. make the changes and return the results. Thanks again, Jim Quote Link to comment Share on other sites More sharing options...
Jimi_l Posted October 29, 2007 Author Share Posted October 29, 2007 Me Again, I have a few questions. If you would rather just point me to a link to read that would be fine to as I have looked but rarely find the exact issue I am looking for. Perhaps you could give me a hint or example? 1) if(isset($_GET['Submit'])) {... I assume the "if" statement is waiting for input from the button but the {... (braces?) seems to indicate there should be something else there inside of them. What would this be? 2) How would I submit three different variables? 3) How would I define the three variables? 4) I knew this was wrong (or I suspected anyway). This is a bunch of cobbled together codes I found online. 5) Braces after it? Like all alone braces after it? Quote Link to comment Share on other sites More sharing options...
Orio Posted October 29, 2007 Share Posted October 29, 2007 Your code was full of many things that weren't defined, it was very messy. Try this: <html> <head><title>Database Search</title></head> <form name="form" action="search.php" method="POST"> <p> make (ex.Acura) <input type="text" name="make" /> <br> model (ex.Inegra) <input type="text" name="model" /> <br> part (ex.brakes) <input type="text" name="part" /> <input type="submit" name="submit" value="Search" /> </p> </form> <?php if(!isset($_POST['submit'])) die; // get variables $make = $_POST['make'] ; $model = $_POST['model'] ; $part = $_POST['part'] ; /* connection information */ $hostname = "localhost"; $username = "root"; $password = "admin"; $dbName = "parts"; $table = "all"; /* make connection to database */ mysql_connect($hostname, $username, $password) or die("Unable to connect to database"); mysql_select_db($dbName) or die("Unable to select database"); if ($make == "") $make = "%"; if ($model == "") $model = "%"; if ($part == "") $part = "%"; $query = "SELECT * FROM $table WHERE make LIKE '$make%' or model LIKE '$model%' or part LIKE '$part%'"; $result = mysql_query($query); if ($result === FALSE) die("failed\n"); $number = mysql_num_rows($result); PRINT "<font face=\"Verdana, Arial, Helvetica, sans-serif\"> <b>There are $number records in the inventory:</b></font><p>"; PRINT "<table cellpadding=5>"; PRINT "<TR bgcolor=black> <td><font face=\"Verdana, Arial,Helvetica, sans-serif\" size=\"-1\" color=white><b>make </b></font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\" color=white><b>model </b></font></td> <td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\" color=white><b>part</b></font></td></tr>"; $i = 0; while ($row = mysql_fetch_array($result)) { $make = $row['make']; $model = $row['model']; $part = $row['part']; if ($i%2 == 0) { PRINT "<tr bgcolor=lightgrey><td><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\">$make</font></td> <td><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\"> $model</font></td> <td><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\"> $part</font></td></tr>"; } else { PRINT "<tr bgcolor=lightgreen><td><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\">$make</font></td><td><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\"> $model</font></td> <td><font face=\"Verdana, Arial, Helvetica,sans-serif\" size=\"-1\"> $part</font></td></tr>"; } $i++; } PRINT "</table>"; /* Close the database connection */ MYSQL_CLOSE(); ?> </body> </html> Now the only thing I don't understand now is how you are using all kinds of column names when you said the columns are called f0-5... Orio. Quote Link to comment Share on other sites More sharing options...
Jimi_l Posted October 31, 2007 Author Share Posted October 31, 2007 Hello Again, This is indeed more promising however I get no results regardless of what I search for. I simply get the "failed" message which I assume is from this line- if ($result === FALSE) die("failed\n"); I added this line to the top to see what was happening- error_reporting(E_ALL); ini_set('display_errors', '1'); And get no errors which I assume would mean the search found nothing however I know the data is there and I get results on similar searches in phpmyadmin. You mentioned using all kinds of column names but I was under the impression I was not. In the search string- $query = "SELECT * FROM $table WHERE make LIKE '$make%' or model LIKE '$model%' or part LIKE '$part%'"; I assumed it was looking at EVERY field in the table called "all" of the DB called "parts" as defined previously as opposed to specific fields. Is this incorrect? Anyway thanks again for helping out a dumb(you know what). I have actually installed a good deal PHP apps, edited files therein, set up mail and Web servers, do a ton of system builds, ect but never hacked away at raw code from scratch. I appreciate your patience. Jim Quote Link to comment Share on other sites More sharing options...
Jimi_l Posted November 3, 2007 Author Share Posted November 3, 2007 bump Quote Link to comment Share on other sites More sharing options...
Orio Posted November 3, 2007 Share Posted November 3, 2007 Replace this line: $result = mysql_query($query); With: $result = mysql_query($query) or die($query."<br>".mysql_error()); And post what you get. Orio. Quote Link to comment Share on other sites More sharing options...
Jimi_l Posted November 3, 2007 Author Share Posted November 3, 2007 Hi All, I changed the line and now get the following with any query- SELECT * FROM all WHERE make LIKE '%%' or model LIKE '%%' or part LIKE 'brakes%' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'all WHERE make LIKE '%%' or model LIKE '%%' or part LIKE 'brakes%'' at line 1 The phrase "brakes" was searched for thin this instance but with other phrases I get identical results. I noticed the double "%" which seems like an issue for the fields left blank. Thanks again, Jim Quote Link to comment Share on other sites More sharing options...
Orio Posted November 5, 2007 Share Posted November 5, 2007 Are you sure you have a table named "all" that has the columns make,model,part? Orio. Quote Link to comment Share on other sites More sharing options...
Jimi_l Posted November 5, 2007 Author Share Posted November 5, 2007 No, I have a DB called "parts" and in it one table called "all. In that table is five fields labeled F0 to F5. I was under the assumption that the query was coded to search all fields in that one table. The make. model and part are the user entered variables from the search script. Is this incorrect? Jim Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.