Home  >  Article  >  Backend Development  >  How to connect php to mysql and query data

How to connect php to mysql and query data

藏色散人
藏色散人Original
2020-10-13 09:25:153885browse

How to connect php to mysql and query data: first create a PHP sample file; then connect to the database through the username and password; and finally query through the query statement "select* from goods".

How to connect php to mysql and query data

Recommended: "PHP Video Tutorial"

php connection and query mysql database

<?php
/**
 * Created by PhpStorm.
 * User: karnetkarnet
 * Date: 17/3/3
 * Time: 09:46
 */
define(&#39;SQL_HOST&#39;,&#39;localhost&#39;);//数据库地址
define("SQL_USER","root");//数据库用户名
define("SQL_PASSWORD","");//数据库密码
define("SQL_DATABASE","shop");//连接的数据库名字
define("SQL_PORT","3306");//数据库端口号,默认为3306
//define("SQL_SOCKDET","");
$mysql = mysqli_connect(SQL_HOST,SQL_USER,SQL_PASSWORD,SQL_DATABASE,SQL_PORT) or  die(mysqli_error());
//连接不上切换数据库
//mysqli_select_db(SQLDATABASE);
//查询语句
$sql = "select* from goods";
//查询
$results = $mysql -> query($sql);
print_r($results);
//遍历循环打印数据
while ($row = mysqli_fetch_array($results))
{
//    print_r($row);
    echo $row[&#39;goods_name&#39;];
    echo "<br>";
}
//释放
mysqli_free_result($results);
//关闭连接
mysqli_close($mysql);
?>

The above is the detailed content of How to connect php to mysql and query data. For more information, please follow other related articles on the PHP Chinese website!

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