Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old October 15th, 2003, 12:07 PM
beley beley is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 5 beley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Problem with SQL query, please help!

I have a page that searches a one-table database of books. It worked fine when the query was only for Title or Author name, but when I added the last segment (to search between two sets of two values) the query just returns 0 rows now. If I remove that entire section it will work. I have tried breaking the query into sections with parentheses but still no luck. I'll post the query below...

Any idea what I'm doing wrong?
Code:
SELECT * FROM booklist WHERE 
('Author' LIKE '%$Author%') AND 
('Title' LIKE '%$Title%') AND 
('ReadingLevel' BETWEEN '$ReadingLow' AND '$ReadingHigh') AND 
('PointValue' BETWEEN '$PointLow' AND '$PointHigh') ORDER BY 'Title','Author'

Reply With Quote
  #2  
Old October 15th, 2003, 04:04 PM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Problem with SQL query, please help!

Code:
SELECT * FROM booklist WHERE 'Author' LIKE '$Author%' AND 'Title' LIKE '%$Title%' AND 'ReadingLevel' BETWEEN '$ReadingLow' AND '$ReadingHigh' AND 'PointValue' BETWEEN '$PointLow' AND '$PointHigh' ORDER BY 'Title','Author'

Reply With Quote
  #3  
Old October 15th, 2003, 04:14 PM
beley beley is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 5 beley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Problem with SQL query, please help!

THanks for the suggestion but that's what I tried first, then tried to separate it out into parentheses to see if that would help. Any other suggestions? Below is the entire page:
php Code:
Original - php Code
  1. <html>
  2. <head>
  3. <title> AR Book List </title>
  4. <STYLE TYPE="text/css">
  5.   BODY { font-size: 12pt; font-family: Arial, Helvetica; }
  6.   TD { font-size: 12pt; font-family: Arial, Helvetica; }
  7. </STYLE>
  8. </head>
  9. <body>
  10.  
  11. <?php
  12.  
  13.     //Colors to Alternate Rows
  14.  
  15.     $color1 = "#F5F5F5";
  16.     $color2 = "#FFFFFF";
  17.     $row_count = 0;
  18.  
  19.  
  20.     // Connect to the database server
  21.     $dbcnx = @mysql_connect('localhost', 'xxxxx', 'xxxxx');
  22.     if (!$dbcnx) {
  23.         die( '<p>Unable to connect to the ' .
  24.             'database server at this time.</p>' );
  25.  }
  26.  
  27.     // Select the database
  28.     if (! @mysql_select_db('xxxxx') ) {
  29.         die( '<p>Unable to locate the ' .
  30.             'database at this time.</p>' );
  31.  }
  32.  
  33. ?>
  34.  
  35. <?php
  36.  
  37. // If the Search form has already been submitted
  38.  
  39. if(isset($_POST['Submit'])) {
  40.  
  41.     //Print the table header
  42.     echo("
  43.     <div align="center">
  44.     <p><h1> Search Results </h1></p>
  45.     <p>
  46.     <table width=90% cellpadding=5 cellspacing=1 bgcolor=cccccc border=0>
  47.     <tr>
  48.     <td bgcolor=f5f5f5>TestNum</td>
  49.     <td bgcolor=f5f5f5>Title</td>
  50.     <td bgcolor=f5f5f5>Author</td>
  51.     <td bgcolor=f5f5f5>ReadingLevel</td>
  52.     <td bgcolor=f5f5f5>PointValue</td>");
  53.  
  54.  
  55.     //MySQL SELECT Query to sort out form data from search
  56.  
  57.     $result = mysql_query("SELECT * FROM booklist WHERE
  58.     ('Author' LIKE '%$Author%') AND
  59.     ('Title' LIKE '%$Title%') AND
  60.     ('ReadingLevel' BETWEEN '$ReadingLow' AND '$ReadingHigh') AND
  61.     ('PointValue' BETWEEN '$PointLow' AND '$PointHigh')
  62.     ORDER BY 'Title','Author'");
  63.      if (!$result) {
  64.        die('<p>Error performing query: ' . mysql_error() .
  65.            '</p>');
  66.      }
  67.  
  68.  
  69.     while ($row = mysql_fetch_array($result)) {
  70.         $testnum = $row["TestNum"];
  71.         $title = $row["Title"];
  72.         $author = $row["Author"];
  73.         $readinglevel = $row["ReadingLevel"];
  74.         $pointvalue = $row["PointValue"];
  75.  
  76.         /* Now we do this small line which is basically going to tell
  77.         PHP to alternate the colors between the two colors we defined above. */
  78.  
  79.         $row_color = ($row_count % 2) ? $color1 : $color2;
  80.  
  81.         // Echo your table row and table data that you want to be looped over and over here.
  82.  
  83.         echo "<tr>
  84.         <tr bgcolor="$row_color">
  85.         <td>$testnum</td>
  86.         <td>$title</td>
  87.         <td>$author</td>
  88.         <td>$readinglevel</td>
  89.         <td>$pointvalue</td>
  90.         </tr>";
  91.  
  92.         // Add 1 to the row count
  93.  
  94.         $row_count++;
  95.         }
  96.  
  97.  
  98. }
  99.  
  100. // If the Form was not submitted, display all the books below
  101.  
  102. else {
  103.  
  104.     echo("
  105.     <div align="center">
  106.     <table border=0 cellspacing=3 cellpadding=3>
  107.     <tr valign=middle><td>
  108.     <p><h1> AR Book List Database </h1></p>
  109.     </td><td align=right>
  110.     <p><b> Search the Book List Database</b></p>
  111.     <form action=" . $_SERVER['PHP_SELF'] . " method="post">
  112.     Search By Author: <input type=textarea name="Author">
  113.     <br />Search By Title: <input type=textarea name="Title">
  114.     <br />Search By Reading Level: Between <input type=text size=4 name="ReadingLow"> and <input type=text size=4 name="ReadingHigh">
  115.     <br />Search By Point Value: Between <input type=text size=4 name="PointLow"> and <input type=text size=4 name="PointHigh">
  116.     <br /><input type="submit" value="Submit" name="Submit"></form>
  117.     </td></tr>
  118.     </table>
  119.     <table width=90% cellpadding=5 cellspacing=1 bgcolor=cccccc border=0>
  120.     <tr>");
  121.  
  122.         echo("<td bgcolor=f5f5f5><a href="" . $_SERVER['PHP_SELF'] . "">TestNum</td>");
  123.         echo("<td bgcolor=f5f5f5><a href="" . $_SERVER['PHP_SELF'] . "?orderby=title">" . "Title</a></td>");
  124.         echo("<td bgcolor=f5f5f5><a href="" . $_SERVER['PHP_SELF'] . "?orderby=author">" . "Author</a></td>");
  125.         echo("<td bgcolor=f5f5f5>ReadingLevel</td>");
  126.         echo("<td bgcolor=f5f5f5>PointValue</td>");
  127.  
  128.     if ($orderby == title) {
  129.  
  130.          // Sort books by title
  131.         $result = @mysql_query('SELECT * FROM booklist ORDER BY Title');
  132.             if (!$result) {
  133.             die('<p>Error performing query: ' . mysql_error() .
  134.             '</p>');
  135.         }
  136.  
  137.     } elseif ($orderby == author) {
  138.  
  139.         // Sort books by author, then by title
  140.         $result = @mysql_query('SELECT * FROM booklist ORDER BY Author,Title');
  141.             if (!$result) {
  142.             die('<p>Error performing query: ' . mysql_error() .
  143.             '</p>');
  144.         }
  145.  
  146.     } else {
  147.  
  148.         // Default - sort books by test number
  149.         $result = @mysql_query('SELECT * FROM booklist ORDER BY TestNum');
  150.             if (!$result) {
  151.             die('<p>Error performing query: ' . mysql_error() .
  152.             '</p>');
  153.         }
  154.  
  155.     }
  156.  
  157.  
  158.         while ($row = mysql_fetch_array($result)) {
  159.         $testnum = $row["TestNum"];
  160.         $title = $row["Title"];
  161.         $author = $row["Author"];
  162.         $readinglevel = $row["ReadingLevel"];
  163.         $pointvalue = $row["PointValue"];
  164.  
  165.         /* Now we do this small line which is basically going to tell
  166.         PHP to alternate the colors between the two colors we defined above. */
  167.  
  168.         $row_color = ($row_count % 2) ? $color1 : $color2;
  169.  
  170.         // Echo your table row and table data that you want to be looped over and over here.
  171.  
  172.         echo "<tr>
  173.         <tr bgcolor="$row_color">
  174.         <td>$testnum</td>
  175.         <td>$title</td>
  176.         <td>$author</td>
  177.         <td>$readinglevel</td>
  178.         <td>$pointvalue</td>
  179.         </tr>";
  180.  
  181.         // Add 1 to the row count
  182.  
  183.         $row_count++;
  184.         }
  185.  
  186.     }
  187.  
  188. ?>
  189.  
  190. </table>
  191. </div>
  192. </body>
  193. </html>

Reply With Quote
  #4  
Old October 15th, 2003, 04:27 PM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Problem with SQL query, please help!

Change your sql statement like this
php Code:
Original - php Code
  1.  
  2. $result = mysql_query("SELECT * FROM booklist WHERE
  3.     Author LIKE '%$Author%' AND
  4.     Title LIKE '%$Title%' AND
  5.     '$ReadingLow'<=ReadingLevel AND ReadingLevel<='$ReadingHigh' AND
  6.     '$PointLow'<=PointValue AND PointValue<='$PointHigh'
  7.     ORDER BY Title,Author");
  8.  

Reply With Quote