----- Original Message ----- 
From: "yousuf hussain"

Dear members:

                        I came across a problem that while searching a record 
from a database it 
only returns one field record. i want to search it with different option.
  Code is given below
  Only Unit record is returned

<snip>

 <?php
 $searchText = $_REQUEST["search"];
// Try $searchText = $_POST['search'];
 include "header.php";
 ?>
<snip>
     <?php
      include "connection.php";
      if($searchText=="syard"){
      $mySQL = "select * from addbunglow where syard='$searchText' and 
isactive=1  order by bangid";
// try $mySQL = "SELECT * FROM addbunglow WHERE syard='" . $searchText . "' AND 
isactive=1 ORDER BY 
bangid";
// or $mySQL = 'SELECT * FROM addbunglow WHERE syard=\'' . $searchText . '\' 
AND isactive=1 ORDER BY 
bangid';
// or $mySQL = "SELECT * FROM addbunglow WHERE syard=\'$searchText\' AND 
isactive=1 ORDER BY 
bangid";
      echo "syard:".$searchText;
      }
      else if($searchText=="price"){
      $mySQL = "select * from addbunglow where price='$searchText' and 
isactive=1  order by bangid";
      echo "price:".$searchText;
      }
      else {
      $mySQL = "select * from addbunglow where unit='$searchText' and 
isactive=1  order by bangid";
      echo "unit:".$searchText;
      }
      $result = mysql_query($mySQL);
      $counter = 1;
      while($row = mysql_fetch_array($result))
// You are fetching an array of rows so why use while as there is only one 
array of rows to be 
fetched.
// Either use $rowS = msql_fetch_array() and then use foreach($rowS as $row) or
// while($row = mysql_fetch_row($result))

      <td ><?php echo $counter;?></td>
      <td ><?php echo $row["syard"];?></td>
      <td ><?php echo $row["street"];?></td>
      <td ><?php echo $row["phase"];?></td>
      <td ><?php echo $row["unit"];?></td>
      <td ><?php echo $row["catagory"];?></td>
      <td ><?php echo $row["price"];?></td>
      <td><?php echo $row["comments"];?></td>
     </tr>

It is really good to see the use of associative MySQL arrays. I only use 
mysql_fetch_assoc() 
although an array of rows is numerically indexed. This saves much heartache 
when making changes to 
the database structure.

      <td ><?php echo $row["syard"];?></td>

I would do this differently -
      <td ><?php echo($row['syard']); ?></td>

php is mostly white space insensitive so take advantage of this. It makes code 
easier to read by 
placing spaces between all units of code. I use $var['key'] rather than 
$var["key"] as there is no 
need to parse a variable key for other variables.

In fact I never use echo("The time is $time"); instead I use echo('The time is' 
. $time); Especially 
with mysql queries. Just remember to have spaces where needed -
$query = 'SELECT COUNT(*) FROM users, groups';
$query .= ' WHERE users.GroupID = groups.GroupID';
$query .= ' AND groups.GroupType = ' . $grouptype;

MySQL doesn't need single quotes (') around table and field names if you use 
the white space 
correctly in queries UNLESS you have spaces in table or field names in the 
database. Save yourself a 
lot of trouble tracking down missing or extra quotes in query string by not 
using spaces in 
table/field names in the database.

The echo 'test'; verses my echo('test'); is a bad habit of mine. I was well 
into learning php before 
I discovered that echo was a primitive rather than a function. Like return, 
continue, break etc.

Here are some simple database abstractions.

// ######## function db_connect() ########

function db_connect()
  {
  global $db_handle;
  $db_handle = mysql_connect('host', 'username', 'password'); // Local access
  mysql_select_db('databasename'); // Local access
  }



// ######## function db_to_array() ########

function db_to_array($query)
  {
  global $db_handle;
  $result = mysql_query($query, $db_handle);
  if(mysql_error($db_handle)) echo(mysql_error($db_handle) . "<br />\n" . 
$query . "<br />\n");
  if(mysql_num_rows($result) == 0) return FALSE;
  if(mysql_error($db_handle)) echo(mysql_error($db_handle) . "<br />\n" . 
$query . "<br />\n");
  $responce = mysql_fetch_row($result);
  return $responce[0];
  }



// ######## function db_to_arrays() ########

function db_to_arrays($query)
  {
  //echo($query . "<br>\n");
  global $db_handle;
  $result = mysql_query($query, $db_handle);
  if(mysql_error($db_handle)) echo(mysql_error($db_handle) . "<br />\n" . 
$query . "<br />\n");
  if(mysql_num_rows($result) == 0) return FALSE;
  if(mysql_error($db_handle)) echo(mysql_error($db_handle) . "<br />\n" . 
$query . "<br />\n");
  while($array = mysql_fetch_assoc($result))
    {
    $db_data[] = $array;
    }
  return $db_data;
  }






Reply via email to