search
HomeBackend DevelopmentPHP TutorialSeveral common code examples for connecting to databases in PHP_PHP Tutorial

Several common code examples for connecting to databases in PHP_PHP Tutorial

Jul 15, 2016 pm 01:32 PM
mysqlphpcodeseveral kindsCommonly useddatabaseofExampleconnect

PHP connection database code for PHP connection MYSQL database

  1. ?php
  2. $mysql_server_name='localhost';
  3. //Change to your own mysql database server
  4. $mysql_username='root' ;
  5. //Change to your own mysql database username
  6. $mysql_password='12345678';
  7. //Change to your own mysql Database password
  8. $mysql_database='mycounter' ;
  9. //Change to your own mysql database name
  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); >sql
  14. ='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); >mysql_query
    ($sql);
  15. //echo $sql; );
  16. echo "Hello! The database mycounter has been successfully created! "; ?> PHP connection to database PHP connection ACCESS database code method
  17. PHP connection database PHP connection MS SQL database code method
  18. 1. Install SQL server and add MSSQL extension for PHP
  19. 2. Use the following code to connect and test
  20. PHP connects to the database PHP connects to the Oracle database

PHP provides two sets of functions to connect to Oracle, namely ORA_ and OCI function. The ORA_ function is slightly older, and the usage syntax of the two is almost the same.

<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>
The above PHP code to connect to the database uses the Oracle database name, user name and password defined by TNSNAME (specified in your tnsnames.ora file) to connect to the database based on a successful connection. , the ora_logon function returns a non-zero connection ID and stores it in the variable $conn

.

http://www.bkjia.com/PHPjc/446144.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/446144.html
<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>

TechArticle

PHP connection database code for PHP connection MYSQL database?php $ mysql_server_name = 'localhost'; //Change to your own mysql database server$ mysql_username = 'root'; //Change to your own my...

Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software