0
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__).'/..' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');

date_default_timezone_set('Asia/Kolkata');
$catid1 = urldecode($_POST['catid1']) ;
$catid2 = urldecode($_POST['catid2']) ;
$sql = "SELECT `id` , `title` , `introtext` , `created` FROM `goqc9w_content` WHERE ( `catid` = $catid1 OR `catid` = $catid2 ) AND `created` >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) ORDER BY `created` DESC" ;

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$db->setQuery($sql);
$results = $db -> loadAssocList();

echo "Echo<br>" ;

if(count($results) > 0) {
  $json = array() ;
  $i = 0 ;
  while($i < count($results)) {
    $results[$i]['title'] = mb_convert_encoding($results[$i]['title'] , "UTF-8") ;
    $results[$i]['img_src'] = (string) reset(simplexml_import_dom(DOMDocument::loadHTML($results[$i]['introtext']))->xpath("//img/@src")) ;
    $results[$i]['introtext'] = mb_convert_encoding($results[$i]['introtext'] , "UTF-8") ;
    $json[] = $results[$i] ;
    $i = $i + 1 ;
    echo "Inside loop<br>" ;
  }
  echo "Outside loop<br>" ;
  print json_encode($json) ;
} else {
    echo "0" ;
}?>

I have to fetch data from goqc9w_content table of my Joomla Database . Above PHP code was working fine before changing server's IP . But after changing server's IP , it is not working and no data is being fetched from Database . The interested thing is that it is neither executing statements of IF block ( if(count($results) > 0) ) nor statements of ELSE block . It is only printing "Echo" and no other errors have been shown . So please help me .

Anuj Gupta
  • 85
  • 1
  • 7
  • check whether the database is connecting in your code. – user3040610 Jan 23 '17 at 09:46
  • By changing the IP do you mean moving the site to a new server? – RiggsFolly Jan 23 '17 at 09:54
  • Did the database also move. Is the connection the same as before? Did you change the connection to match the new server? – RiggsFolly Jan 23 '17 at 09:57
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jan 23 '17 at 09:58
  • Does all your database access fail or just this specific piece of code – RiggsFolly Jan 23 '17 at 10:02
  • How to check whether Joomla database is connected or not ? @ user3040610 – Anuj Gupta Jan 23 '17 at 13:35
  • As I know , $db = JFactory::getDbo() automatically fetches the current configuration of server . So there is no need to do changes manually . Even I have not written code manually for connection . @ RiggsFolly – Anuj Gupta Jan 23 '17 at 13:39
  • All other PHP files also fail to access data . @ RiggsFolly – Anuj Gupta Jan 23 '17 at 13:45
  • Thanks for SQL Injection problem . But there are no user inputs , An Android application uses this file to fetch data and post variables are sent by this application only . So probably there is no need to worry about SQL Injection . – Anuj Gupta Jan 23 '17 at 14:00
  • Try to do a var dump on JPATH_BASE. i.e var_dump(JPATH_BASE); What results do you get? – Amit Ray Jan 24 '17 at 05:51
  • string(44) "/home/indiansportsnews/public_html/static/.." This is what I am getting @ Amit Ray – Anuj Gupta Jan 26 '17 at 10:34
  • Same file is working fine in localhost ... I have echoed count($result) to see whether rows are being fetched or not .. And it is echoing 566 , that means rows are fetched . Now why is it not going inside IF block ? @ Amit Ray – Anuj Gupta Jan 26 '17 at 10:52

0 Answers0