PHP杂记

Jun 23, 2016 pm 02:36 PM

mysql常用命令
1、mysql -uroot -padmin --登录,用户名:root,密码:admin(自己设的)
2、show databases;显示数据库

PHP程序访问数据库

PHP程序访问数据库,完全可以使用存储过程,有人认为使用存储过程便于维护
不过仁者见仁,智者见智,在这个问题上,偶认为使用存储过程意味着必须要DBA和开发人员更紧密配合,如果其中一方更变,则显然难以维护。
但是使用存储过程至少有两个最明显的优点:速度和效率。
使用存储过程的速度显然更快。
在效率上,如果应用一次需要做一系列SQL操作,则需要往返于PHP与ORACLE,不如把该应用直接放到数据库方以减少往返次数,增加效率。
但是在INTERNET应用上,速度是极度重要的,所以很有必要使用存储过程。
偶也是使用PHP调用存储过程不久,做了下面这个列子。

代码:--------------------------------------------------------------------------------

//建立一个TEST表
CREATE TABLE TEST (
  ID        NUMBER(16)        NOT NULL,
  NAME      VARCHAR2(30)      NOT NULL,
  PRIMARY KEY (ID)
);

//插入一条数据
INSERT INTO TEST VALUES (5, 'PHP_BOOK');

//建立一个存储过程
CREATE OR REPLACE PROCEDURE PROC_TEST (
  p_id IN OUT NUMBER,
  p_name OUT VARCHAR2
) AS
BEGIN
  SELECT NAME INTO p_name
    FROM TEST
    WHERE ID = 5;
END PROC_TEST;
/

--------------------------------------------------------------------------------

PHP代码:--------------------------------------------------------------------------------


//建立数据库连接
$user = "scott";                //数据库用户名
$password = "tiger";            //密码
$conn_str = "tnsname";          //连接串(cstr : Connection_STRing)
$remote = true                  //是否远程连接
if ($remote) {
  $conn = OCILogon($user, $password, $conn_str);
}
else {
  $conn = OCILogon($user, $password);
}

//设定绑定
$id = 5;                        //准备用以绑定的php变量 id
$name = "";                     //准备用以绑定的php变量 name

/** 调用存储过程的sql语句(sql_sp : SQL_StoreProcedure)
 *  语法:
 *      BEGIN 存储过程名([[:]参数]); END;
 *  加上冒号表示该参数是一个位置
**/
$sql_sp = "BEGIN PROC_TEST(:id, :name); END;";

//Parse
$stmt = OCIParse($conn, $sql_sp);

//执行绑定
OCIBindByName($stmt, ":id", $id, 16);           //参数说明:绑定php变量$id到位置:id,并设定绑定长度16位
OCIBindByName($stmt, ":name", $name, 30);

//Execute
OCIExecute($stmt);

//结果
echo "name is : $name
";

?>


php连接mysql数据库

对于熟悉做网站的人来说,要想网站做成动态的,肯定要有数据库的支持,利用特定的脚本连接到数据库,从数据库中提取资料、向数据库中添加资料、删除资料等。这里我通过一个实例来说明如何用php连接到数据库的。

   我准备建立一个简单的通讯录,数据库的名字叫txl,数据库只有一个表叫personal_info,表中有5个字段
pi_id     pi_name     pi_tel     pi_qq     pi_email

   首先我们要创建数据库:
   create database txl;
   然后我们建立表
   CREATE TABLE `personal_info` (
  `pi_id` bigint(20) NOT NULL auto_increment,
  `pi_name` varchar(50) NOT NULL,
  `pi_tel` varchar(15) default NULL,
  `pi_qq` varchar(15) default NULL,
  `pi_email` varchar(50) default NULL,
  PRIMARY KEY  (`pi_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
上面的sql语句很简单,通过字面都能猜出什么意思。
下面是连接到数据库并且显示表personal_info的所有字段信息:
// connsql.php
    $mysql_server_name="localhost"; //数据库服务器名称
    $mysql_username="root"; // 连接数据库用户名
    $mysql_password="root"; // 连接数据库密码
    $mysql_database="lxr";  // 数据库的名字
  
    // 连接到数据库
    $conn=mysql_connect($mysql_server_name, $mysql_username,
                        $mysql_password);
  
    // 从表中提取信息的sql语句
    $strsql="select * from personal_info";
    // 执行sql查询
    $result=mysql_db_query($mysql_database, $strsql, $conn);
    // 获取查询结果
    $row=mysql_fetch_row($result);
  
    echo '';
    echo '

';

    // 显示字段名称
    echo "\n
\n";
    for ($i=0; $i     {
      echo '
\n";
    }
    echo "
\n";
    // 定位到第一条记录
    mysql_data_seek($result, 0);
    // 循环取出记录
    while ($row=mysql_fetch_row($result))
    {
      echo "
\n";
      for ($i=0; $i       {
        echo '
';
      }
      echo "
\n";
    }
  
    echo "
'.
      mysql_field_name($result, $i);
      echo "
';
        echo "$row[$i]";
        echo '
\n";
    echo "
";
    // 释放资源
    mysql_free_result($result);
    // 关闭连接
    mysql_close(); 
?>
 
下面是运行结果:
pi_id     pi_name     pi_tel     pi_qq     pi_email
1     Zhangsan
    13911111111     642864125     zhangsan@126.com
2     Lisi
    13122222222     63958741     lisi@163.com
3     Wangwu
    13833333333     912345678     wangwu@sohu.com

所谓“万变不离其宗”,再复杂的操作也都是基于上面的来的,都少不了上面的基本的步骤,当需要时查一下相关的手册便可以解决。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles