Archive

Posts Tagged ‘update’

Getting Started With Zend_Lucene

February 20th, 2009 1 comment

Zend_Lucene is an implementation of the Lucene search engine in PHP5 and is included as part of the Zend Framework from version 1.6. Lucene implements all of the standard search engine query syntaxes (eg. boolean and wildcard searches) and stores its index as files so it doesn’t need a database server to run. Lucene can be used if you want to add search functionality to a site but don’t want to go down the route of building a querying syntax from scratch.

To get started with Lucene you need to create an index. The following code has the effect of creating a directory on your server that Lucene will use to store and retrieve documents.

$index = Zend_Search_Lucene::create('/data/my-index');

To open the index use the following code.

$index = Zend_Search_Lucene::open('/data/my-index');

Of course your index will not contain anything so the next step is to add some documents to it.

To create a new document you need to create a new document object. This is done using the Zend_Search_Lucene_Document() class.

$doc = new Zend_Search_Lucene_Document();

You can then assign fields to this document using the static functions of the Zend_Search_Lucene_Field class.

$doc->addField(Zend_Search_Lucene_Field::Text('title', 'The title of the document'));
$doc->addField(Zend_Search_Lucene_Field::Text('contents', 'The contents of the document.'));

You can also use binary data, which is useful if you have used a document scanning service and want to be able to search the data at a later date.

$doc->addField(Zend_Search_Lucene_Field::Binary('originalfile', $filedata));

Any binary data you assign like this isn’t tokenized or indexed but it is stored in the index so you would need to assign other fields so that the data can be searched for.

Once you have added your fields you can add the document using the addDocument() function of the index opened index object.

$index->addDocument($doc);

If you are building a search index for a site then you might want to use the built in HTML parsing functionality. This makes it easy for you to add either a HTML string or a HTML filename that Lucene will then index. You then add this file to the index using the addDocument() function of the opened index object. Note that when adding documents in this way you should also add the URL of the document as a field so that you can retrieve it later.

$doc = Zend_Search_Lucene_Document_Html::loadHTMLFile('http://www.talkincode.com/');
$doc->addField(Zend_Search_Lucene_Field::Text('url','http://www.talkincode.com/'));
$index->addDocument($doc);

You can also index and search Word, Excel and Powerpoint documents in much the same way as this.

Once you have the index you can search it. This is done using an opened index object, you can find out how big your index is and how many documents you have in your index by using the count() and numDocs() functions receptively.

$indexSize = $index->count();
$documents = $index->numDocs();

To construct a query and implement the boolean and wildcard searching you need to use the Zend_Search_Lucene_Search_QueryParser class, this is then passed onto the Zend_Search_Lucene_Search_Query_Boolean object using the addSubquery() function.

$queryStr = 'talk';
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
 
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($userQuery, true);
 
  // do the search
$hits = $index->find($query);

The variable $hits now contains an array of the Zend_Search_Lucene_Search_QueryHit object. This object has a property called score, which is the score of the hit result. The score is an indication (between 0 and 1) of how closely the query matched the index. The first item in the $hits array will have the highest score value. Every field that you defined for the document whilst indexing is now presented as a property of this object. So if you set a URL field for your document you can see a list of your documents using the following code:

$hits = $index->find($query);
foreach ($hits as $hit) {
 echo $hit->score.'<br />';
 echo $hit->url.'<br />';
}

Lucene can do a lot more than what I have briefly detailed here so I might write some posts in the future on how to refine updating, indexing and searching.

Reset Your WordPress Password

September 9th, 2008 No comments

If for some reason you can’t remember your WordPress password and you can’t use the "lost your password" function that comes with WordPress, due to problems with email, then you can use the following SQL command to reset your password.

UPDATE wp_users SET user_pass = md5('newpassword') WHERE user_login = 'admin';

This can be useful if you have a local web server that you are trying things out on before they go live on the Internet. These servers often don’t have access to email as they are just testing platforms and will therefore fail if you try to use the "lost your password" function.

This command has been tested on WordPress version 2.6.2.

Categories: Wordpress Tags: , , , , ,

Update Or Insert A Row With MySQL And PHP

April 6th, 2008 No comments

Many situations arise where you need to either insert or update some data in a table but which you will not be certain as to which function to perform. A common solution is to do a query on the table first to see if the data exists and then insert if it doesn’t and update if it does. However, this creates an unnecessary overhead in that every time the code is run at least 2 queries are run.

A better way is to try to update the table and then use the mysql_info() function to detect how many rows where updated in the query and how many rows matched the parameters in the update query.

Take the following query.

UPDATE table SET value = "value" WHERE valueId = 2';

When run on a table the mysql_info() function returns the following result.

Rows matched: 0 Changed: 0 Warnings: 0

This shows us that the valueID of 2 doesn’t exist in the table. We would therefore need to run an insert query.

The following bit of code will allow you to run an update query on a table, detect if the value exists and then insert data instead.

$sql = 'UPDATE table SET value = "value" WHERE valueId = 2';
mysql_query($sql);
// has the update worked?
preg_match('/Rows matched: ([0-9])/',mysql_info(),$match);
 if($match[1]==0){
  // insert into database
  $sql = 'INSERT INTO table(valueID,value) VALUE(2,"value")';
  mysql_query($sql);
 }
}

Finally, you could argue that the use of the mysql_affected_rows() function might be a better solution. However, this only tells you the number of rows affected, so if an update query find the correct row but doesn’t change the information then it will show a value of 0.

Categories: PHP Tags: , , , ,

Toggle a TINYINT Field in MySQL

December 31st, 2007 No comments

MySQL uses the datatype TINYINT to store boolean values. MySQL stores the value as TINYINT(1) which is the same as a bit so the value is either 0 (false) or 1 (true). Using boolean fields can be very useful, but it can be costly in processing as to change the value you have to query the database, find out the value of the field and then act accordingly.

Here is a simple MySQL query that can be used to toggle the value already present in the TINYINT field without having to do any pre-querying.

UPDATE table SET field = 1 - field

Categories: MySQL Tags: , , , , ,