ow to connect to MySQL database using PHP |
<!---->
|
|
Before you can get content out of your MySQL database, you must know
how to establish a connection to MySQL from inside a PHP script. To
perform basic queries from within MySQL is very easy. This article will
show you how to get up and running.
Let's get started. The first thing to do is connect to the
database.The function to connect to MySQL is called mysql_connect. This
function returns a resource which is a pointer to the database
connection. It's also called a database handle, and we'll use it in
later functions. Don't forget to replace your connection details.
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
|
All going well, you should see "Connected to MySQL" when you run
this script. If you can't connect to the server, make sure your
password, username and hostname are correct.
Once you've connected, you're going to want to select a database to
work with. Let's assume the database is called 'examples'. To start
working in this database, you'll need the mysql_select_db() function:
<?php
//select a database to work with
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
?>
|
Now that you're connected, let's try and run some queries. The
function used to perform queries is named - mysql_query(). The function
returns a resource that contains the results of the query, called the
result set. To examine the result we're going to use the
mysql_fetch_array function, which returns the results row by row. In
the case of a query that doesn't return results, the resource that the
function returns is simply a value true or false.
A convenient way to access all the rows is with a while loop. Let's add the code to our script:
<?php
//execute the SQL query and return records
$result = mysql_query("SELECT id, model, year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'model'}."
".$row{'year'}."<br>";
}
?>
|
Finally, we close the connection. Although this isn't strictly
speaking necessary, PHP will automatically close the connection when
the script ends, you should get into the habit of closing what you open.
<?php
//close the connection
mysql_close($dbhandle);
?>
|
Here is a code in full:
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
$row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>
|
To create 'examples' database on your MySQL server you should run the following script:
CREATE DATABASE `examples`;
USE `examples`;
CREATE TABLE `cars` (
`id` int UNIQUE NOT NULL,
`name` varchar(40),
`year` varchar(50),
PRIMARY KEY(id)
);
INSERT INTO cars VALUES(1,'Mercedes','2000');
INSERT INTO cars VALUES(2,'BMW','2004');
INSERT INTO cars VALUES(3,'Audi','2001');
分享到:
相关推荐
PHP连接MySQL
一个普遍通用的PHP连接MYSQL数据库类
Php连接mysql类。用法如下: 加入数据库连接类文件和配置文件 require_once ('DBConnect.class.php'); require_once ('dbconfig.php'); $dbconnect = new DBConnect ( CP_CONN_HOST, CP_CONN_USER, CP_CONN_PASS, ...
在我们从我们的MySQL数据库中获取我们的Web页面所包含的内容之前,我们首先必须知道如何建立与MySQL的连接。在第二章中,我们使用了一个叫mysql的程序来做...PHP不需要这样的一个程序,对连接MySQL的支持是语言内置的。
PHP连接MYSQL数据库PHP连接MYSQL数据库
本文章是一款比较实例的php 连接mysql数据库的连接类,比起一般的php mysql数据库函数要实用方法了很多,操作维护起来也很简单,只要处理这一个就KO了,实例化时自动连接数据库,连接mysql服务器,连接数据库,设置字符...
php 连接mysql数据库做简单的登录页面,实现用户的登录注册功能,连接数据库需改动一下conn.php里面的链接参数
这是一款比较全的mysql操作类,包括数据查询,更新,删除,等操作,实例代码见压缩包。需要的朋友可以参考下,方便大家学习php
php连接Mysql数据库
NULL 博文链接:https://onestopweb.iteye.com/blog/2371715
php连接mysql,适合初学者的连接mysql数据库的最好代码
php 连接 mysql 数据库代码 哈哈哈哈哈
自己重新封装的类库,是php连接mysql数据库的类库。这个类库以前是连接oracle的,自己重新封装后改成连接mysql了,不存在版权问题。就是没有时间改动例子代码,急需分,要不也不用放上来了。
MySQL-php连接MySQL.pdf 学习资料 复习资料 教学资源
PHP连接MYSQL代码 可以直接调用到工程文件中
用来连接mysql数据库,获取与mysql数据库的连接,十分方便。
用PHP连接MySQL.pdf
php连接mysql数据库增删改查、分页、搜索、md5加密、二进制,建立数据库在select.php页面底部有提示,数据库名字:dome1.表名:demo1
PHP连接数据库有多种方法,现介绍常用的MySQL数据库连接方法,PHP连接MySQL也有两种方式,一是面向对象,二是面向过程方式,两种方法稍有区别。下面通过代码介绍两种方法连接MySQL并以json格式输出。 2、面向对象...
4.5 PHP连接mysql数据库.pptx