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****************************************

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.