Jump to content

[SOLVED] Not very good with PHP, and need help!


Recommended Posts

I am using Dreamweaver CS3 to connect to a MySQL database and do a search.  Dreamweaver created the code for me, but I need to enhance the function to include an error check which handles a "no results found".

 

Everything I have tried has caused the page to not display at all?  I have no idea how to change my little script to include this?  Can anyone help? 

 

This is the code from the searchresults.php

 

****************CODE****************************

 

<?php require_once('../Connections/MySQL2.php'); ?>

<?php

$currentPage = $_SERVER["PHP_SELF"];

 

$colname_search = "-1";

if (isset($_GET['title'])) {

  $colname_search = (get_magic_quotes_gpc()) ? $_GET['title'] : addslashes($_GET['title']);

}

mysql_select_db($database_MySQL2, $MySQL2);

$query_search = sprintf("SELECT * FROM items WHERE description LIKE '%%%s%%'    ORDER BY items.Description", $colname_search);

$search = mysql_query($query_search, $MySQL2) or die(mysql_error());

$row_search = mysql_fetch_assoc($search);

$totalRows_search = mysql_num_rows($search);

 

$queryString_Desription = "";

if (!empty($_SERVER['QUERY_STRING'])) {

  $params = explode("&", $_SERVER['QUERY_STRING']);

  $newParams = array();

  foreach ($params as $param) {

    if (stristr($param, "pageNum_Desription") == false &&

        stristr($param, "totalRows_Desription") == false) {

      array_push($newParams, $param);

    }

  }

  if (count($newParams) != 0) {

    $queryString_Desription = "&" . htmlentities(implode("&", $newParams));

  }

}

$queryString_Desription = sprintf("&totalRows_Desription=%d%s", $totalRows_Desription, $queryString_Desription);

?>

<?php require_once('../Connections/MySQL2.php'); ?>

 

....

 

<?php do { ?>

<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td class="style5"><?php echo $row_search['Description']; ?></td>

</tr>

</table>

<?php } while ($row_search = mysql_fetch_assoc($search)); ?>

 

*****************END CODE****************************************

 

 

 

I presume you could change

<?php do { ?>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="style5"><?php echo $row_search['Description']; ?></td>
</tr>
</table>
<?php } while ($row_search = mysql_fetch_assoc($search)); ?>

 

to

<?php
if($totalRows_search > 0)
{
    do { ?>
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="style5"><?php echo $row_search['Description']; ?></td>
</tr>
</table>
<?php } while ($row_search = mysql_fetch_assoc($search));
}
else
{
    echo 'No results forund';
} ?>

 

However you'll be better of coding the script yourself, that way you know what is happening in your script. Letting Dreamweaver do it for only complicates things, as the generated code is messy and alot of is not needed. The above code could be written to around 10 lines max.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.