首页 >后端开发 >php教程 >几种常用PHP连接数据库的代码示例_PHP教程

几种常用PHP连接数据库的代码示例_PHP教程

WBOY
WBOY原创
2016-07-15 13:32:27838浏览

PHP连接数据库之PHP连接MYSQL数据库代码

  1.  ?php   
  2. $mysql_server_name='localhost'
  3. //改成自己的mysql数据库服务器  
  4. $mysql_username='root'
  5. //改成自己的mysql数据库用户名  
  6. $mysql_password='12345678'
  7. //改成自己的mysql数据库密码  
  8. $mysql_database='mycounter';
  9.  //改成自己的mysql数据库名  
  10. $conn=mysql_connect($mysql_server_name,
    $mysql_username,$mysql_password,
    $mysql_database);   
  11. $sql='CREATE DATABASE mycounter 
    DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;   
  12. ';   
  13. mysql_query($sql);   
  14. $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;'
    ;   
  15. mysql_select_db($mysql_database,$conn);   
  16. $result=mysql_query($sql);   
  17. //echo $sql;   
  18. mysql_close($conn);   
  19. echo "Hello!数据库mycounter已经成功建立!";   
  20. ?> 

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中。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446144.htmlTechArticlePHP连接数据库之PHP连接MYSQL数据库代码 ?php $ mysql_server_name = 'localhost' ; //改成自己的mysql数据库服务器 $ mysql_username = 'root' ; //改成自己的my...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn