Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The Frosted MySQL Library is an extension to the default mysql and mysqli functions of php. With it you can easily create complex queries and get big advantages while development. Beginners or infrequent developers will get an easy entry to mysql and pro's can do it's work much faster and with more possibilities.

License

NotificationsYou must be signed in to change notification settings

dkern/frosted-mysql-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TheFrosted MySQL Library is an extension to the defaultmysql andmysqli functions of php.With it you can easily create complex queries and get big advantages while development.Beginners or infrequent developers will get an easy entry to mysql and pro's can do it's work much faster and with more possibilities.Just take a look to the examples or read the little documentation below.

As of the official mysql documentation, theFrosted MySQL Library can create100% of allINSERT,UPDATE,DELETE,REPLACE,TRUNCATE queries and nearly everySELECT query you want(the only exception are the sql parameters and the file output in theSELECT query).

All classes in the library has fully support for code-completion in IDEs likePHPStorm orAptana and are documented withPHPDoc.

##Features##

  • supportmysql andmysqli
  • optional persistent connections
  • read-only andwrite permissions
  • in-query replacements
  • automatically escaping all values
  • create queries inoop way
  • detailed error messages
  • receive results in many formats
  • dynamically createcollections of results
  • formatted query output if wished
  • dynamic parameters to create queries in many ways
  • many alias methods to write the code as you think
  • fully documented withPHPDoc
  • support for auto complete in IDEs likePHPStorm orAptana
  • easy to configure, easy to use
  • and much more ...

###Installation###To use theFrosted MySQL Library just include thepacked file, or every single file in theclasses/ folder (themysql.config.php is optional) in your scripts.With thepacked version you receive the full library within one file and is the best solution for the most.If you doesn't use all features you can include all needed files by your own.

require_once("packed/mysql.class.packed.php");include("packed/mysql.config.php")

###Config & Examples###After including theFrosted MySQL Library you just have to configure the mysql server settings and start using it.Take a look to theexamples/02_config.php, to see which settings are available and how to do this in different ways.A manual way to configure the library is seen below.In theexamples/ folder you will find more different examples to get you into the code.

$sql =newmysqlClass();$sql->setHostname("localhost");$sql->setUsername("root");$sql->setPassword("Pass1234");$sql->setDatabase("frosted");$sql->connect();// start using Frosted MySQL Library ...

###Create Queries###As the whole library, creating a query is very simple and straight-forward.Just write the code as you would, when writing an mysql query.If you use an IDE like PHPStorm or Aptana you will take advantage of the automatically code-completion which can show you all possible options.

$result =$sql->select("id")              ->from("users")              ->where("login = ?","frosted@eisbehr.de")              ->limit(1);

This example above is equal to the mysql query:

SELECT idFROM usersWHERE login='frosted@eisbehr.de'LIMIT1

The order of query functions is not relevant, you can mix the order as you want or specify some options later.

$query =$sql->select("id")             ->limit(1)             ->from("users");$result =$query->where("login = ?","frosted@eisbehr.de");

###Complex & Sub-Queries###Like possible in usual mysql queries, you can use sub-queries even withFrosted MySQL Library.Functions likecolumns (select),where,orWhere,having,orHaving,select (insert) orunion can receive mysqlClass instances as well.You can split your queries in many different ones, and use them separately, or write them in-code.Following an example of an bit complexer query with sub queries in two ways.

// create query with in-code sub-queries$result =$sql->select(array("p.entity_id","p.sku" =>"sku"))              ->columns(array("n.value" =>"name","t.value" =>"image","s.attribute_set_name"))  ->all()  ->highPriority()  ->straight()  ->from(array("{DB}.catalog_product_entity" =>"p"))  ->from(array("{DB}.eav_attribute" =>"a"))  ->from(array("{DB}.eav_attribute_set" =>"s"))  ->from(array("{DB}.catalog_product_entity_varchar" =>"n"))  ->from(array("{DB}.catalog_product_entity_varchar" =>"t"))  ->from(array("{DB}.catalog_product_entity_varchar" =>"v"))  ->where("n.entity_id = p.entity_id")  ->where("t.entity_id = p.entity_id")  ->where("v.entity_id = p.entity_id")  ->where("n.attribute_id = ?",$sql->select("s.attribute_id",true)    ->from(array("eav_attribute" =>"s","eav_entity_type" =>"e"))    ->where("s.attribute_code = 'name'")    ->where("s.entity_type_id = e.entity_type_id")    ->where("e.entity_type_code = 'catalog_product'"))  ->where("t.attribute_id = ?",$sql->select("s.attribute_id",true)    ->from(array("eav_attribute" =>"s","eav_entity_type" =>"e"))    ->where("s.attribute_code = ?","image_transparent")    ->where("s.entity_type_id = e.entity_type_id")    ->where("e.entity_type_code = 'catalog_product'"))  ->where("v.attribute_id = a.attribute_id")  ->where("p.attribute_set_id = s.attribute_set_id")  ->where("s.attribute_set_name NOT IN(?)",array("123456","654321"))  ->where("a.attribute_code = ?","product_info")  ->where("t.value != ?","no_selection")  ->where("v.value = ?","new")  ->order("rand()")  ->limit(10)  ->lockInShareMode()  ->union($sql->select(true)  ->from("{DB}.product_buffer_table")  ->where("product_info = ?","new"))  ->run()  ->getCollection();

You can even split all sub-queries to own instances an use them separately. Just as you like ...

// create and use sub-query$nameAttribute =$sql->select("s.attribute_id",true)  ->from(array("eav_attribute" =>"s","eav_entity_type" =>"e"))  ->where("s.attribute_code = 'name'")  ->where("s.entity_type_id = e.entity_type_id")  ->where("e.entity_type_code = 'catalog_product'");$nameId =$nameAttribute->showQuery()->run();// create and use sub-query$imageAttribute =$sql->select("s.attribute_id",true)  ->from(array("eav_attribute" =>"s","eav_entity_type" =>"e"))  ->where("s.attribute_code = ?","image_transparent")  ->where("s.entity_type_id = e.entity_type_id")  ->where("e.entity_type_code = 'catalog_product'");$imageId =$imageAttribute->showQuery()->run();// create and use sub-query$productBuffer =$sql->select(true)  ->from("{DB}.product_buffer_table")  ->where("product_info = ?","new");$buffered =$productBuffer->showQuery()->run();// create and use main query with all sub-queries as variable$products =$sql->select(array("p.entity_id","p.sku" =>"sku"))  ->columns(array("n.value" =>"name","t.value" =>"image","s.attribute_set_name"))  ->all()    ->highPriority()  ->straight()  ->from(array("{DB}.catalog_product_entity" =>"p"))    ->from(array("{DB}.eav_attribute" =>"a"))    ->from(array("{DB}.eav_attribute_set" =>"s"))    ->from(array("{DB}.catalog_product_entity_varchar" =>"n"))  ->from(array("{DB}.catalog_product_entity_varchar" =>"t"))  ->from(array("{DB}.catalog_product_entity_varchar" =>"v"))  ->where("n.entity_id = p.entity_id")  ->where("t.entity_id = p.entity_id")  ->where("v.entity_id = p.entity_id")  ->where("n.attribute_id = ?",$nameAttribute)  ->where("t.attribute_id = ?",$imageAttribute)  ->where("v.attribute_id = a.attribute_id")  ->where("p.attribute_set_id = s.attribute_set_id")  ->where("s.attribute_set_name NOT IN(?)",array("123456","654321"))  ->where("a.attribute_code = ?","product_info")  ->where("t.value != ?","no_selection")  ->where("v.value = ?","new")  ->order("rand()")  ->limit(10)  ->lockInShareMode()  ->union($productBuffer)  ->run()  ->getCollection();

###Run Query###You can create or change a query as long as you want.The instance will be available all the time.When you finished creating your query, it's time to send them to the mysql server and run them.For doing this, just callrun() on the query instance, orrun(true) to receive the rawmysql resource or themysqli_result, according to the functions you use.

// create query$query =$sql->select()->from("users");// add option$query->limit(1000);// run query$result =$query->run();

###Results###When using theFrosted MySQL Library you don't have to fetch the mysql results by your own anymore, likewhile( $row = mysql_fetch_assoc($result) ) { ... }.It's easy to receive the results in all ways you want and need. Thefetch() function is all you need from now on.

// create and run query$result =$sql->select()->from("users")->run();// mysql_fetch_assoc()$assoc =$result->fetch();$assoc =$result->fetch(mysqlClass::FETCH_ASSOC);// mysql_fetch_array()$array =$result->fetch(mysqlClass::FETCH_ARRAY);// mysql_fetch_row()$row =$result->fetch(mysqlClass::FETCH_ROW);// loop trough the resultsforeach($assocas$rowNumber =>$row )// do your work

But if you sill want to use the default php mysql fetch functionality you can receive the rawmysql resource or themysqli_result, according to the functions you use, by userun(true).

// create and run query$rawResult =$sql->select()->from("users")->run(true);// loop through the resultswhile($row =mysql_fetch_assoc($rawResult) )// do your work

###Collections###Acollection is another result type you can receive for a result.It's a powerful tool to handle mysql results.You can receive specific data from collections, run further filters, replace data, output all rows in different formats and much more.

// create and run query$result =$sql->select()->from("users")->run();// get loaded collection$collection =$result->getCollection();// alternative ways$collection =$result->fetch(mysqlClass::FETCH_OBJ);$collection =$result->fetch(mysqlClass::FETCH_OBJECT);$collection =$result->fetch(mysqlClass::FETCH_COLLECTION);// loop through collectionforeach($collectionas$row )// do your work// get a row by id$row =$collection->getItemById(1);// get a row by position$row =$collection->getItem(10);// further filter on collection// receive only rows where id is greater than 10$collection->addColumnToFilter("id",10, mysqlClass_Collection::LOGIC_GT);// output all as xmlecho$collection->toXml();

###Alternative Calls###To support your coding style and to use the classes in many, many different ways, the most query functions have dynamical parameters.The default parameters are more an hint as an strict requirement. An example with select columns:

// this$sql->select("id","name","email");// is the same as$sql->select(array("id","name","email"));// is the same as$sql->select("id",array("name","email"));// is the same as$sql->select()->columns("id","name","email");// is the same as$sql->select()->columns(array("id","name","email"));// is the same assql->select()->columns(array("id"),"name","email");// is the same as$sql->select("id")->column("name")->column("email");// is the same as$sql->select()->column("id")->column("name")->column("email");// is the same as$sql->select(array("id" =>"id","name" =>"name","email" =>"email");// and so on ...

###Multiple Query Instances###By default theFrosted MySQL Library will create only one instance per query at a time to get the best possible speed.This means that you can only build one query type at once (oneSELECT, oneINSERT, oneUPDATE, ...).

If you need more instances of a query, like for sub-queries, add an booleantrue as last parameter of the query construct.

// create a select instance$select_1 =$sql->select("id");// this will override the first query instance// $select_1 will now be the same as $select_2$select_2 =$sql->select("id","name");// create a new instance// $select_3 is a whole new query and $select_2 is still available$select_3 =$sql->select("id",true);

###Debug & Printing###For easy debugging or if you want to useexception instead or errors, set theverbose option to themysqlClass.Another help to debug queries is to print them directly withshowQuery() or receive them as string to use it somewhere else withgetQuery().If you want to print the query or maybe show them somewhere enablesetFormat to themysqlClass, to receive all query formatted for better readability.

// set options$sql->setVerbose(true);$sql->setFormat(true);// create query$queryString =$sql->update("table_name")   ->set("column_1 = ?","colOne")   ->set("column_2 = ?","colTwo")   ->set("column_3 = ?","colThree")   ->set("column_4 = ?","colFour")   ->where("id = ?",1337)   ->limit(1)   ->showQuery()   ->getQuery();

This will pass the query string to the$queryString variable and print the query directly to the browser.The output will look like this:

UPDATE     table_nameSET    column_1='colOne',     column_2='colTwo',     column_3='colThree',     column_4='colFour'WHERE     id=1337LIMIT1

About

The Frosted MySQL Library is an extension to the default mysql and mysqli functions of php. With it you can easily create complex queries and get big advantages while development. Beginners or infrequent developers will get an easy entry to mysql and pro's can do it's work much faster and with more possibilities.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp