dkoolgeek Posted February 4, 2007 Share Posted February 4, 2007 I have a program that I want to search a database and return the records found, preferrably in some sort of organized way(a table). Here is the code I have so far, but I don't think its correct. Any help would be appreciated. Thanks SEARCH PAGE: <body bgcolor="#505050"> <?php include("Header.php")?> <body bgcolor="#505050"> <center><p align="center" class="style4" style="font-family:Verdana, Arial, Helvetica, sans-serif">Search for CD's, DVD's, and Books! </p> <form action="Results.php" method="post" name="Search" id="Search"> <table width="782" height="31" border="0"> <tr> <td width="567"> <div align="right"> <input name="Search" type="text" size="75"> </div></td><td width="205"><input name="Search" type="submit" id="Search" value="Search"></td> </tr> </table> </form></center> </body> </html> RESULTS PAGE <?php @mysql_connect("??","???","???") or die("Cannot Connect to DB!"); @mysql_select_db("trademymedia")or die("Cannot select DB"); $SEARCH = $_POST["Search"]; $CD = $mysqldb->query("SELECT * FROM cd WHERE Artist = '$SEARCH' OR Title = '$SEARCH'"); if ($CD -> num_rows> 0 ) { while($row = $result->fetch_object()) echo "$row->Title, $row->Artist <br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/ Share on other sites More sharing options...
yzerman Posted February 4, 2007 Share Posted February 4, 2007 I am not sure about the way you have written your PHP, so I am just going to go ahead and answer the question about your query, because I had a similar problem and this may save you some time. 1.) Index the cd table if you are going to be searching it. It will free up alot of resources on your server, and speed up your search. 2.) Your query should be SELECT * FROM `CD`WHERE `ARTIST` LIKE '%$search%' OR `TITLE` like '%$search%' The % is a wild card. If I wanted to do a search for a cd by Jon Bon Jovi, but I just put Bon Jovi in the Artist search field, it would not return anything if the query was `Artist` = '$search' because its looking for EXACTLY "Bon Jovi", with like and wild cards, you will return a more useful search. Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176474 Share on other sites More sharing options...
dkoolgeek Posted February 4, 2007 Author Share Posted February 4, 2007 Once I have the search query bringing back results, what is the best way to display them? Thanks Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176477 Share on other sites More sharing options...
dkoolgeek Posted February 4, 2007 Author Share Posted February 4, 2007 I am now getting the following error: Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Results.php on line 48 With the code <?php session_start() ?> <!-- Name: Homepage.php Date Last Updated:10-5-06 Created by: Bryan High School Advanced Division --> <html> <head> <title>trademymedia.com</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> a:link { color:#0099FF} a:visited{ color:#CC99FF} table{ color:#FFFFFF} a:hover img{ filter:Alpha(Opacity=50)Wave(Add=0, Freq=5, LightStrength=20, Phase=220, Strength=10);} a img { border-width: 0px; } .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; } .style2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; } </style> </head> <body bgcolor="#505050"> <div align="center"> <?php include("Header.php") ?> <?php @mysql_connect("-","-","-") or die("Cannot Connect to DB!"); @mysql_select_db("trademymedia")or die("Cannot select DB"); $SEARCH = $_POST["Search"]; $CD = $mysqldb->query("SELECT * FROM `CD` WHERE `ARTIST` LIKE '%$search%' OR `TITLE` like '%$search%'"); while ($row = $result->fetch_object()) echo "$row->Artist"; ?> Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176482 Share on other sites More sharing options...
yzerman Posted February 4, 2007 Share Posted February 4, 2007 That would depend on your database layout truthfully. Since you are selecting "*" (all fields in your table CD) there are going to be differing numbers of rows. I will assume at a minimum you have: Artist, and Title. I will also assume that the Artist is the first field, and title is the second field. To print these out: if ($cd) { //If the query went ok if (mysql_num_rows($cd) > 0) { //if there were any matches echo '<table>'; //begin the table echo '<tr><td><b><u>Artist</u></b></td></tr><tr><td><b><u>Title</u></b></td></tr>'; //Show the table headers while ($row = mysql_fetch_array($cd)) { //begin the while loop echo '<tr><td>', $row[0], '</td></tr><tr><td>', $row[1], '</td></tr>; //echo the results into the table } // end while echo '</table>'; // close the table } else { echo 'Sorry but your search for $Search didn't return any results. <a href="searchpage.html">Try Again?</a>'; } } else { echo 'There was a problem with the query. The error returned from mysql was <br />', mysql_error(); } Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176487 Share on other sites More sharing options...
yzerman Posted February 4, 2007 Share Posted February 4, 2007 I am now getting the following error: Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Results.php on line 48 With the code <?php session_start() ?> <!-- Name: Homepage.php Date Last Updated:10-5-06 Created by: Bryan High School Advanced Division --> <html> <head> <title>trademymedia.com</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> a:link { color:#0099FF} a:visited{ color:#CC99FF} table{ color:#FFFFFF} a:hover img{ filter:Alpha(Opacity=50)Wave(Add=0, Freq=5, LightStrength=20, Phase=220, Strength=10);} a img { border-width: 0px; } .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; } .style2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; } </style> </head> <body bgcolor="#505050"> <div align="center"> <?php include("Header.php") ?> <?php @mysql_connect("-","-","-") or die("Cannot Connect to DB!"); @mysql_select_db("trademymedia")or die("Cannot select DB"); $SEARCH = $_POST["Search"]; $CD = $mysqldb->query("SELECT * FROM `CD` WHERE `ARTIST` LIKE '%$search%' OR `TITLE` like '%$search%'"); while ($row = $result->fetch_object()) echo "$row->Artist"; ?> where do you define the variable $mysqldb? Try this on line 3: $mysqldb = @mysql_select_db("trademymedia")or die("Cannot select DB"); Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176490 Share on other sites More sharing options...
dkoolgeek Posted February 4, 2007 Author Share Posted February 4, 2007 Thanks to everyone who's been helping me out. I am just starting php. But now I have this problem on the "$CD" query with this code(the code has been updated with most of your tips): Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Results.php on line 49 <?php session_start() ?> <!-- Name: Homepage.php Date Last Updated:10-5-06 Created by: Bryan High School Advanced Division --> <html> <head> <title>trademymedia.com</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> a:link { color:#0099FF} a:visited{ color:#CC99FF} table{ color:#FFFFFF} a:hover img{ filter:Alpha(Opacity=50)Wave(Add=0, Freq=5, LightStrength=20, Phase=220, Strength=10);} a img { border-width: 0px; } .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; } .style2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; } </style> </head> <body bgcolor="#505050"> <div align="center"> <?php include("Header.php") ?> <?php @mysql_connect("localhost","student","student") or die("Cannot Connect to DB!"); @mysql_select_db("trademymedia")or die("Cannot select DB"); $mysqldb = @mysql_select_db("trademymedia")or die("Cannot select DB"); $SEARCH = $_POST["Search"]; $CD = mysql_query("SELECT * FROM `CD` WHERE `ARTIST` LIKE '%$search%' OR `TITLE` like '%$search%'"); if ($CD) { //If the query went ok if (mysql_num_rows($cd) > 0) { //if there were any matches echo '<table>'; //begin the table echo '<tr><td><b><u>Artist</u></b></td></tr><tr><td><b><u>Title</u></b></td></tr>'; //Show the table headers while ($row = mysql_fetch_array($cd)) { //begin the while loop echo '<tr><td>', $row[1], '</td></tr><tr><td>', $row[2], '</td></tr>'; //echo the results into the table } // end while echo '</table>'; } else { } } else { echo 'There was a problem with the query. The error returned from mysql was <br />', mysql_error(); } ?> Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176503 Share on other sites More sharing options...
dkoolgeek Posted February 4, 2007 Author Share Posted February 4, 2007 I figured it out. Thanks to everyone for their time and effort. I'll probably need more help later on. Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176508 Share on other sites More sharing options...
yzerman Posted February 4, 2007 Share Posted February 4, 2007 <?php session_start() ?> <!-- Name: Homepage.php Date Last Updated:10-5-06 Created by: Bryan High School Advanced Division --> <html> <head> <title>trademymedia.com</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> a:link { color:#0099FF} a:visited{ color:#CC99FF} table{ color:#FFFFFF} a:hover img{ filter:Alpha(Opacity=50)Wave(Add=0, Freq=5, LightStrength=20, Phase=220, Strength=10);} a img { border-width: 0px; } .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; } .style2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; } </style> </head> <body bgcolor="#505050"> <div align="center"> <?php include("Header.php") ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="SEARCH" value="<?php if(isset($_POST['SEARCH'])) echo $_POST['SEARCH']; ?>"> <input type="submit" name="submit" value="Search"> </form> <?php //form submittal if (isset($_POST['submit'])) { @mysql_connect("localhost","student","student") or die("Cannot Connect to DB!"); @mysql_select_db("trademedia")or die("Cannot select DB"); $SEARCH = $_POST["Search"]; $CD = mysql_query("SELECT * FROM `CD` WHERE `ARTIST` LIKE '%$Search%' OR `TITLE` like '%$Search%'"); if ($CD) { //If the query went ok if (mysql_num_rows($CD) > 0) { //if there were any matches echo '<table>'; //begin the table echo '<tr><td><b><u>Artist</u></b></td><td><b><u>Title</u></b></td></tr>'; //Show the table headers while ($row = mysql_fetch_array($CD)) { //begin the while loop echo '<tr><td>', $row[0], '</td><td>', $row[1], '</td></tr>'; //echo the results into the table } // end while echo '</table>'; } else { } } else { echo 'There was a problem with the query. The error returned from mysql was <br />', mysql_error(); } } ?> There you go, tested, and works, see it at www.sandynpaul.com/cdsearch.php the only entry in the database is Bon Jovi's slippery when wet. Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176513 Share on other sites More sharing options...
dkoolgeek Posted February 4, 2007 Author Share Posted February 4, 2007 <?php session_start() ?> <!-- Name: Homepage.php Date Last Updated:10-5-06 Created by: Bryan High School Advanced Division --> <html> <head> <title>trademymedia.com</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> a:link { color:#0099FF} a:visited{ color:#CC99FF} table{ color:#FFFFFF} a:hover img{ filter:Alpha(Opacity=50)Wave(Add=0, Freq=5, LightStrength=20, Phase=220, Strength=10);} a img { border-width: 0px; } .style1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 18px; } .style2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; } </style> </head> <body bgcolor="#505050"> <div align="center"> <?php include("Header.php") ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="SEARCH" value="<?php if(isset($_POST['SEARCH'])) echo $_POST['SEARCH']; ?>"> <input type="submit" name="submit" value="Search"> </form> <?php //form submittal if (isset($_POST['submit'])) { @mysql_connect("localhost","student","student") or die("Cannot Connect to DB!"); @mysql_select_db("trademedia")or die("Cannot select DB"); $SEARCH = $_POST["Search"]; $CD = mysql_query("SELECT * FROM `CD` WHERE `ARTIST` LIKE '%$Search%' OR `TITLE` like '%$Search%'"); if ($CD) { //If the query went ok if (mysql_num_rows($CD) > 0) { //if there were any matches echo '<table>'; //begin the table echo '<tr><td><b><u>Artist</u></b></td><td><b><u>Title</u></b></td></tr>'; //Show the table headers while ($row = mysql_fetch_array($CD)) { //begin the while loop echo '<tr><td>', $row[0], '</td><td>', $row[1], '</td></tr>'; //echo the results into the table } // end while echo '</table>'; } else { } } else { echo 'There was a problem with the query. The error returned from mysql was <br />', mysql_error(); } } ?> There you go, tested, and works, see it at www.sandynpaul.com/cdsearch.php the only entry in the database is Bon Jovi's slippery when wet. When I test your application, it works fine if I hit the "submit" button. However, if i'm in the textbox, and I hit ENTER, I get no results Mine works backwards, if I hit enter, it works; if I click submit, it gives 0 results. What might be causing this? Link to comment https://forums.phpfreaks.com/topic/36974-search-and-table-help/#findComment-176530 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.