medoo 使用分页
In this article I’m going to walk you through Medoo, a lightweight database abstraction library for PHP. Its main features include:
在本文中,我将带您了解Medoo,它是PHP的轻量级数据库抽象库。 其主要功能包括:
Support for multiple databases – it supports MySQL, MariaDB, Sybase, MS SQL, PostgreSQL, and Oracle.
支持多个数据库 –它支持MySQL,MariaDB,Sybase,MS SQL,PostgreSQL和Oracle。
Secure – prevents SQL injection, it uses PDO.
安全 –防止SQL注入,它使用PDO。
Easy to use – its API is very intuitive.
易于使用 -其API非常直观。
While Medoo is nothing revolutionary, and the fact that it sports a very small filesize matters little to few, it’s still an interesting project that went from being outright dismissed to vaguely accepted, as evident in these threads. It’s on its way up, and that’s our reason for taking a look at it.
尽管Medoo并非革命性的东西,而且它具有很小的文件大小也几乎无关紧要,但它仍然是一个有趣的项目,从被彻底删除到模糊接受, 从这些主题中可以明显看出 。 它正在上升,这就是我们研究它的原因。

安装 (Installation)
Even though the website recommends installing it by downloading a file and including it in your project, you should use Composer to do so.
即使网站建议通过下载文件并将其包含在项目中来进行安装,也应使用Composer进行安装 。
连接到数据库 (Connecting to the Database)
Creating a new instance of Medoo requires an array to be passed in as an argument. The array should contain the following items:
创建Medoo的新实例需要将数组作为参数传递。 该数组应包含以下各项:
database_type – the type of database you want to connect to. This can either be one of the following: mysql, mariadb, pgsql, sybase, oracle, mssql, sqlite.
database_type –您要连接的数据库的类型。 可以是以下之一:mysql,mariadb,pgsql,sybase,oracle,mssql,sqlite。
database_name – the name of the database.
database_name – 数据库的名称。
server – the name of the server or IP address.
服务器 – 服务器名称或IP地址。
username – the username of the user used for logging in to the database.
用户名 –用于登录数据库的用户的用户名。
password – the password of the user.
密码 –用户的密码。
$db = new medoo(array(
'database_type' => 'mysql',
'database_name' => 'pokemon',
'server' => 'localhost',
'username' => 'ash_ketchum',
'password' => 'pikachu'
));
The default port used by medoo is port 3306. If your database server uses something different, you can also pass in the port
and assign the correct port as the value.
medoo使用的默认端口是端口3306。如果您的数据库服务器使用其他port
,则也可以传入该port
并为该值分配正确的端口。
选择数据 (Selecting Data)
The first thing we’re going to do is select data from the database. You can download the example database here if you want to follow along.
我们要做的第一件事是从数据库中选择数据。 如果要继续,可以在此处下载示例数据库。
You can select data from a specific table using the select
method. This takes up the following arguments:
您可以使用select
方法从特定表中选择数据。 这需要以下参数:
- name of table 表名
- join condition (optional) 连接条件(可选)
- fields to select 要选择的字段
- where condition (optional) 条件(可选)
As a primer, let’s select some trainers from the database:
首先,让我们从数据库中选择一些培训师:
$trainers = $db->select(
'trainers',
array('id', 'name', 'pokemon_count', 'region')
);
Medoo returns the results as an array:
Medoo将结果作为数组返回:
Array
(
[0] => Array
(
[id] => 1
[name] => Brock
[pokemon_count] => 7
[region] => Kanto
)
[1] => Array
(
[id] => 2
[name] => Marshal
[pokemon_count] => 8
[region] => Unova
)
...
The query above selects all the trainers. What if we only want to select trainers from a specific region? We can do that by specifying the where condition:
上面的查询选择了所有培训师。 如果我们只想从特定区域选择培训师怎么办? 我们可以通过指定where条件来做到这一点:
$trainers = $db->select(
'trainers',
array('id', 'name', 'pokemon_count', 'region'),
array('region' => 'Kanto')
);
The query above only returns the trainers from the Kanto region.
上面的查询仅返回关东地区的培训师。
What if we only want to select the trainers who have defeated a specific number of trainers from a specific region? Here’s how we do that:
如果我们只想选择在特定地区击败了一定数量的教练员的教练怎么办? 我们的操作方法如下:
$trainers = $db->select('trainers',
array('id', 'name', 'trainers_defeated', 'region'),
array('AND' => array('trainers_defeated[>]' => 150, 'region' => 'Johto'))
);
All you have to remember when using the where condition is to specify an array of fields with their corresponding condition as a value for the keyword that you want to use. In this case we want the condition for both to return true so we’re using the AND
keyword.
使用where条件时,您所要记住的就是指定具有相应条件的字段数组作为您要使用的关键字的值。 在这种情况下,我们希望两个条件都返回true,因此我们使用AND
关键字。
If you need to use a relational operator other than =
, you need to specify it after the name of the field and wrap it in brackets []
. Here are some examples:
如果需要使用=
以外的关系运算符,则需要在字段名称之后指定它,并将其包装在方括号[]
。 这里有些例子:
- Select trainers who defeated more than 150 opponents: 选择击败150多个对手的教练员:
'trainers_defeated[>]' => 150
Select trainers whose
badges_count
is not equal to zero:选择
badges_count
不等于零的培训师:
'badges_count[!]' => 0
- Select trainers who defeated trainers within the range of 100 to 300: 选择在100到300之间击败训练员的训练员:
'trainers_defeated[<>]' => array(100, 300)
Select trainers whose
badges_count
is less than 8:选择
badges_count
小于8的培训师:
'badges_count[<]' => 8
Going back, what if we want to select the trainers who are from either ‘Kanto’ or ‘Unova’ and have a badges_count
of 5 and above or trainers_defeated
of 20 and above? Here’s how we do it:
回想一下,如果我们想从“关东”或“ badges_count
”中选择具有badges_count
为5及以上或trainers_defeated
为20及以上的trainers_defeated
呢? 这是我们的方法:
$trainers = $db->select(
'trainers',
array('id', 'name', 'trainers_defeated', 'badges_count', 'region'),
array('AND' => array(
'region' => array('Kanto', 'Unova'),
'OR' => array('badges_count[>=]' => 5, 'trainers_defeated[>=]' => 20)
))
);
All we had to do was to wrap our conditions inside the AND
keyword. For the first condition we passed in an array. In this case, it’ s the name of regions that we want to select. In the second condition, we had to wrap it in OR
, this means that the conditions inside it will return results for either of them.
我们要做的就是将条件包装在AND
关键字中。 对于第一个条件,我们传入一个数组。 在这种情况下,这就是我们要选择的区域的名称。 在第二个条件中,我们必须将其包装在OR
,这意味着其中的条件将返回其中任何一个的结果。
Now we want to know which trainer has the highest number of Pokemon caught. We can do that by using the get
method. Unlike the select
method, this will only return a single row from the table that we are fetching from.
现在,我们想知道哪个培训师捕获的口袋妖怪数量最多。 我们可以使用get
方法来做到这一点。 与select
方法不同,这只会从我们要从中获取的表中返回一行。
$most_caught = $db->get(
'trainers',
array('name', 'pokemon_count'),
array('ORDER' => 'pokemon_count DESC')
);
You can also use ORDER
with the select
method. For example, we want to select all the trainers and order them by the number of Pokemon they caught.
您也可以将ORDER
与select
方法一起使用。 例如,我们要选择所有培训师,并按他们捕获的口袋妖怪的数量对其进行排序。
$trainers_most_caught = $db->select(
'trainers',
array('name', 'pokemon_count', 'trainers_defeated'),
array('ORDER' => 'pokemon_count DESC')
);
We can also order by multiple fields. Instead of passing in a string for the value of the ORDER
, we pass in an array:
我们还可以按多个字段排序。 而不是传递一个字符串作为ORDER
的值,我们传递一个数组:
$trainers_most_caught = $db->select(
'trainers',
array('name', 'pokemon_count', 'trainers_defeated'),
array('ORDER' => array('pokemon_count DESC', 'trainers_defeated DESC'))
);
What if we want to know how many trainers are in each region? Medoo doesn’t have that kind of functionality built into it yet, so we’ll have to use a raw query:
如果我们想知道每个地区有多少教练员怎么办? Medoo还没有内置这种功能,因此我们必须使用原始查询:
$trainer_count_per_region = $db->query(
'SELECT COUNT(name) as trainers, region FROM trainers GROUP BY region'
)->fetchAll(PDO::FETCH_ASSOC);
Behind the scenes, Medoo uses PDO’s fetch method. This means it also uses the default fetch style used by PDO’s fetch method. So we have to pass in the fetch style as an argument to the fetchAll
method. In this case its PDO::FETCH_ASSOC
, this means that it will only return the field names and their value. If we leave it blank, it will return both the associative and indexed columns. Here’s the result that we get when we use PDO::FETCH_ASSOC
as the fetch style:
在后台,Medoo使用PDO的访存方法 。 这意味着它还使用PDO的获取方法所使用的默认获取样式。 因此,我们必须将fetch样式作为fetchAll
方法的参数传递。 在这种情况下,其PDO::FETCH_ASSOC
意味着这将仅返回字段名称及其值。 如果我们将其保留为空白,它将同时返回关联列和索引列。 这是使用PDO::FETCH_ASSOC
作为获取样式时得到的结果:
Array
(
[0] => Array
(
[trainers] => 2
[region] => Hoenn
)
[1] => Array
(
[trainers] => 4
[region] => Johto
)
[2] => Array
(
[trainers] => 2
[region] => Kalos
)
[3] => Array
(
[trainers] => 1
[region] => Kanto
)
[4] => Array
(
[trainers] => 3
[region] => Unova
)
)
If we leave it blank, the results will look like this:
如果我们将其保留为空白,结果将如下所示:
Array
(
[0] => Array
(
[trainers] => 2
[0] => 2
[region] => Hoenn
[1] => Hoenn
)
[1] => Array
(
[trainers] => 4
[0] => 4
[region] => Johto
[1] => Johto
)
[2] => Array
(
[trainers] => 2
[0] => 2
[region] => Kalos
[1] => Kalos
)
[3] => Array
(
[trainers] => 1
[0] => 1
[region] => Kanto
[1] => Kanto
)
[4] => Array
(
[trainers] => 3
[0] => 3
[region] => Unova
[1] => Unova
)
)
Finally, let’s touch on joins. We can use the following syntax for joins:
最后,让我们谈谈联接。 我们可以对联接使用以下语法:
>
– left join>
–左加入<
– right join<
–正确加入><
– inner join><
–内部联接<>
– full join<>
–完全加入
Here’s an example of using an inner join to select all pokemon and their corresponding type names:
这是使用内部联接选择所有宠物小精灵及其对应类型名称的示例:
$db->select(
'pokemon',
array('[><]types' => array('type_id' => 'id')),
array('pokemon.name', 'types.name(type)')
);
Just like with the regular select
method, the first argument is the name of the table. In this case its the primary table we want to select data from. The second argument is an array containing the join condition. We supply the join type and the name of table to join as the key. And the value is an array with the field on the primary table as the key and the field on the secondary table as the value. The third argument is the array of fields that you want to return. Its good practice to always specify the name of the table before the field name. Currently medoo doesn’t support table aliases so we’ll have to use the full name of the table for now. If the name of the fields that you want to select are the same, you need to use a column alias. If you don’t, only the last field with the same name that you specified will get selected. In our example above we specified an alias by using this syntax:
就像常规的select
方法一样,第一个参数是表的名称。 在这种情况下,我们要从中选择数据的主表。 第二个参数是包含连接条件的数组。 我们提供联接类型和要联接的表的名称作为键。 该值是一个数组,其中主表上的字段作为键,而辅助表上的字段作为值。 第三个参数是要返回的字段数组。 最好始终在字段名称之前指定表名称。 当前medoo 不支持表别名,因此我们现在必须使用表的全名。 如果要选择的字段名称相同,则需要使用列别名。 如果不这样做,则只会选择与您指定名称相同的最后一个字段。 在上面的示例中,我们使用以下语法指定了别名:
table_name.field_name(alias)
插入资料 (Inserting Data)
Next, we add some data into the pokemon
table. For that, were going to use the Pokemon API as our data source.
接下来,我们将一些数据添加到pokemon
表中。 为此,将使用Pokemon API作为我们的数据源。
First install guzzle with composer require guzzlehttp/guzzle:~5.0
首先用composer require guzzlehttp/guzzle:~5.0
Create a new instance of the guzzle client:
创建一个新的食人魔客户端实例:
use GuzzleHttp\Client;
$client = new Client();
Next, we select all the trainers and the Pokemon types from the database. We will be using these later on when we insert the data into the pokemon
table.
接下来,我们从数据库中选择所有教练和口袋妖怪类型。 稍后将数据插入到pokemon
表中时,我们将使用它们。
$trainers = $db->select('trainers', array('id', 'pokemon_count'));
$types = $db->select('types', array('id', 'name'));
Next, we make a request to the pokedex resource in the Pokemon API. This returns a JSON string, so we use guzzle’s json
method to convert it to an array. The pokedex resource returns an array of all pokemon.
接下来,我们向Pokemon API中的pokedex资源发出请求。 这将返回JSON字符串,因此我们使用guzzle的json
方法将其转换为数组。 pokedex资源返回所有pokemon的数组。
$pokedex_response = $client->get('http://pokeapi.co/api/v1/pokedex/1');
$pokedex_data = $pokedex_response->json();
$pokemon = $pokedex_data['pokemon'];
$total_pokemon = count($pokemon) - 1; //were zero-indexed
Next, we loop through all the trainers, get the number of Pokemon they caught and then create a for loop based on that number. The pokedex resource returns the name of the pokemon and a resource URI in which we can get further information about it. We also need to get the primary type of the pokemon and a list of moves that it can do so we also make a request to the pokemon resource. Once we get the data back, we then need to get the id of the pokemon type. We already have a table that has a list of all the pokemon types so we just determine the id by looping through it, and if the type name returned from the API is the same as one of the type names from the database, we just get its id. After that, we give the pokemon a random level by using mt_rand
. Next, we call Medoo’s insert
method to insert the Pokemon data in the database. This takes the name of the table as the first argument, then the array of data as the second argument. Finally, we assign some moves to each of the Pokemon. When you call the insert
method, it returns the last insert id. We use this last insert id to determine the id of the pokemon that was assigned by the database, and then insert it to the pokemon_moves
table along with the name of the move.
接下来,我们遍历所有培训师,获取他们捕获的Pokemon的数量,然后根据该数量创建一个for循环。 pokedex资源返回宠物小精灵的名称和资源URI,我们可以在其中获取有关它的更多信息。 我们还需要获取神奇宝贝的主要类型以及它可以执行的动作的列表,以便我们也可以向神奇宝贝资源发出请求。 一旦取回数据,我们就需要获取口袋妖怪类型的ID。 我们已经有一个表,其中列出了所有神奇宝贝类型,因此我们只需通过遍历它就可以确定ID,并且如果API返回的类型名称与数据库中的一种类型名称相同,则只需获取它的编号。 在那之后,我们使用mt_rand
给神奇宝贝一个随机级别。 接下来,我们调用Medoo的insert
方法将Pokemon数据插入数据库中。 这将表名作为第一个参数,然后将数据数组作为第二个参数。 最后,我们为每个口袋妖怪分配一些动作。 调用insert
方法时,它将返回最后一个插入ID。 我们使用最后一个插入ID来确定数据库分配的神奇宝贝的ID,然后将其与pokemon_moves
名称一起插入到pokemon_moves
表中。
foreach($trainers as $trainer){
$trainer_id = $trainer['id'];
$pokemon_count = $trainer['pokemon_count'];
for($x = 0; $x < $pokemon_count; $x++){
$pokemon_id = mt_rand(0, $total_pokemon);
$pokemon_name = $pokemon[$pokemon_id]['name'];
$pokemon_resource = $pokemon[$pokemon_id]['resource_uri'];
$pokemon_response = $client->get('http://pokeapi.co/' . $pokemon_resource);
$pokemon_data = $pokemon_response->json();
$pokemon_types = $pokemon_data['types'];
//pokemon types in the database starts with a capital letter
$type_name = ucfirst($pokemon_types[0]['name']);
$type_id = null;
foreach($types as $t){ //determine type id
if($t['name'] == $type_name){
$type_id = $t['id'];
}
}
$level = mt_rand(1, 100); //give a random level between 1 and 100
$pokemon_db_id = $db->insert(
'pokemon',
array(
'name' => $pokemon_name,
'trainer_id' => $trainer_id,
'type_id' => $type_id,
'level' => $level
)
);
//assign some moves
$pokemon_moves = $pokemon_data['moves'];
if(!empty($pokemon_moves)){
$move_count = count($pokemon_moves) - 1;
$move_limit = 4; //each pokemon can only have 4 moves
for($z = 0; $z < $move_limit; $z++){
$move_id = mt_rand(0, $move_count);
$move_name = $pokemon_moves[$move_id]['name'];
$db->insert(
'pokemon_moves',
array(
'pokemon_id' => $pokemon_db_id,
'move_name' => $move_name
)
);
}
}
}
}
After running the script above, we should now have some Pokemon along with their moves in our database.
在运行完上面的脚本之后,我们现在应该在数据库中有一些神奇宝贝以及它们的移动。
Let’s say trainer Roxie caught 3 new Pokemon: Drapion, Toxicroak and Crobat. How can we insert them all at once into our pokemon
table? The insert
method also supports multi-inserts, so we can just supply an array containing all of the rows of data that we want to insert.
假设培训师Roxie抓了3个新的神奇宝贝:Drapion,Toxicroak和Crobat。 我们如何将它们全部一次插入pokemon
表中? insert
方法还支持多插入,因此我们只需要提供一个包含要插入的所有数据行的数组即可。
First, we get the trainer id from the trainers
table:
首先,我们从trainers
表中获取Trainer ID:
$trainer_id = $db->get('trainers', 'id', array('name' => 'Roxie'));
Let’s assume that those 3 Pokemon have the same type so we go ahead and get the type id from the database:
假设这3个Pokemon具有相同的类型,那么我们继续从数据库中获取类型ID:
$type_id = $db->get('types', 'id', array('name' => 'Poison'));
The pokemon API doesn’t really have a search method, so all we can do is access the pokedex
resource directly from the browser:
pokemon API确实没有搜索方法,因此我们所能做的就是直接从浏览器访问pokedex
资源:
http://pokeapi.co/api/v1/pokedex/1/
And then look for the resource_uri
that we want to get. I already did it so you don’t have to. The ids that we need are: 452, 454 and 169. We then loop through those and get all the necessary data. This time, instead of doing an insert
call on every iteration of the loop, we store it in an array. We then call the insert
method once and supply the array in which we stored the pokemon data.
然后寻找我们想要获得的resource_uri
。 我已经做到了,所以您不必这样做。 我们需要的id是:452、454和169。然后我们遍历这些ID并获取所有必要的数据。 这次,我们没有在循环的每次迭代中进行insert
调用,而是将其存储在数组中。 然后,我们调用一次insert
方法,并提供存储神奇宝贝数据的数组。
$ids = array(452, 454, 169);
$pokemon_caught = array();
foreach($ids as $id){
$response = $client->get('http://pokeapi.co/api/v1/pokemon/' . $id);
$data = $response->json();
$name = $data['name'];
$pokemon_caught[] = array(
'name' => $name,
'trainer_id' => $trainer_id,
'type_id' => $type_id,
'level' => mt_rand(1, 100)
);
}
$db->insert('pokemon', $pokemon_caught);
更新数据 (Updating Data)
Now Roxie has 3 more Pokemon, but her trainer data hasn’t been updated yet. We need to add 3 to her current pokemon_count
. To do that we call the update
method. This takes up the following arguments:
现在Roxie还有3个Pokemon,但她的教练数据尚未更新。 我们需要在她当前的pokemon_count
添加3。 为此,我们调用update
方法。 这需要以下参数:
- name of table 表名
- data 数据
- where condition 条件在哪里
Here’s how we do an update:
这是我们进行更新的方法:
$db->update(
'trainers',
array('pokemon_count[+]' => 3),
array('id' => $trainer_id)
);
See what we did there? Medoo comes with a nice utility wherein you can do the following mathematical operations on a specific field that you’re updating:
看看我们在那里做了什么? Medoo附带了一个不错的实用程序,您可以在其中对要更新的特定字段执行以下数学运算:
+
– add a specific value to the current value.+
–在当前值上添加一个特定值。-
– subtract a specific value to the current value.-
–将特定值减去当前值。*
– multiply a specific value to the current value.*
–将特定值乘以当前值。/
– divide a specific value to the current value./
–将特定值除以当前值。
删除资料 (Deleting Data)
Now we want to release Drapion because it sucks. We can delete him from the pokemon
table using the delete
method. This takes up the name of the table as its first argument and an array of conditions as the second argument.
现在我们要释放Drapion,因为它很烂。 我们可以使用delete
方法从pokemon
表中删除他。 这将表名作为其第一个参数,并将条件数组作为第二个参数。
$db->delete('pokemon', array('name' => 'Drapion'));
We can also delete based on 2 conditions. Here we want to delete all Pokemon which are of ‘Normal’ type and are below level 60:
我们还可以根据2个条件删除。 在这里,我们要删除所有“普通”类型且低于60级的神奇宝贝:
$type_id = $db->get('types', 'id', array('name' => 'Normal'));
$db->delete(
'pokemon',
array('AND' => array('level[<]' => 60, 'type_id' => $type_id))
);
Note that calling delete doesn’t return the id of the deleted row. This means you’ll have to find a way to get it on your own if you want to use that id for something.
请注意,调用delete不会返回已删除行的ID。 这意味着,如果您想使用该ID,则必须自己找到一种方法。
汇总功能 (Aggregate Functions)
Medoo also comes with some aggregate functions.
Medoo还带有一些聚合功能。
For example, we want to get the total number of trainers from the trainers
table. For that, we use the count
method:
例如,我们想从trainers
中获得培训师的总数。 为此,我们使用count
方法:
$total_trainers = $db->count('trainers');
If we want to know the trainer with the most/least number of Pokemon, there’s also a function for that. For getting the most, we use the max
method. This takes up the name of the table and the field you want to use:
如果我们想知道口袋妖怪数量最多/最少的教练员,那还有一个功能。 为了获得max
,我们使用max
方法。 这将占用表名和要使用的字段:
$db->max('trainers', 'pokemon_count');
For getting the least, we use the min
method:
为了获得最少的数量,我们使用min
方法:
$db->min('trainers', 'pokemon_count');
We can also use the avg
method, if we want to know the average number of pokemon that each trainer in the trainers
table have:
我们也可以用avg
的方法,如果我们想知道口袋妖怪的平均数量,在每个培训师trainers
表有:
$db->avg('trainers', 'pokemon_count');
We can also get the total number of pokemon that all of the trainers have by using the sum
method:
我们还可以使用sum
方法获得所有训练师拥有的口袋妖怪sum
:
$db->sum('trainers', 'pokemon_count');
Note that with the count
, min
, max
, and sum
method we can also specify some additional conditions. We supply those as the last argument. For example, we only want to know the maximum number of pokemon that a trainer has in the ‘Hoenn’ region:
注意,使用count
, min
, max
和sum
方法,我们还可以指定一些附加条件。 我们将这些作为最后一个论据。 例如,我们只想知道培训师在'Hoenn'地区拥有的宠物小精灵的最大数量:
$db->max('trainers', 'pokemon_count', array('region' => 'Hoenn'));
调试 (Debugging)
Medoo also provides utilities for checking errors and debugging. Note that medoo doesn’t return any errors when you try to do something that cannot yield a successful result, that’s why sometimes you have to check for it explicitly.
Medoo还提供了用于检查错误和调试的实用程序。 请注意,当您尝试执行无法产生成功结果的操作时,medoo不会返回任何错误,这就是为什么有时您必须显式检查它的原因。
For example, we made a typo for the trainers
table. Instead of trainers
we typed in trainerw
:
例如,我们为trainers
表打了一个错字。 而不是trainers
我们输入trainerw
:
$db->update(
'trainerw',
array('pokemon_count[-]' => 1),
array('id' => 99)
);
Executing the code above won’t make medoo complain about it. It just fails silently. To check for the error we need to call the error
method right after the offending code:
执行上面的代码不会使medoo抱怨。 它只是默默地失败。 要检查错误,我们需要在有问题的代码之后立即调用error
方法:
$db->error();
This returns an array which look like the following:
这将返回一个如下所示的数组:
Array (
[0] => 42S02
[1] => 1146
[2] => Table 'pokemon.trainerw' doesn't exist
)
The first item is the error code returned by MySQL. You can see a list of error codes on this page. The second item is the SQL state. And the third item is a human readable description of the error.
第一项是MySQL返回的错误代码。 您可以在此页面上看到错误代码列表。 第二项是SQL状态。 第三项是该错误的可读描述。
If the error is an error with the arguments passed to the specific method that you’re using, medoo returns an error so you won’t really need to check for those if it happens.
如果错误是传递给您正在使用的特定方法的参数错误,则medoo返回错误,因此您真的不需要检查那些错误。
If there aren’t any errors returned when you call the error
method, then it might not be an error. Instead, it can be that the query generated by medoo doesn’t do what you want it to do. In those cases you need to use the last_query()
method. As the name suggests, calling this method will return the last query executed by medoo. Let’s take a look at some examples:
如果在调用error
方法时没有返回任何错误,则可能不是错误。 相反,可能是medoo生成的查询没有执行您想要的操作。 在这种情况下,您需要使用last_query()
方法。 顾名思义,调用此方法将返回medoo执行的最后一个查询。 让我们看一些例子:
Decrease the
pokemon_count
by 1 for trainer with the id of 1:将
pokemon_count
为1的培训师的pokemon_count
减少1:
$db->update(
'trainers',
array('pokemon_count[-]' => 1),
array('id' => 1)
);
echo $db->last_query();
/*
returns:
UPDATE "trainers" SET "pokemon_count" = "pokemon_count" - 1 WHERE "id" = 1
*/
- Select the pokemon with the name ‘virizion’: 选择名称为“ virizion”的神奇宝贝:
$db->get(
'pokemon',
array('name', 'level', 'type'), array('name' => 'virizion')
);
echo $db->last_query();
/*
returns:
SELECT "name","level","type" FROM "pokemon" WHERE "name" = 'virizion' LIMIT 1
*/
- Select all pokemon with their corresponding type: 选择所有具有相应类型的口袋妖怪:
$db->select(
'pokemon',
array('[><]types' => array('type_id' => 'id')),
array('pokemon.name', 'types.name(type)')
);
echo $db->last_query();
/*
returns:
SELECT "pokemon"."name","types"."name" AS "type" FROM "pokemon" INNER JOIN "types" ON "pokemon"."type_id" = "types"."id"
*/
结论 (Conclusion)
That’s it! In this tutorial we have learned about Medoo, a lightweight and easy to use database abstraction library for PHP. We learned how to select, update, delete, and insert data into the database. We also learned some debugging techniques that we can use if something goes wrong. If you want to learn more, check out meedo’s official documentation.
而已! 在本教程中,我们了解了Medoo,这是一个轻量级且易于使用PHP数据库抽象库。 我们学习了如何选择,更新,删除数据以及将数据插入数据库。 我们还学习了一些调试技术,可以在出现问题时使用。 如果您想了解更多信息,请查阅meedo的官方文档 。
Are you using Medoo for anything? What do you think about the project? Let us know!
您是否正在使用Medoo? 您如何看待该项目? 让我们知道!
翻译自: https://www.sitepoint.com/getting-started-medoo-examples-use/
medoo 使用分页