Nov
28

PHP Fatal error: Allowed memory size of xxx bytes exhausted

PHP          Trackback

Familiar with this error code? I was working on some PHP code that is doing bunch of data analysis and I got that error. There is one line of code that can help you to fix this problem. Just put this line at the top of the script:
ini_set('memory_limit', '16M');
This will set memory limit to 16MB. If this is not helping, try to increase memory limit even more.
If you have access to php.ini and you would like to increase memory limit for all your scripts you could add this line to the server’s php.ini file:
memory_limit = 16M

If you see this message when you are running your script that is frequently executed, I would recommend you to check your code first, because most common case when you are going to see this message is whan your code is poorly written and is needlessly eating too much memory.


Oct
15

Export MySQL To Excel better way

PHP, SQL          Trackback

I showed you how to export your MySQL tables to MS Excel file, but that wasn’t real excel files we were creating. Actually you were getting CSV and HTML files which excel can read. These were textual files, and now I am gonna show you how to create binary excel file: [SOURCE CODE]



/*
Export MySQL to Excel (binary mode)
Author: Vlatko Zdrale, http://blog.zemoon.com

Look but don't touch :)
*/
$dbHost = 'myserver'; // database host
$dbUser = 'myusername'; // database user
$dbPass = 'mypassword'; // database password
$dbName = 'mydatabase'; // database name
$dbTable = 'mytable'; // table name

$connection = @mysql_connect($dbHost, $dbUser, $dbPass) or die("Couldn't connect.");
$db = mysql_select_db($dbName, $connection) or die("Couldn't select database.");

function excelHeader() {
$now_date = date('m_d_Y'); //date for title

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=export_$now_date.xls");
header("Content-Transfer-Encoding: binary ");

echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}

function excelFooter() {
echo pack("ss", 0x0A, 0x00);
return;
}

function excelWriteNumber($row, $col, $value) {
echo pack("sssss", 0x203, 14, $row, $col, 0x0);
echo pack("d", $value);
return;
}

function excelWriteLabel($row, $col, $value ) {
$len = strlen($value);
echo pack("ssssss", 0x204, 8 + $len, $row, $col, 0x0, $len).$value;
return;
}

excelHeader();

$sql = "SELECT * FROM `$dbTable`";
//execute query
$result = @mysql_query($sql)
or die("Couldn't execute query:
“.mysql_error().’
‘.mysql_errno().’
‘.$sql);

for ($i = 0; $i < mysql_num_fields($result); $i++) //print column names as names of MySQL fields
excelWriteLabel(0, $i, mysql_field_name($result, $i));

$excelRow = 1;
while($row = mysql_fetch_row($result)) {
$excelCol = 0;
foreach($row as $value) {
if (is_int($value))
excelWriteNumber($excelRow, $excelCol, $value);
else
excelWriteLabel($excelRow, $excelCol, $value);
$excelCol++;
}
$excelRow++;
}
excelFooter();
?>


Oct
04

Simple PHP breadcrumb navigation

PHP, Tutorials          Trackback

I was working on one application this morning and I needed breadcrumb links on the top. First thought that came to me was: ‘recursion’. That’s scary word, for memory eating functions, but at the same time that is easiest way to create breadcrumbs navigation. All you have to do is to create MySQL table, with these fields: (category_id, parent_id, category_name)
Top categories must have parent_id set to 0. And here goes that simple recursive printbreadcrumb function:

function printbreadcrumb($category_id) {
	$sql = "SELECT category_id, parent_id, category_name
				FROM categories
				WHERE category_id=$category_id";
	$result	= mysql_query($sql) or die('something is wrong here');
	$row = mysql_fetch_array($result);
	if ($row['parent_id']) { // if this node has parent
		printbreadcrumb($row['parent_id']); // make recursive call to this function
	}
	echo $row['categories_name'].' > '; // print nodes one by one
	return;
}

You are calling this function like this: printbreadcrumb($category_id)

Some would say that this is slow script, because you’ll have same number of sql calls, as you have levels. What you can do is to change this function to work with arrays and to load that complete table in one array.


Sep
04

show PHP errors

PHP, htaccess          Trackback

Did you ever work on some php script, and all you get when you point your browser to that script was blank page? Just emptiness. You would expect error message at least. Probably your server is configured not to display and log errors.

Try to add these two lines to your .htaccess file:


php_flag display_errors on
php_value error_reporting 7

That can solve your problem


Sep
03

PHP code inside HTML page

PHP, htaccess          Trackback

Did you ever try to put some PHP code inside your HTML page? It might work, it might not. It depends on your webserver settings. Usually you dont have access to this settings, but you can use .htaccess file to fix this problem. All you have to do is just to put this line into your .htaccess file:

AddType application/x-httpd-php .html

With this line your web server will interpret all your html files like they are php.


top