Validate record

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Validate record

Post by Sculpture »

Hello,

I want to say to the audience of the site that there are no records if that is the case and I want to do it down the body of the page.

Code: Select all

<?php
			if (empty($colname)){
					echo "No matching record found.";
					exit;
			}
	?>
I think this is the start but where do I go from here?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not entirely sure of what you are trying to do, as your code doesn't really seem to correllate directly to your wishes. Normally, when performing a query the number of results is tested for using a function like mysql_num_rows(), which will tell you how many records are in the result set given from a query. You can compare the quantity it returns to a cutoff you determine, zero in this case, which will direct the code to output something different then.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I agree with feyd, something like the following would be better:

Code: Select all

if (mysql_num_rows($row) < 1){
                    echo "No matching record found.";
                    exit;
            }
Or better in my eyes would be to lose the exit() and do something like this:

Code: Select all

if (mysql_num_rows($row) > 0){
// put code here for when rows are found
            } else {
                    echo "No matching record found.";
            }
(#10850)
User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Post by Sculpture »

Thank you for your answer.

The problem though is that the recordset is before the <HEAD> tags and I still want most of the information to be displayed in the <BODY> tags. Is there a way where I can do this?

Thanks again.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

either perform the processing and store it into a variable you output during the body, or do the processing in the body section..
User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Post by Sculpture »

So far I have tried to make the HTML that I want to include a variable, making sure that there where no "'s in the body of the variable and I have tried to move the entire recordset and functions to the BODY of the page and the entire page has fallen over each time. I have tried a few other ideas but I will not go into them.

The PHP before the <head> tag is:

Code: Select all

<?php
if(!isset($tfm_rows) || intval($tfm_rows) == 0){$tfm_rows = 2;}
if(!isset($tfm_columns) || intval($tfm_columns) == 0){$tfm_columns = 4;}
if(!isset($tfm_vertical)){$tfm_vertical = "false";}
?>
<?php
//PHP version of DWTeam Dynamic Search SQL
//for qSearch
$tfmsqlstr = "";
if (isset($HTTP_POST_VARS["search"]) || isset($HTTP_GET_VARS["search"])){
  $tfm_searchField = (isset($HTTP_POST_VARS["search"]))?$HTTP_POST_VARS["search"]:$HTTP_GET_VARS["search"];
  if($tfm_searchField != "") {
    $tfm_andor = "AND";
    $tfm_exact = "false";
    $bellChar = chr(7);
    //if any words option
    //not implemented
    //if exact phrase option
    //not implemented
    $tfmsqlstr = " WHERE ((";
    $tfm_databaseFields = explode(",","name,description");    
    if ((strstr($tfm_searchField,'"')) || ($tfm_exact == "true")){ 
       $tfm_searchField = str_replace('"','',$tfm_searchField); 
       $tfm_andor = "OR";
    }else 
    if (stristr($tfm_searchField," or ")){ 
      $tfm_searchField = preg_replace('/\s+or\s+/i',$bellChar,$tfm_searchField);
      $tfm_andor = "OR";
    }else  
    if (strstr($tfm_searchField,',') || strstr($tfm_searchField,' ') || stristr(strtolower($tfm_searchField),' and ')) { 
      $tfm_searchField = preg_replace("/\s+and\s+/i",$bellChar,$tfm_searchField);
      $tfm_searchField = str_replace(",",$bellChar,$tfm_searchField);
      $tfm_searchField = str_replace(" ",$bellChar,$tfm_searchField);
    }
    $splitField = explode($bellChar,$tfm_searchField);
    for ($i = 0; $i < sizeof($splitField) ;$i++){
      for ($j = 0; $j < sizeof($tfm_databaseFields); $j++){
        $tfmsqlstr = $tfmsqlstr."(".$tfm_databaseFields[$j]." LIKE '%".str_replace("'","''",$splitField[$i])."%')"; 
        if ($j < sizeof($tfm_databaseFields)-1) $tfmsqlstr = $tfmsqlstr." OR "; 
      }
      if ($i < sizeof($splitField) -1) $tfmsqlstr = $tfmsqlstr.") ".$tfm_andor." (";
    }
    $tfmsqlstr = $tfmsqlstr."))";
  }else{
    $tfmsqlstr = " WHERE 1=1 ";
  }
}else{
  $tfmsqlstr = " WHERE 1=1 ";
}
?>
<?php require_once('Connections/connect_front.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

if (!$tfm_search){
		echo "No search term given.";
		exit;
}

// Horizontal looper block 1 -- set up array, row and column values
$HLoop_qSearch = array();
$HLoop_qSearch_rows = $tfm_rows;
$HLoop_qSearch_columns = $tfm_columns;
$HLoop_qSearch_vertical = $tfm_vertical;

if($HLoop_qSearch_vertical == "true") {
	$HLoop_qSearch_loopTo = $HLoop_qSearch_rows;
}else{
	$HLoop_qSearch_loopTo = $HLoop_qSearch_columns;
}
$HLoop_qSearch_i = 0;
$HLoop_qSearch_ii = 0;
$HLoop_qSearch_actualrows = 1;


$maxRows_qSearch = $HLoop_qSearch_rows * $HLoop_qSearch_columns;
$pageNum_qSearch = 0;
if (isset($_GET['pageNum_qSearch'])) {
  $pageNum_qSearch = $_GET['pageNum_qSearch'];
}
$startRow_qSearch = $pageNum_qSearch * $maxRows_qSearch;

$tfmsqlstr_qSearch = " WHERE 1=1 ";
if (isset($tfmsqlstr)) {
  $tfmsqlstr_qSearch = (get_magic_quotes_gpc()) ? $tfmsqlstr : addslashes($tfmsqlstr);
}
mysql_select_db($database_connect_front, $connect_front);
$query_qSearch = sprintf("SELECT entry_id, name, thumbnail, description FROM entry %s ORDER BY name ASC", $tfmsqlstr_qSearch);
$query_limit_qSearch = sprintf("%s LIMIT %d, %d", $query_qSearch, $startRow_qSearch, $maxRows_qSearch);
$qSearch = mysql_query($query_limit_qSearch, $connect_front) or die(mysql_error());
$row_qSearch = mysql_fetch_assoc($qSearch);

if (isset($_GET['totalRows_qSearch'])) {
  $totalRows_qSearch = $_GET['totalRows_qSearch'];
} else {
  $all_qSearch = mysql_query($query_qSearch);
  $totalRows_qSearch = mysql_num_rows($all_qSearch);
}
$totalPages_qSearch = ceil($totalRows_qSearch/$maxRows_qSearch)-1;

if ($HLoop_qSearch_rows < 1){
	$HLoop_qSearch_rows = ceil($totalRows_qSearch / $HLoop_qSearch_columns);
	if($HLoop_qSearch_loopTo < 1) {
		$HLoop_qSearch_loopTo = $HLoop_qSearch_rows;
	}
} 

$queryString_qSearch = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_qSearch") == false && 
        stristr($param, "totalRows_qSearch") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_qSearch = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_qSearch = sprintf("&totalRows_qSearch=%d%s", $totalRows_qSearch, $queryString_qSearch);
if (mysql_num_rows($qSearch) == 0) {
		$result = TRUE;
}
?>
As you can see I have tried in my own lame way to try to get this to work by trying to put the (mysql_num_rows) function somewhere where it will not cause any problems.
Then I tried to echo it in the body:

Code: Select all

<?php
			if ($qSearch = TRUE) {
					echo "Sorry no results.";
					exit;
			}
	?>
Is there any way?

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Only skimming the boards before bed.. but I noticed

Code: Select all

<?php
            if ($qSearch = TRUE) {
                    echo "Sorry no results.";
                    exit;
            }
    ?>
You are using the assigment operator '=' when you should be using the comparison operator '=='

ie.

Code: Select all

<?php
            if ($qSearch == TRUE) {
    ?>
or even

Code: Select all

if ($qSearch) {
User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Post by Sculpture »

Thanks for that. I fixed it to == but it still isn't working.

I think I have to put the (mysql_num_rows) function right after the query but when I do everything falls over.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

If the query that you are dealing with is the one higher up with the $query_limit_qSearch query string then the mysql_num_rows($qSearch) actually should be right after that call If you store the return of that function in a unique variable and then use that variable later you should have no problems.

The problem was that you were using the result of the query and not the result of the mysql_num_rows.

Code: Select all

$qSearch = mysql_query($query_limit_qSearch, $connect_front) or die(mysql_error()); 
$qRes = mysql_num_rows($qSearch);

// a whole bunch of stuff can go here, including HTML

if ($qRes == 0)
{    echo "Sorry, no results";
}
else
{
     // whatever, using $qSearch
}
User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Post by Sculpture »

Thanks for your reply Bill, it is the closest to what my solution would be.

I am still scouring the forums and looking at tuts, text books etc but my knowlege of PHP is still in it's infancy. I know that the "mysql_num_rows" function has to come right after the query but it seems to mess up the recordset. Then again the code isn't working anyway so it might be ok. Also I do not know a way of saying in the else statement, when there are indeed records, continue with the page.

Thanks again.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Well, the mysql_num_rows() doesn't really have to be right after the query, because it is called with the result of the query as a parameter, but I was suggesting that it should be there for clarity. It should not, however, affect the recordset in any way, because it doesn't do anything to the query result. All it does is return the number of rows that were affected by the query.

I do notice that after doing that query and getting the count of rows returned, you then go on to build another query statement, but you never use it in a query. (?)
User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Post by Sculpture »

The second query?

Code: Select all

$queryString_qSearch = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_qSearch") == false && 
        stristr($param, "totalRows_qSearch") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_qSearch = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_qSearch = sprintf("&totalRows_qSearch=%d%s", $totalRows_qSearch, $queryString_qSearch);

Code: Select all

Records <?php echo ($startRow_qSearch + 1) ?> to <?php echo min($startRow_qSearch + $maxRows_qSearch, $totalRows_qSearch) ?> of <?php echo $totalRows_qSearch ?>    
    <table border="0" width="50%" align="center">
      <tr>
        <td width="23%" align="center"><?php if ($pageNum_qSearch > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_qSearch=%d%s", $currentPage, 0, $queryString_qSearch); ?>">First</a>
          <?php } // Show if not first page ?>
        </td>
        <td width="31%" align="center"><?php if ($pageNum_qSearch > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_qSearch=%d%s", $currentPage, max(0, $pageNum_qSearch - 1), $queryString_qSearch); ?>">Previous</a>
          <?php } // Show if not first page ?>
        </td>
        <td width="23%" align="center"><?php if ($pageNum_qSearch < $totalPages_qSearch) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_qSearch=%d%s", $currentPage, min($totalPages_qSearch, $pageNum_qSearch + 1), $queryString_qSearch); ?>">Next</a>
          <?php } // Show if not last page ?>
        </td>
        <td width="23%" align="center"><?php if ($pageNum_qSearch < $totalPages_qSearch) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_qSearch=%d%s", $currentPage, $totalPages_qSearch, $queryString_qSearch); ?>">Last</a>
          <?php } // Show if not last page ?>
        </td>
      </tr>
    </table>
I am looking at finding a way to manipulate this. Am I going off track?

Thanks
User avatar
Sculpture
Forum Commoner
Posts: 41
Joined: Sat Jun 11, 2005 6:57 pm
Location: Australia
Contact:

Post by Sculpture »

Going back to the original idea. I put the "mysql_num_rows" function back in the recordset, after the query and in the BODY I put

Code: Select all

<?php 
	if ($res == 0) {
			echo "No matching records.";
			exit; 
			}
	
	?>
To my mind if there are records returned the script should run as normal but I get.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'%t%\') OR (description LIKE \'%t%\'))) ORDER BY name ASC LIMIT
And if there are no records the "No matching records" should be echo'd.

Thanks
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Sculpture wrote: To my mind if there are records returned the script should run as normal but I get.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'%t%\') OR (description LIKE \'%t%\'))) ORDER BY name ASC LIMIT
Where is the query? Echo the query to the browser and post it here....

EDIT:

Code: Select all

'\'%t%\'
From what I could see here, is if you want to include single quote in your seach then single quotes should be within %%, like this....

Code: Select all

'%\'t\'%'
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Well, something is wrong with your query string, but that error isn't consistent with the querystring posted in the code earlier. I'm also not sure what you mean by "I put the "mysql_num_rows" function back in the recordset"

It's kind of hard to follow when your code is a) presented piecemeal and b) contains no whitespace which makes it rather hard to read.
Post Reply