I am having an issue with a basic query in PHP. The query runs perfectly in PHPMyAdmin, but in the browser I am getting:
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, object given in /home/######/public_html/test.php on line 54
I've tried single quotes and double quotes but I'm getting nowhere fast. Below is my full code so far:
//display errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
//init Joomla Framework
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__).'/' ); // print this out or observe errors to see which directory you should be in (this is two subfolders in)
define( 'DS', DIRECTORY_SEPARATOR );
//echo JPATH_BASE;
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_CONFIGURATION .DS.'configuration.php' );
require_once ( JPATH_LIBRARIES .DS.'joomla'.DS.'database'.DS.'database.php' );
require_once ( JPATH_LIBRARIES .DS.'import.php' );
//DB Connection
$Config = new JConfig();
$db_driver = $Config->dbtype; // Database driver name
$db_host = $Config->host; // Database host name
$db_user = $Config->user; // User for database authentication
$db_pass = $Config->password; // Password for database authentication
$db_name = $Config->db; // Database name
$db_prefix = $Config->dbprefix; // Database prefix (may be empty)
// Database prefix (if empty then remove prefixing double underscore)
$db_prefix = (trim($db_prefix)=="") ? "":$db_prefix;
//echo $db_prefix;
$db_connect = mysqli_connect($db_host,$db_user,$db_pass);
$content_count = 0;
//CONNECTED! so run a SQL query as per usual
if (!mysqli_connect_errno()) {
$query="SELECT COUNT(*) as ProductSku FROM ".$db_prefix."virtuemart_products";
if ($result = mysqli_query($db_connect, $query, MYSQLI_USE_RESULT)) {
while($obj = $result->fetch_object()){
$content_count = $obj->ProductSku;
}
}
}
//echo $query;
//echo $content_count;
mysqli_free_result( $db_connect );
Update
Ok, so the code below works fine, but what I want to do is return all records where the created_on
field has today's date. (rather than the simple query I quoted first, this was just to try and get the code working) Thus the query would be:
SELECT product_sku FROM #__virtuemart_products WHERE created_on <= NOW();"
Following this I want to export to a csv file, but I am getting an error:
Fatal error: Call to a member function fetch_fields() on a non-object in
Here is my full code so far:
//display errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
//init Joomla Framework
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__).'/');
define('DS', DIRECTORY_SEPARATOR);
echo JPATH_BASE;
require_once (JPATH_BASE .DS. 'includes' .DS. 'defines.php');
require_once (JPATH_BASE .DS. 'includes' .DS. 'framework.php');
// Start database stuff
$db = JFactory::getDbo();
//Select records that were created today <------This works but i can't get it to //work in the joomla API.
$query = "SELECT product_sku FROM #__virtuemart_products WHERE created_on <= NOW();";
$db->setQuery($query);
$result = $db->loadObjectList();
//print_r($result);
if (!$result) die('Couldn\'t fetch records');
$headers = $result->fetch_fields();
foreach($headers as $header) {
$head[] = $header->name;
}
$fp = fopen('/home/#####/public_html/test1.csv', 'x');
if ($fp && $result) {
fputcsv($fp, array_values($head));
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
?>