It is sometimes necessary to run two instances of mysql, like in my case. I need a mysql database in addition to the one Zimbra uses. One solution to this problem is to run the mysql database on a non default socket. This can be done by changing the following line in my.cnf
my.cnf
[client] ... socket = /var/run/mysqld/mysqld2.sock
Restart the mysql daemon afterwards to activate the change.
In PHP it’s possible to avoid programming the nondefault socket info whenever we use mysql
php.ini
[MySQL] ... mysql.default_socket = /var/run/mysqld/mysqld2.sock
If you don’t have access to php.ini you have to program this in your PHP code
$dbcnx = @mysql_connect('localhost:/var/run/mysqld/mysqld2.sock',$username, $password);
Posted by Hans-Henry Jakobsen
On my eZDB I’ve found two different scenarios of corrupt objects, they must have become corrupt when not having transaction enabled, and eZp or the user breaks out of a content object creation, og content object edit:
This script report, and does the required job:
<?php
// Script for finding and handling content_objects that are not completely created
// That may occur under some circustances when using a database without transations enabled
//
//
// 2007.09.20, jonny.bergkvist@hit.no
// $doUpdate, true or false. Set to false for at dry test-run
$doUpdate = true;
include_once( 'kernel/common/template.php' );
include_once( "lib/ezutils/classes/ezhttptool.php" );
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/ezscript.php' );
include_once( 'lib/ezdb/classes/ezdb.php' );
$cli =& eZCLI::instance();
$script =& eZScript::instance();
$script->initialize();
$db =& eZDB::instance();
set_time_limit( 0 );
$arrayResult1 = $db->arrayQuery( "SELECT id, current_version FROM ezcontentobject" );
echo "First checking for content objects that has no contentobject_attributes at all...n";
$i = 0;
foreach( $arrayResult1 as $item) {
//check if object has no attributes of any version stored
$hasAttribute = $db->arrayQuery( "SELECT contentobject_id FROM ezcontentobject_attribute WHERE contentobject_id = " . $item['id'] );
if ( empty( $hasAttribute ) ) {
echo "Corrupt object: " . $item['id'] . ". ";
if ( $doUpdate ) {
echo "Deleting corrupt object with no attributes...n";
$db->query( "DELETE FROM ezcontentobject WHERE ezcontentobject.id = " . $item['id'] );
}
$i++;
}
}
echo "Total corrupt objects with no attributes: " . $i . "nn";
$arrayResult2 = $db->arrayQuery( "SELECT id, current_version FROM ezcontentobject" );
echo "Then checking for content objects that has contentobject_attributes, but not of the current_version...n";
$i = 0;
foreach( $arrayResult2 as $item) {
//check if current_version has content attributes
$hasAttribute = $db->arrayQuery( "SELECT contentobject_id FROM ezcontentobject_attribute WHERE contentobject_id = " . $item['id'] . " AND version = " . $item['current_version'] );
if ( empty( $hasAttribute ) ) {
echo "Corrupt object: " . $item['id'] . ", current_version: " . $item['current_version'] . ". ";
if ( $doUpdate ) {
$previousCurrentVersion = $item['current_version'] - 1;
echo "Setting back to version: " . $previousCurrentVersion . "n";
$db->query( "UPDATE ezcontentobject SET current_version = " . $previousCurrentVersion . " WHERE id = " . $item['id'] );
}
$i++;
}
}
echo "Total objects with wrong current_version: " . $i . "n";
$script->shutdown();
?>
Source: http://ez.no/developer/forum/general/problem_corrupt_contentobjects
Tags: Apache, eZ Publish, Flau Bris, PHP
Posted by Hans-Henry Jakobsen
Function to check if a URL is online/exists.
function is_valid_url($url)
{
$url = @parse_url($url);
if (!$url)
{
return false;
}
$url = array_map('trim', $url);
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
$path = (isset($url['path'])) ? $url['path'] : '';
if ($path == '')
{
$path = '/';
}
$path .= (isset($url['query'])) ? "?$url[query]" : '';
if (isset($url['host']) AND $url['host'] != gethostbyname($url['host']))
{
if (PHP_VERSION >= 5)
{
$headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
}
else
{
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
if (!$fp)
{
return false;
}
fputs($fp, "HEAD $path HTTP/1.1rnHost: $url[host]rnrn");
$headers = fread($fp, 4096);
fclose($fp);
}
$headers = (is_array($headers)) ? implode("n", $headers) : $headers;
return (bool)preg_match('#^HTTP/.*s+[(200|301|302)]+s#i', $headers);
}
return false;
}
Example
if (is_valid_url('http://pario.no'))
{
do_something();
}
Source: http://www.bytemycode.com
Tags: PHP
Posted by Hans-Henry Jakobsen
So here’s how I parsed to flat files with e-mail addresses (nothing special about it)
approved_emails.txt has the following emails in it:
check_emails.txt is composed of:
My php file that I’m actually running from a command prompt is check_email_addresses.php
You could pipe this out to another file if you needed it for later use.
php check_email_addresses.php > delete_these_addresses.txt
Tags: PHP
Posted by Hans-Henry Jakobsen
Connect to MySQL and to the database
// Connect to MySQL
$connect = mysql_connect("localhost", "mysqluser", "userpassword") or die(mysql_error());
// Select the database
mysql_select_db("databasename", $connect) or die(mysql_error());
PHP Manual Reference
us2.php.net/manual/en/function.mysql-connect.php
Close connection
// Use the variable that was used to connect to MySQL mysql_close($connect) or die(mysql_error()); // PHP Manual Reference: us2.php.net/manual/en/function.mysql-close.php
Create a database
mysql_create_db("dbname") or die(mysql_error());
// PHP Manual Reference: us2.php.net/manual/en/function.mysql-create-db.php // W3Schools Reference: www.w3schools.com/sql/sql_create.asp
Delete (drop) a database
$query_drop_db = "DROP DATABASE dbname"; mysql_query($query_drop_db, $connect) or die(mysql_error());
// PHP Manual Reference: us2.php.net/manual/en/function.mysql-drop-db.php // W3Schools Reference: www.w3schools.com/sql/sql_drop.asp
Create a table
/* If you don't want your script return an error if the table
already exists, you can replace "CREATE TABLE" by
"CREATE TABLE IF NOT EXISTS" which creates the table only
if the table does not already exist, without returning any errors. */ $table = "CREATE TABLE tablename ( table_id int(11) NOT NULL auto_increment, table_default int(11) NOT NULL default 1, table_varcharfield varchar(255) NOT NULL, table_textfield text NOT NULL, PRIMARY KEY (table_id) )"; // Actually create the new table in the database. $result = mysql_query($table) or die (mysql_error());
// W3Schools Reference: www.w3schools.com/sql/sql_create.asp
Select data
// Select specific fields in the table $query_select = "SELECT tablefield, tablefield2 FROM tablename WHERE tablefield = '" .$variable. "' AND tablefield2 = '" .$variable2. "'"; $result_select = mysql_query($query_select) or die(mysql_error());
// W3Schools Reference: www.w3schools.com/sql/sql_select.asp // W3Schools Reference ("where" clause): www.w3schools.com/sql/sql_where.asp
// Select all the fields in the table $query_select = "SELECT * FROM tablename WHERE tablefield = '" .$variable. "' AND tablefield2 = '" .$variable2. "'"; $result_select = mysql_query($query_select) or die(mysql_error());// W3Schools Reference: www.w3schools.com/sql/sql_select.asp
Select data: Retrieve and display
// Retrieve as several variables
$row_select = mysql_fetch_array($result_select);
extract($row_select);
echo $tablefield;
// Retrieve as a loop
while ($row_select = mysql_fetch_array($result_select)) {
extract($row_select);
echo $tablefield;
}
Insert data
$query_insert = "INSERT INTO tablename(tablefield, tablefield2) VALUES('" .$variable. "', '" .$variable2. "')";
$result_insert = mysql_query($query_insert) or die(mysql_error());
// PHP Manual Reference: us2.php.net/manual/en/function.mysql-insert-id.php
// W3Schools Reference: www.w3schools.com/sql/sql_insert.asp
Insert data: Insert multiple rows
$query_insert = "INSERT INTO tablename(tablefield, tablefield2) VALUES ('" .$variable. "', '" .$variable2. "'),
('" .$variable. "', '" .$variable2. "'),
('" .$variable. "', '" .$variable2. "')";
$result_insert = mysql_query($query_insert) or die(mysql_error());
Update data
$query_update = "UPDATE tablename SET tablefield = '" .$variable. "', tablefield2 = '" .$variable. "' WHERE tablefield3 = '" .$variable3. "'"; $result_update = mysql_query($query_update) or die(mysql_error()); // W3Schools Reference: www.w3schools.com/sql/sql_update.asp
Delete data
// Delete a row from a table $query_delete = "DELETE FROM tablename WHERE tablefield = '" .$variable. "'"; $result_delete = mysql_query($query_delete) or die(mysql_error()); // W3Schools Reference: http://www.w3schools.com/sql/sql_delete.asp
When you create database tables, you need field types. Certain field types are for certain reasons. Here is a small list of some of the more commonly used field types.
| Field Name | Description |
|---|---|
| int(length) | Integer field that can range from negative 2147483648 to positive 2147483647. length can limit the length of the integer. |
| int(length) unsigned | Same as int, but can store up to 4294967295, but no negative numbers. |
| char(length) | Any character can be in this field, but the field is at a static length. |
| varchar(length) | Any character can be in this field. The field value length can be from 1 to 255 characters. length is the maximum length it will store. |
| text | Any characters can be in this field. The maximum size the value can be is 65536 characters. |
| decimal(length, decimal) | A field for decimals, where length defines the length of the decimal (as a whole), and the decimal parameter is the number of decimal places. |
| datetime | Field that stores both date and time. Stores as yyyy-mm-dd hh:mm:ss. |
Posted by Hans-Henry Jakobsen