Home >Backend Development >PHP Problem >How to connect php to mysql and query data
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".
Recommended: "PHP Video Tutorial"
php connection and query mysql database
<?php /** * Created by PhpStorm. * User: karnetkarnet * Date: 17/3/3 * Time: 09:46 */ define('SQL_HOST','localhost');//数据库地址 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['goods_name']; 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!