Milantex项目数据库访问包装器。

1.0.5 2016-09-28 09:41 UTC

This package is not auto-updated.

Last update: 2024-09-23 15:51:13 UTC


README

Build Status codecov Code Climate Latest Stable Version Total Downloads License SensioLabsInsight

什么是Milantex-DAW?

此包提供了一个机制,可以轻松连接和使用MySQL/MariaDB数据库。以下文本提供了有关如何安装它的信息,以及如何使用它的示例。

安装

使用命令行中的composer

您可以在项目源目录中使用以下命令使用composer安装此包

composer require milantex/daw

如果需要,请确保更新您的自动加载器

composer dump-autoload -o

在composer.json中将包作为依赖项

将以下代码添加到您的composer.json中。以下是一个包含milantex/daw包的依赖项的composer.json文件示例

{
    "name": "your-name/your-project",
    "description": "Your project's description",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "Your Name",
            "email": "your@mail.tld"
        }
    ],
    "require": {
        "milantex/daw": "*"
    }
}

请确保运行composer命令以安装依赖项

composer install

在项目中使用它

要开始使用DAW,首先要求composer的自动加载脚本

require_once 'vendor/autoload.php';

之后,您可以创建一个Milantex\DAW\DataBase的实例

use Milantex\DAW\DataBase;

# Open a connection using the DAW
$daw = new DataBase('localhost', 'bayou', 'root', '');

以下是一个示例,说明如何从数据库中选择多个记录并打印它们

# Write an SQL query (no parameters)
$query1 = 'SELECT * FROM `post` WHERE `visible` = 1;';

# Execute the query and retrieve all result set rows
$visiblePosts = $daw->selectMany($query1);

# Print out the dump of the result
echo '<pre>' . print_r($visiblePosts, true) . '</pre>';

输出

<pre>
  Array
  (
      [0] => stdClass Object
          (
              [post_id] => 1
              [created_at] => 2015-02-12 20:44:04
              [user_id] => 1
              [title] => Limitless Bayou begins
              [link] => limitsless-bayou-begins
              [content] => This is a sample post in a database.
              [visible] => 1
          )
  )
</pre>

以下是一个示例,说明如何使用带有命名参数占位符的SQL查询从数据库中选择单个记录

# Write an SQL query (with one parameter for the username)
$query2 = 'SELECT * FROM `user` WHERE `username` = :username AND `active` = 1;';

# Prepare the parameter associative array
$params2 = [ ':username' => 'milantex' ];

# Execute the query and retrieve a single result set
$user = $daw->selectOne($query2, $params2);

# Print out the dump of the result
echo '<pre>' . print_r($user, true) . '</pre>';

输出

<pre>
  stdClass Object
  (
      [user_id] => 1
      [created_At] => 2015-02-12 20:41:12
      [username] => milantex
      [password] => SOME_HASH_VALUE
      [active] => 1
  )
</pre>

以下是一个示例,说明如何使用未命名的参数占位符(按顺序)从数据库中删除记录

# Write an SQL query (with an unnamed parameter placeholder)
$query3 = 'DELETE FROM `post` WHERE `post_id` = ?;';

# Prepare the ordered parameter array
$params3 = [ 130 ];

# Execute the query and retrieve a single result set
$result3 = $daw->execute($query3, $params3);

# Check the result
if (!$result3) {
	# Print out the dump of error information if the result is bad
	echo '<pre>' . print_r($daw->getLastExecutionError(), true) . '</pre>';
} else {
	# Print out how many records were affected
	$affectedRows = $daw->getLastExecutionAffectedRownCount();
	echo 'Deleted record count: ' . $affectedRows . '<br><br>';
}

输出

Deleted record count: 0

以下是一个示例,说明如何使用命名参数占位符将记录添加到表中

# Write an SQL query (with unnamed parameter placeholders)
$query4 = 'INSERT INTO `post` (`user_id`, `title`, `link`, `content`) '.
		  'VALUES (:user_id, :title, :link, :content);';

# Prepare the parameter associative array
$params4 = [
	':user_id' => 1,
	':title'   => 'A test post',
	':link'    => 'a-test-post',
	':content' => '<p>This is the content of the new test post.</p>'
];

# Execute the query and retrieve a single result set
$result4 = $daw->execute($query4, $params4);

# Check the result
if (!$result4) {
	# Print out the dump of error information if the result is bad
	echo '<pre>' . print_r($daw->getLastExecutionError(), true) . '</pre>';
} else {
	# Get the ID of the new post
	$postId = $daw->getLastInsertId();
	echo 'The ID of the new post is: ' . $postId . '<br><br>';
}

输出

The ID of the new post is: 6