Home Database Mysql Tutorial php mysql connects to database instance_MySQL

php mysql connects to database instance_MySQL

Nov 30, 2016 pm 11:59 PM
mysql php

An aside, I changed the my.ini encoding of the data to utf-8 at night, and then the database could not be started. I just changed it back to gbk. If anyone knows, let me know what the problem is.

Because it is a link to the database, there is nothing to explain, so just go to the code.

<&#63;php

/* Connect to a MySQL server 连接数据库服务器 */
$link = mysqli_connect(
  'localhost', /* The host to connect to 连接MySQL地址 */
  'jian',   /* The user to connect as 连接MySQL用户名 */
  '123456', /* The password to use 连接MySQL密码 */
  'jian');  /* The default database to query 连接数据库名称*/

if (!$link) {
  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
  exit;
}else
  echo '数据库连接上了!';

/* Close the connection 关闭连接*/
mysqli_close($link);
&#63;>

The jian here is actually the administrator of one of the libraries named jian. There is no need to write the database administrator, because in actual projects, what we may get is the database account, password, and host address of a library administrator.

Test results:

Original database table:

Operation 1: Query student IDs, classes and scores with scores greater than 60

SELECT id,class,scores FROM jian_scores WHERE scores>60

All codes:

<&#63;php

/* Connect to a MySQL server 连接数据库服务器 */
$link = mysqli_connect(
  'localhost', /* The host to connect to 连接MySQL地址 */
  'jian',   /* The user to connect as 连接MySQL用户名 */
  '123456', /* The password to use 连接MySQL密码 */
  'jian');  /* The default database to query 连接数据库名称*/

if (!$link) {
  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
  exit;
}else
  echo '数据库连接上了!'. "<br/>";

if ($result = mysqli_query($link, 'SELECT id,class,scores FROM jian_scores WHERE scores>60 ')) {

echo('id 班级 分数 '). "<br/>";

/* Fetch the results of the query 返回查询的结果 */
while( $row = mysqli_fetch_assoc($result) ){
  echo $row['id'], " ", $row['class'], " ", $row['scores'], "<br/>";
  // printf("%s (%s) ", $row['id'],$row['class'], $row['scores']);
}

/* Destroy the result set and free the memory used for it 结束查询释放内存 */
mysqli_free_result($result);
}


/* Close the connection 关闭连接*/
mysqli_close($link);
&#63;>

Result:

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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)

Hot Topics

PHP Tutorial
1580
276
How to use the $_COOKIE variable in php How to use the $_COOKIE variable in php Aug 20, 2025 pm 07:00 PM

$_COOKIEisaPHPsuperglobalforaccessingcookiessentbythebrowser;cookiesaresetusingsetcookie()beforeoutput,readvia$_COOKIE['name'],updatedbyresendingwithnewvalues,anddeletedbysettinganexpiredtimestamp,withsecuritybestpracticesincludinghttponly,secureflag

How to work with arrays in php How to work with arrays in php Aug 20, 2025 pm 07:01 PM

PHParrayshandledatacollectionsefficientlyusingindexedorassociativestructures;theyarecreatedwitharray()or[],accessedviakeys,modifiedbyassignment,iteratedwithforeach,andmanipulatedusingfunctionslikecount(),in_array(),array_key_exists(),array_push(),arr

Describe the Observer design pattern and its implementation in PHP. Describe the Observer design pattern and its implementation in PHP. Aug 15, 2025 pm 01:54 PM

TheObserverdesignpatternenablesautomaticnotificationofdependentobjectswhenasubject'sstatechanges.1)Itdefinesaone-to-manydependencybetweenobjects;2)Thesubjectmaintainsalistofobserversandnotifiesthemviaacommoninterface;3)Observersimplementanupdatemetho

What is the difference between UNION and UNION ALL in MySQL? What is the difference between UNION and UNION ALL in MySQL? Aug 14, 2025 pm 05:25 PM

UNIONremovesduplicateswhileUNIONALLkeepsallrowsincludingduplicates;1.UNIONperformsdeduplicationbysortingandcomparingrows,returningonlyuniqueresults,whichmakesitsloweronlargedatasets;2.UNIONALLincludeseveryrowfromeachquerywithoutcheckingforduplicates,

How would you implement API versioning in a PHP application? How would you implement API versioning in a PHP application? Aug 14, 2025 pm 11:14 PM

APIversioninginPHPcanbeeffectivelyimplementedusingURL,header,orqueryparameterapproaches,withURLandheaderversioningbeingmostrecommended.1.ForURL-basedversioning,includetheversionintheroute(e.g.,/v1/users)andorganizecontrollersinversioneddirectories,ro

How to lock tables in MySQL How to lock tables in MySQL Aug 15, 2025 am 04:04 AM

The table can be locked manually using LOCKTABLES. The READ lock allows multiple sessions to read but cannot be written. The WRITE lock provides exclusive read and write permissions for the current session and other sessions cannot read and write. 2. The lock is only for the current connection. Execution of STARTTRANSACTION and other commands will implicitly release the lock. After locking, it can only access the locked table; 3. Only use it in specific scenarios such as MyISAM table maintenance and data backup. InnoDB should give priority to using transaction and row-level locks such as SELECT...FORUPDATE to avoid performance problems; 4. After the operation is completed, UNLOCKTABLES must be explicitly released, otherwise resource blockage may occur.

Explain database indexing strategies (e.g., B-Tree, Full-text) for a MySQL-backed PHP application. Explain database indexing strategies (e.g., B-Tree, Full-text) for a MySQL-backed PHP application. Aug 13, 2025 pm 02:57 PM

B-TreeindexesarebestformostPHPapplications,astheysupportequalityandrangequeries,sorting,andareidealforcolumnsusedinWHERE,JOIN,orORDERBYclauses;2.Full-Textindexesshouldbeusedfornaturallanguageorbooleansearchesontextfieldslikearticlesorproductdescripti

phpMyAdmin security best practices phpMyAdmin security best practices Aug 17, 2025 am 01:56 AM

To effectively protect phpMyAdmin, multiple layers of security measures must be taken. 1. Restrict access through IP, only trusted IP connections are allowed; 2. Modify the default URL path to a name that is not easy to guess; 3. Use strong passwords and create a dedicated MySQL user with minimized permissions, and it is recommended to enable two-factor authentication; 4. Keep the phpMyAdmin version up to fix known vulnerabilities; 5. Strengthen the web server and PHP configuration, disable dangerous functions and restrict file execution; 6. Force HTTPS to encrypt communication to prevent credential leakage; 7. Disable phpMyAdmin when not in use or increase HTTP basic authentication; 8. Regularly monitor logs and configure fail2ban to defend against brute force cracking; 9. Delete setup and

See all articles