标签 php 下的文章

一、转自:http://www.cnblogs.com/richardw/archive/2012/10/06/2713027.html
二、转自:https://sjolzy.cn/PHP-Class-ezSQL-database-operations.html(PHP数据库操作类 - ezSQL)

一、操作MySQL,使用ezSQL,简单而方便

最近使用PHP做点小东东,使用了ezSQL,真的感觉很简单很ez。

ezSQL官方下载地址:http://justinvincent.com/ezsql

使用示例:

取数值:

$var = $db->get_var("SELECT count(*) FROM users");


取对象:

$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");


取数组:

复制代码

$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user )
{
    // 使用对象语法
    echo $user->name;
    echo $user->email;
}

复制代码
可以看出,其实函数返回值为二维数组,经foreach分解后,$user为每条记录的内容,可直接用$user->字段名的方式访问。

get_results()还有另一种调用方式:

复制代码

// Extract results into the array $dogs (and evaluate if there are any results at the same time)..
if ( $dogs = $db->get_results(“SELECT breed, owner, name FROM dogs”, ARRAY_A) )
{
            // Loop through the resulting array on the index $dogs[n]
            foreach ( $dogs as $dog_detail )
            {
 
                        // Loop through the resulting array
                        foreach ( $dogs_detail as $key => $val )
                        {
                                    // Access and format data using $key and $val pairs..
                                    echo “<b>” . ucfirst($key) . “</b>: $val<br>”;
                        }
 
                        // Do a P between dogs..
                        echo “<p>”;
            }
}
else
{
            // If no users were found then if evaluates to false..
            echo “No dogs found.”;
}

复制代码

输出结果:

Output:
Breed: Boxer
Owner: Amy
Name: Tyson

Breed: Labrador
Owner: Lee
Name: Henry

Breed: Dachshund
Owner: Mary
Name: Jasmine


执行Insert操作:

$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");


调试信息

// Display last query and all associated results

$db->debug();

四种方法:

bool    $db->query(query)
var    $db->get_var(query)
mixed    $db->get_row(query)
mixed    $db->get_results(query)
ezSQL functions

$db->get_results -- get multiple row result set from the database (or previously cached results)

$db->get_row -- get one row from the database (or previously cached results)

$db->get_col -- get one column from query (or previously cached results) based on column offset

$db->get_var -- get one variable, from one row, from the database (or previously cached results)

$db->query -- send a query to the database (and if any results, cache them)

$db->debug -- print last sql query and returned results (if any)

$db->vardump -- print the contents and structure of any variable

$db->select -- select a new database to work with

$db->get_col_info -- get information about one or all columns such as column name or type

$db->hide_errors -- turn ezSQL error output to browser off

$db->show_errors -- turn ezSQL error output to browser on

$db->escape -- Format a string correctly to stop accidental mal formed queries under all PHP conditions

$db = new db -- Initiate new db object.

 

ezSQL variables

$db->num_rows – Number of rows that were returned (by the database) for the last query (if any)

$db->insert_id -- ID generated from the AUTO_INCRIMENT of the previous INSERT operation (if any)

$db->rows_affected -- Number of rows affected (in the database) by the last INSERT, UPDATE or DELETE (if any)

$db->num_queries -- Keeps track of exactly how many 'real' (not cached) queries were executed during the lifetime of the current script

$db->debug_all – If set to true (i.e. $db->debug_all = true;) Then it will print out ALL queries and ALL results of your script.

$db->cache_dir – Path to mySQL caching dir.

$db->cache_queries – Boolean flag (see mysql/disk_cache_example.php)

$db->cache_inserts – Boolean flag (see mysql/disk_cache_example.php)

$db->use_disk_cache – Boolean flag (see mysql/disk_cache_example.php)

$db->cache_timeout – Number in hours (see mysql/disk_cache_example.php)



二、PHP数据库操作类 - ezSQL

07 August 2010 10:57 Saturdayby Sjolzy
ezSQL 下载地址://sjolzy.cn/php/ezSQL/bak/ez_sql_2.05.zip

新版本是2.05添加了很多支持,包括 CodeIgniter,MSSQL, PDO等等。

查看示例:

Example 1

// Select multiple records from the database and print them out..
$users = $db->get_results("SELECT name, email FROM users");
foreach ( $users as $user ) {
            // Access data using object syntax
            echo $user->name;
            echo $user->email;
}

Example 2

// Get one row from the database and print it out..
$user = $db->get_row("SELECT name,email FROM users WHERE id = 2");
echo $user->name;
echo $user->email;

Example 3

// Get one variable from the database and print it out..
$var = $db->get_var("SELECT count(*) FROM users");
echo $var;

Example 4

// Insert into the database
$db->query("INSERT INTO users (id, name, email) VALUES (NULL,'justin','jv@foo.com')");

Example 5

// Update the database
$db->query("UPDATE users SET name = 'Justin' WHERE id = 2)");

Example 6

// Display last query and all associated results
$db->debug();

Example 7

// Display the structure and contents of any result(s) .. or any variable
$results = $db->get_results("SELECT name, email FROM users");
$db->vardump($results);

Example 8

// Get 'one column' (based on column index) and print it out..
$names = $db->get_col("SELECT name,email FROM users",0)
foreach ( $names as $name ) {
    echo $name;
}

Example 9

// Same as above ‘but quicker’
foreach ( $db->get_col("SELECT name,email FROM users",0) as $name ) {
    echo $name;
}

Example 10

// Map out the full schema of any given database and print it out..
$db->select("my_database");
foreach ( $db->get_col("SHOW TABLES",0) as $table_name ) {
    $db->debug();
    $db->get_results("DESC $table_name");
}
$db->debug();


转自永久地址:https://sjolzy.cn/PHP-Class-ezSQL-database-operations.html

--EOF--


本文永久地址:http://duuge.com/archives/ezSQL.html

1.

apt-get install apache2

2.

apt-get install php5 libapache2-mod-php5 php5-gd php5-curl php-pear

3.

apt-get install mysql-server mysql-client php5-mysql

4.

apt-get install phpmyadmin

5.

sudo ln -s /usr/share/phpmyadmin /var/www

最后打开:

[http://localhost/phpmyadmin][1] 

成功!

1、

  sudo apt-get install nginx php5-fpm

然后编辑配置文件。

  sudo gedit /etc/nginx/site-available/default

注意,如果是用gedit而不是用vi编辑,那应该编辑site-available下的default文件,如果是编辑site-enabled下的default,因为gedit保存时默认会生成一个“default~”的备份,这个备份也会被nginx当成启用的配置文件而出错无法启动。保险的做法是,编辑site-available下的文件后仍手动删除备份文件。

找到location ~ .php$的地方,5行取消注释,变成这样:

  location ~ \.php$ {  
  #   fastcgi_split_path_info ^(.+\.php)(/.+)$;  
  #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini  
  #  
  #   # With php5-cgi alone:  
  #   fastcgi_pass 127.0.0.1:9000;  
  #   # With php5-fpm:  
      fastcgi_pass unix:/var/run/php5-fpm.sock;  
      fastcgi_index index.php;  
      include fastcgi_params;  
  }  

这就成了!
启动nginx:

  sudo service nginx start

扩展:

  1. default文件中,找到
    index index.html index.htm;

这行,加入成

  index index.html index.htm index.php;

这就可以用php文件做默认主页

2.default文件中,在server{}指示符的 location / {} 指示符内,加入

  autoindex on;

当文件夹内没有index文件,就会自动索引文件。

  1. server{} 指示符的 root 行是文件根目录,自行修改就能把那个文件夹作为网站根目录

参考:http://ubuntuhandbook.org/index.php/2013/10/install-nginx-php5-mysql-lemp-ubuntu-1310/

转载请注明出处:http://blog.csdn.net/hursing

安装MySQL
要安装 MySQL,可以在终端提示符后运行下列命令:

  sudo apt-get install mysql-server mysql-client #中途会让你输入一次root用户密码
  sudo apt-get install php5-mysql  #安装php5-mysql 是将php和mysql连接起来

一旦安装完成,MySQL 服务器应该自动启动。

  sudo start mysql #手动的话这样启动
  sudo stop mysql #手动停止

当你修改了配置文件後,你需要重启 mysqld 才能使这些修改生效。

要想检查 mysqld 进程是否已经开启,可以使用下面的命令:

  pgrep mysqld

如果进程开启,这个命令将会返回该进程的 id。

文件结构
MySQL配置文件:/etc/mysql/my.cnf ,其中指定了数据文件存放路径

  datadir         = /var/lib/mysql

如果你创建了一个名为 test 的数据库,那么这个数据库的数据会存放到 /var/lib/mysql/test 目录下。

进入MySQL

  mysql -uroot -p 

(输入mysql的root密码)

  qii@ubuntu:~$ mysql -u root -p
  Enter password: 
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 37
  Server version: 5.1.41-3ubuntu12.3 (Ubuntu)

  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  mysql> 

修改 MySQL 的管理员密码:

  sudo mysqladmin -u root password newpassword;

简单的操作

显示数据库:

mysql> show databases;
Database
information_schema
mysql

2 rows in set (0.00 sec)

phpmyadmin管理
用随便一个支持PHP的web服务器(如Apache、Nginx、Lighttpd),下载phpmyadmin,装之。

  sudo apt-get install phpmyadmin  #注意这是安装到/usr/share/phpmyadmin

在ubuntu下,运行:

  sudo apt-get install phpmyadmin

过一会后会有一些设置,如选择服务器(选apache别选lighttpd)、密码设定等等内容。安装完成后,访问http://localhost/phpmyadmin会出现404错误,这是因为没有将phpmyadmin目录映射到apache目录下面,运行下面命令即可:

nginx服务器用:

  sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/html

apache服务器用:

  sudo ln -s /usr/share/phpmyadmin /var/www

最后打开:http://localhost/phpmyadmin 成功!