Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Multiple mysql_fetch_array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Multiple mysql_fetch_array

Postby neo115162 » Sat Mar 16, 2013 3:12 am

Hello,

I am having problems retrieving results for a particular query which is for events. Every time I type a particular word to search for in the news table I get results and when I do for events it doesn't show. I tried troubleshooting the problem and realized that if I do print mysql_num_rows($resEvents); I will get a result which is more than 0.

Code: Select all
<?php
require_once 'config.php';
require_once 'sXmAdmin/classes/DB_connection.php';
require_once 'UIfunctions.php';

if(strtolower($_GET['lang'])!='en' && strtolower($_GET['lang'])!='nl')
   header( 'Location: ./' ) ;   //Redirect to index page

if (strlen($_REQUEST["query"])>=3){

   try {
      function search_split_terms($terms){
      
         $terms = preg_replace("/\"(.*?)\"/e", "search_transform_term('\$1')", $terms);
         $terms = preg_split("/\s+|,/", $terms);
      
         $out = array();
      
         foreach($terms as $term){
      
            $term = preg_replace("/\{WHITESPACE-([0-9]+)\}/e", "chr(\$1)", $term);
            $term = preg_replace("/\{COMMA\}/", ",", $term);
      
            $out[] = $term;
         }
      
         return $out;
      }
      
      function search_transform_term($term){
         $term = preg_replace("/(\s)/e", "'{WHITESPACE-'.ord('\$1').'}'", $term);
         $term = preg_replace("/,/", "{COMMA}", $term);
         return $term;
      }
      
      function search_escape_rlike($string){
         return preg_replace("/([.\[\]*^\$])/", '\\\$1', $string);
      }
      
      function search_db_escape_terms($terms){
         $out = array();
         foreach($terms as $term){
            $out[] = '[[:<:]]'.AddSlashes(search_escape_rlike($term)).'[[:>:]]';
         }
         return $out;
      }
      
      $terms = search_split_terms($_REQUEST["query"]);
      $terms_db = search_db_escape_terms($terms);
      
/**********************************************************************************/      
      $partsNews = array();
      foreach($terms_db as $term_db){
         $partsNews[] = "".strtolower($_GET['lang'])."_heading RLIKE '$term_db'
                  OR ".strtolower($_GET['lang'])."_story RLIKE '$term_db'";
      }
      $partsNews = implode(' AND ', $partsNews);
      
/**********************************************************************************/
      $partsEvents = array();
      foreach($terms_db as $term_db){
         $partsEvents[] = "".strtolower($_GET['lang'])."_title RLIKE '$term_db'
         OR ".strtolower($_GET['lang'])."_description RLIKE '$term_db'";
      }
      $partsEvents = implode(' AND ', $partsEvents);
      
/**********************************************************************************/
      
      $connect = new DB_connection();
   
      $resNews = $connect->get_query("SELECT id,".strtolower($_GET['lang'])."_heading,".strtolower($_GET['lang'])."_story,DATE_FORMAT(date_ins, '%b %d %Y, %h:%i %p')
                              FROM news
                              WHERE ($partsNews)
                              AND ".strtolower($_GET['lang'])."_heading IS NOT NULL
                              AND publish = 'yes'");
      
      $resEvents = $connect->get_query("SELECT id,".strtolower($_GET['lang'])."_title,".strtolower($_GET['lang'])."_description,DATE_FORMAT(date_ins, '%b %d %Y, %h:%i %p')
                              FROM events
                              WHERE ($partsEvents)
                              AND ".strtolower($_GET['lang'])."_title IS NOT NULL
                              AND event_datum >= NOW()
                              AND publish = 'yes'");
         
   }catch (Exception $error){
      print '<div class="emsg">'.$error->getMessage().'</div>';      //Display error message
      die();                                             //Stop anything else after this line
   }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex, nofollow" />
<title><?php print stripslashes(siteName); ?></title>
<?php require('includes/css.php'); ?>
<script type="text/javascript" src="stmenu.js"></script>

<script type="text/javascript" src="./jquery/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="./js/clear-default-text.js"></script>

<?php
include 'includes/banner_rotator_files.php';
?>
</head>
<body>
<div id="main_container">
 
 <div id="header_container">
 
   <div id="topheader">
   <?php require("includes/language.php"); ?>
   </div>
   <div id="menu"><?php require('menu.php'); ?>
</div>
 </div>
 <div class="main">
   <?php require("includes/mid_section_docs.php"); ?>
   <div id="content_wrapper">
   <?php require("includes/content_side.php"); ?>
   <?php require("includes/motto.php"); ?>
  <div id="right_col"> 
  <?php
if ($_GET['lang']=='en') {
?>
  <h1>Search results</h1>
    <?php
}else{
?>
<h1>Zoek resultaat</h1>
  <?php
}

if (strlen($_REQUEST["query"])>=3){
?>
     <table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
   while (($arrNews=mysql_fetch_array($resNews, MYSQL_NUM))!==FALSE) {
?>
      <tr>
          <td><a href="news_details.php?lang=<?php print $_GET['lang'];?>&newsid=<?php print $arrNews[0];?>"><?php print html_entity_decode($arrNews[1]);?></a> [<i><?php print $arrNews[3]; ?></i>]</td>         
      </tr>
      <tr>
         <td><?php print limit_characters(html_entity_decode($arrNews[2]),150);?></td>
      </tr>   
<?php              
   }
   mysql_free_result($resNews);
?>
<?php

   while (($arrEvents=mysql_fetch_array($resEvents, MYSQL_NUM))!==FALSE) {
?>
      <tr>
          <td><a href="eventdetails.php?lang=<?php print $_GET['lang'];?>&eid=<?php print $arrEvents[0];?>"><?php print html_entity_decode($arrEvents[1]);?></a> [<i><?php print $arrEvents[3]; ?></i>]</td>         
      </tr>
      <tr>
         <td><?php print limit_characters(html_entity_decode($arrEvents[2]),150);?></td>
      </tr>   
<?php              
   }
   mysql_free_result($resEvents);
?>   
   
   </table>
<?php
}else{
?>
   <?php if (strtolower($_GET['lang'])=='en'){?>No items matched your search criteria.
   <?php }else{ ?>Er zijn geen zoekresultaten gevonden voor uw zoekopdracht.
   <?php } ?>
   <form action="searchresults.php?lang=<?php print $_GET['lang'];?>" method="post">
        <input type="text" name="query" />
        <input type="hidden" name="lang" value="<?php print $_GET['lang'];?>" />
        <?php if ($_GET['lang']=='en') { ?>
        <input type="submit" value="Search" />
        <?php }else{ ?>
      <input type="submit" value="Zoeken" />
      <?php } ?>
    </form>
<?php        
}
?>   
  </div>
</div>
</div>
 </div>

 <div id="footer_container"><?php require("includes/footer.php"); ?></div>
</body>
</html>



Can anybody help me with this issue?
neo115162
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Mar 16, 2013 3:09 am

Re: Multiple mysql_fetch_array

Postby neo115162 » Sun Mar 17, 2013 1:45 am

Maybe my question is unclear.
I am calling two queries, namely;

Code: Select all
$resNews = $connect->get_query("SELECT id,".strtolower($_GET['lang'])."_heading,".strtolower($_GET['lang'])."_story,DATE_FORMAT(date_ins, '%b %d %Y, %h:%i %p')
FROM news
WHERE ($partsNews)
AND ".strtolower($_GET['lang'])."_heading IS NOT NULL
AND publish = 'yes'");


$resEvents = $connect->get_query("SELECT id,".strtolower($_GET['lang'])."_title,".strtolower($_GET['lang'])."_description,DATE_FORMAT(date_ins, '%b %d %Y, %h:%i %p')
FROM events
WHERE ($partsEvents)
AND ".strtolower($_GET['lang'])."_title IS NOT NULL
AND event_datum >= NOW()
AND publish = 'yes'");


I am displaying them separately, like this;

Code: Select all
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<?php
while (($arrNews=mysql_fetch_array($resNews, MYSQL_NUM))!==FALSE) {
?>
<tr>
   <td><a href="news_details.php?lang=<?php print $_GET['lang'];?>&newsid=<?php print $arrNews[0];?>"><?php print html_entity_decode($arrNews[1]);?></a> [<i><?php print $arrNews[3]; ?></i>]</td>     
</tr>
<tr>
<td><?php print limit_characters(html_entity_decode($arrNews[2]),150);?></td>
</tr>   
<?php             
}
mysql_free_result($resNews);
?>
<?php
while (($arrEvents=mysql_fetch_array($resEvents, MYSQL_NUM))!==FALSE) {
?>
<tr>
   <td><a href="eventdetails.php?lang=<?php print $_GET['lang'];?>&eid=<?php print $arrEvents[0];?>"><?php print html_entity_decode($arrEvents[1]);?></a> [<i><?php print $arrEvents[3]; ?></i>]</td>     
</tr>
<tr>
<td><?php print limit_characters(html_entity_decode($arrEvents[2]),150);?></td>
</tr>   
<?php             
}
mysql_free_result($resEvents);
?>
</table>


I am not getting any results for events, only for news.
Can anyone help me?
neo115162
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Mar 16, 2013 3:09 am

Re: Multiple mysql_fetch_array

Postby johnj » Sun Mar 17, 2013 8:28 pm

Did you check the database if there are any records for events?
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron