几种常用PHP连接数据库的代码示例
PHP连接数据库之PHP连接MYSQL数据库代码
- ?php
- $mysql_server_name='localhost';
- //改成自己的mysql数据库服务器
- $mysql_username='root';
- //改成自己的mysql数据库用户名
- $mysql_password='12345678';
- //改成自己的mysql数据库密码
- $mysql_database='mycounter';
- //改成自己的mysql数据库名
-
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database); -
$sql='CREATE DATABASE mycounter
DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; - ';
- mysql_query($sql);
-
$sql='CREATE TABLE `counter`
(`id` INT(255) UNSIGNED NOT NULL
AUTO_INCREMENT ,`count` INT(255)
UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY
( `id` ) ) TYPE = innodb;'; - mysql_select_db($mysql_database,$conn);
- $result=mysql_query($sql);
- //echo $sql;
- mysql_close($conn);
- echo "Hello!数据库mycounter已经成功建立!";
- ?>
PHP连接数据库之PHP连接ACCESS数据库代码方法
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li> <span>$</span><span class="attribute">conn</span><span> = </span><span class="attribute-value">new</span><span> com("ADODB.Connection"); </span> </li> <li class="alt"> <span>$</span><span class="attribute">connstr</span><span> = </span><span class="attribute-value">"DRIVER={Microsoft<br> Access Driver (*.mdb)}; <br>DBQ="</span><span>. realpath("data/db.mdb"); </span> </li> <li> <span>$conn-</span><span class="tag">></span><span>Open($connstr); </span> </li> <li class="alt"> <span>$</span><span class="attribute">rs</span><span> = </span><span class="attribute-value">new</span><span> com("ADODB.RecordSet"); </span> </li> <li> <span>$rs-</span><span class="tag">></span><span>Open("select *<br> from szd_t",$conn,1,1); </span> </li> <li class="alt"> <span>while(! $rs-</span><span class="tag">></span><span>eof) { </span> </li> <li> <span>$</span><span class="attribute">f</span><span> = $rs-</span><span class="tag">></span><span>Fields(1); </span> </li> <li class="alt"> <span>echo $f-</span><span class="tag">></span><span>value; </span> </li> <li> <span>$rs-</span><span class="tag">></span><span>MoveNext(); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
PHP连接数据库之PHP连接MS SQL数据库代码方法
1.安装SQL服务器并添加PHP的MSSQL扩展
2.使用以下代码连接并测试
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">myServer</span><span> = </span><span class="attribute-value">localhost</span><span>; //主机 </span> </li> <li class="alt"> <span>$</span><span class="attribute">myUser</span><span> = </span><span class="attribute-value">sa</span><span>; //用户名 </span> </li> <li> <span>$</span><span class="attribute">myPass</span><span> = </span><span class="attribute-value">password</span><span>; //密码 </span> </li> <li class="alt"> <span>$</span><span class="attribute">myDB</span><span> = </span><span class="attribute-value">Northwind</span><span>; //MSSQL库名 </span> </li> <li> <span>$</span><span class="attribute">s</span><span> = @mssql_connect($myServer,<br> $myUser, $myPass) </span> </li> <li class="alt"><span>or die(Couldnt connect to <br>SQL Server on $myServer); </span></li> <li> <span>$</span><span class="attribute">d</span><span> = @mssql_select_db($myDB, $s) </span> </li> <li class="alt"><span>or die(Couldnt open database $myDB); </span></li> <li> <span>$</span><span class="attribute">query</span><span> = </span><span class="attribute-value">SELECT</span><span> TitleOfCourtesy<br>+ +FirstName+ +LastName AS Employee ; </span> </li> <li class="alt"> <span>$query </span><span class="attribute">.</span><span>= </span><span class="attribute-value">FROM</span><span> Employees ; </span> </li> <li> <span>$query </span><span class="attribute">.</span><span>= </span><span class="attribute-value">WHERE</span><span> </span><span class="attribute">Country</span><span>=</span><span class="attribute-value">USA</span><span> <br>AND Left(HomePhone, 5) = (206); </span> </li> <li class="alt"> <span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">mssql_query</span><span>($query); </span> </li> <li> <span>$</span><span class="attribute">numRows</span><span> = </span><span class="attribute-value">mssql_num_rows</span><span>($result); </span> </li> <li class="alt"> <span>echo </span><span class="tag"><span class="tag-name">h1</span><span class="tag">></span><span> . $numRows . Row .<br> ($</span><span class="attribute">numRows</span><span> == 1 ? : s) . Returned </span><span class="tag"> </span><span class="tag-name">h1</span><span class="tag">></span><span>; </span></span> </li> <li> <span>while($</span><span class="attribute">row</span><span> = </span><span class="attribute-value">mssql_fetch_array</span><span>($result)) </span> </li> <li class="alt"><span>{ </span></li> <li> <span>echo </span><span class="tag"><span class="tag-name">li</span><span class="tag">></span><span> . $row[Employee] . </span><span class="tag"><span class="tag-name">li</span><span class="tag">></span><span>; </span></span></span> </li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
PHP连接数据库之PHP连接Oracle数据库
PHP提供了两套函数与Oracle连接,分别是ORA_和OCI函数。其中ORA_函数略显陈旧。OCI函数更新据说更好一些。两者的使用语法几乎相差无几。你的PHP安装选项应该可以支持两者的使用。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li class="alt"><span><span>if ($</span><span class="attribute">conn</span><span>=</span><span class="attribute-value">Ora_Logon<br><br></span><span>("user@TNSNAME","password")) </span></span></li> <li><span>{ echo "SUCCESS ! <br>Connected to databasen"; </span></li> <li class="alt"><span>}else </span></li> <li><span>{echo "Failed :-( <br>Could not connect to databasen";} </span></li> <li class="alt"><span>Ora_Logoff($conn); </span></li> <li><span>phpinfo(); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
以上PHP连接数据库的代码使用TNSNAME(在你的tnsnames.ora文件中指明)定义的Oracle数据库名称、用户名称和密码连接数据库。在成功连接的基础上,ora_logon函数返回一个非零的连接ID并储存在变量$conn中。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

In-depth analysis of the top 15 Bitcoin Escape Index: Market Outlook for 2025 This article deeply analyzes fifteen commonly used Bitcoin Escape Index, among which the Bitcoin Rhodl ratio, USDT current wealth management and altcoin seasonal index have reached the Escape Index in 2024, attracting market attention. How should investors deal with potential risks? Let us interpret these indicators one by one and explore reasonable response strategies. 1. Detailed explanation of key indicators AHR999 coin hoarding indicator: Created by ahr999, assisting Bitcoin fixed investment strategy. The current value is 1.21, which is in the wait-and-see range, so it is recommended to be cautious. Link to AHR999 Escape Top Indicator: A supplement to AHR999 Coin Hoarding Indicator, used to identify the top of the market. The current value is 2.48, this week

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content
