Web Hosting Forum | Lunarpages
News: July 14, 2008 - New Contest! - Submit Your WordPress Theme Designs, Win BIG!
June 30, 2008 - Submit Your Site for the July 08 Site of the Month Award!
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 25, 2008, 05:48:13 PM


Login with username, password and session length


Pages: [1]   Go Down
  Print  
Author Topic: Databases, recordsets, php and a simple search  (Read 529 times)
Paulapost
Trekkie
**
Offline Offline

Posts: 13


« on: March 05, 2007, 07:19:56 PM »

I have been working in dreamweaver to create a simple search on a database. I have a database named "library"  It has one table: "main" and the fields in my table are "bookid - category -  mediatype -  title  - author -  year -  notes "

Using dreamweaver, (php) I can get all of the records to print out in a table using recordsets. (so I know my connections are OK.) But I can not figure out how to display a recordset or query that just shows a list of all the items for a selected category (i.e. category=children.)

I want to have a form that users can select from a finite list of media types (i.e. biography, fiction, somethingelse) and return results matching their query. here is my search page code between my <body tags:

Code:
<form action="/result.php" method="get" name="categorysearch" id="categorysearch">
  <select name="category" id="category">
    <option>Biography
    <option>Child Development
    <option>Children
    <option>Culture and Politics
    </select>
  <input type="submit" name="Submit" value="Submit">
</form>

Here is the result page tags:
Code:
<body>
<p>Library Holdings</p>
<p>&nbsp;</p>
<table border="1" cellpadding="2" cellspacing="4">
  <tr>
    <td>bookid</td>
    <td>category</td>
    <td>mediatype</td>
    <td>title</td>
    <td>author</td>
    <td>year</td>
    <td>notes</td>
  </tr>
  <?php do { ?>
  <tr>
    <td><?php echo $row_holdings['bookid']; ?></td>
    <td><?php echo $row_holdings['category']; ?></td>
    <td><?php echo $row_holdings['mediatype']; ?></td>
    <td><?php echo $row_holdings['title']; ?></td>
    <td><?php echo $row_holdings['author']; ?></td>
    <td><?php echo $row_holdings['year']; ?></td>
    <td><?php echo $row_holdings['notes']; ?></td>
  </tr>

  <?php } while ($row_holdings mysql_fetch_assoc($holdings)); ?>
</table>
  <?php require_once('Connections/library.php'); ?>
<?php
$maxRows_holdings 
10;
$pageNum_holdings 0;
if (isset(
$HTTP_GET_VARS['pageNum_holdings'])) {
  $pageNum_holdings $HTTP_GET_VARS['pageNum_holdings'];
}
$startRow_holdings $pageNum_holdings $maxRows_holdings;

$colname_holdings "1";
if (isset(
$HTTP_GET_VARS['category'])) {
  $colname_holdings = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['category'] : addslashes($HTTP_GET_VARS['category']);
}
mysql_select_db($database_library$library);
$query_holdings sprintf("SELECT * FROM main WHERE category = '%s'"$colname_holdings);
$query_limit_holdings sprintf("%s LIMIT %d, %d"$query_holdings$startRow_holdings$maxRows_holdings);
$holdings mysql_query($query_limit_holdings$library) or die(mysql_error());
$row_holdings mysql_fetch_assoc($holdings);

if (isset(
$HTTP_GET_VARS['totalRows_holdings'])) {
  $totalRows_holdings $HTTP_GET_VARS['totalRows_holdings'];
} else {
  $all_holdings mysql_query($query_holdings);
  $totalRows_holdings mysql_num_rows($all_holdings);
}
$totalPages_holdings ceil($totalRows_holdings/$maxRows_holdings)-1;

mysql_free_result($holdings);
?>

Records <?php echo ($startRow_holdings 1?> to <?php echo min($startRow_holdings $maxRows_holdings$totalRows_holdings?> of <?php echo $totalRows_holdings ?>
</body>
</html>

The connections folder is uploaded and working correctly.

The error message I get is:
Quote
Library Holdings

HERE SHOWS A TABLE WITH ONLY THE FIELD TITLES, BUT NO RECORDS (7columns, a row)  

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/aquas3/public_html/paxeducare/result.php on line 31
 Records 1 to 10 of 70

The odd thing is, this counter is returning the correct total number of records for each search that I test. The part that I am messing up with seems to simply be how to display the results of the search.

Any Help is greatly appreciated!!!





Logged
tarheit
Galactic Royalty
*****
Offline Offline

Posts: 461


WWW
« Reply #1 on: March 06, 2007, 08:36:28 AM »

It's not really that odd.  The code basically, first tries to output the results of the query,  Then it actually does the query and then prints out the total number of matches.

You probably have to set $holdings to a valid object before you use it the first time, not after.

-Tim
Logged

Paulapost
Trekkie
**
Offline Offline

Posts: 13


« Reply #2 on: March 06, 2007, 08:42:11 AM »

Thanks!

I think the code was messed up. I finally got it to work. I will include the code below. Now I just need to format and such. since I used dreamweaver to create the code, I didn't choose the order for the code. The Html and header stiff is not first. Can I mote the first lines of the .php code below the header statements?

Code:
<?php require_once('Connections/library.php'); ?>
<?php
$colname_rsholdings 
"1";
if (isset(
$HTTP_GET_VARS['select'])) {
  $colname_rsholdings = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['select'] : addslashes($HTTP_GET_VARS['select']);
}
mysql_select_db($database_library$library);
$query_rsholdings sprintf("SELECT * FROM main WHERE category = '%s'"$colname_rsholdings);
$rsholdings mysql_query($query_rsholdings$library) or die(mysql_error());
$row_rsholdings mysql_fetch_assoc($rsholdings);
$totalRows_rsholdings mysql_num_rows($rsholdings);
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table border="1" cellpadding="2" cellspacing="4">
  <tr>
    <td>bookid</td>
    <td>category</td>
    <td>mediatype</td>
    <td>title</td>
    <td>author</td>
    <td>year</td>
    <td>notes</td>
  </tr>
  <?php do { ?>
  <tr>
    <td><?php echo $row_rsholdings['bookid']; ?></td>
    <td><?php echo $row_rsholdings['category']; ?></td>
    <td><?php echo $row_rsholdings['mediatype']; ?></td>
    <td><?php echo $row_rsholdings['title']; ?></td>
    <td><?php echo $row_rsholdings['author']; ?></td>
    <td><?php echo $row_rsholdings['year']; ?></td>
    <td><?php echo $row_rsholdings['notes']; ?></td>
  </tr>
  <?php } while ($row_rsholdings mysql_fetch_assoc($rsholdings)); ?>
</table>
</body>
</html>
<?php
mysql_free_result
($rsholdings);
?>

Logged
tarheit
Galactic Royalty
*****
Offline Offline

Posts: 461


WWW
« Reply #3 on: March 07, 2007, 04:45:34 AM »

Yes, you can move the php code that now appears before the header to after the header if you wish.  It really doesn't make much difference so long as it appears before you try and displaythe results (eg. before the line '<?php do { ?>')

-Tim
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM