Posts Under php Category
A PHP Mail Class
Below is a Mail class I created in PHP, it requires 4 arguments on initiation of the class via the constructor and uses the method ->send() to send the created mail once complete. class Mail { var $to; var $subject; var $content; var $headers; function Mail($to, $subject, $content, $from){ …
unexpected T_IF
Getting “Parse error: syntax error, unexpected T_IF” ? Check if the preceeding line ends with a ;
MySQL Main Query Types used in PHP to select, insert, update and delete data
MySQL Main Query Types SELECT SELECT * FROM tablename INSERT INSERT INTO tablename (col1, col2, col3,..) VALUES (val1, val2, val3,…) UPDATE UPDATE tablename SET `col1`=`val1`, `col2`=`val2`, `col3`=`val3`,… DELETE DELETE FROM tablename WHERE `col4`=`val6` Note You will use a lot of WHERE clauses as well along with the above. e.g. SELECT * FROM tablename WHERE `id`=’15′ In PHP Calling to MySQL is really easy in PHP, just use the mysql_query() function….
Connect to mysql database from php
If you need to connect to a mysql database from php you can do it like this: <?php> $DBH = ‘localhost’; $DBU = ‘root’; $DBPWD = ‘password’; $DBN = ‘petstore’; $conn = mysql_connect($DBH, $DBU, $DBPWD) or die ("Error: Could not connect to database."); mysql_select_db($dbname); ?> This allows you to make a connection to the mysql database and gets it ready for you to make…
Get all directories in PHP
I think this may be the fastest way to get a list of all the directories in a given folder/directory using PHP. $directories = array_filter(glob(‘*’), ‘is_dir’); ..and of course you can also add a path to it if you need to: $directories = array_filter(glob($myPath.‘*’), ‘is_dir’);
Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource
Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource You have just gotten a warning when trying to check if an inserted row was successful but when calling mysql_affected_rows(mysql_query(“INSERT INTO……”)) you get a warning telling you that the supplied argument is not a valid MySQL-Link resource. This is because mysql_query INSERT actually returns true or false and not a resource id. So rather do this: if (mysql_query(“INSERT INTO…”))
What does __FILE__ in php mean?
In php there are a few magic constants and I find __FILE__ to be a very useful one. It returns the full path and filename of the file in question. Take a look at the PHP Manual – Magic Constants for more information on the others.
Change PHP Session Time
The default time for a php session is 1440 seconds. You can change it by doing the following: ini_set("session.gc_maxlifetime",1440); You can obviously adjust the second parameter (1440) to anything you like to change the timeout limit.
Get current working directory of php script/application
$myPath = realpath(dirname(__FILE__)); or $myPath = getcwd();
Force Download in PHP
This script works in all browsers, including Internet Explorer! if (strstr($_SERVER[‘HTTP_USER_AGENT’],"MSIE")) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); header("Content-Type: application-download"); header("Content-Disposition: attachment; filename=\"".basename($filename)."\";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".@filesize($ab_file)); set_time_limit(0); } else { header("Content-type: application-download"); header("Content-Length: ".filesize($ab_file)); header("Content-Disposition attachment; filename=".$filename); } readfile($ab_file);
shortText function toggler in php
I often have to show a shortened version of a news item and then a link to show the entire news article or whatever. Here’s a way to get that short text! function shortText($text, $length) { if( strlen($text) > $length ) return substr(preg_replace("/(<\/?)(\w+)([^>]*>)/", "", $text), 0, $length)."…"; return preg_replace("/(<\/?)(\w+)([^>]*>)/", "", $text); }
PHP – file() lines
$lines = file(‘all-words.txt’); foreach ($lines as $line_num => $line) { echo "Line number ".$line_num; }