这篇文章主要为大家详细介绍了php 操作mysql数据库的简单示例,具有一定的参考价值,可以用来参考一下。
php操作mysql的简单范例,感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
/**
* 操作mysql
*
* @param
* @arrange (512.笔记) www.q1010.com
**/
// connect to the database
$con = mysql_connect('localhost','testuser','testpassword')
or die('Could not connect to the server!');
// select a database:
mysql_select_db('testdb')
or die('Could not select a database.');
/*
** Fetch some rows from database:
*/
// read username from URL
$username = $_GET['username'];
// escape bad chars:
$username = mysql_real_escape_string($username);
// build query:
$sql = "SELECT id, timestamp, text FROM logs WHERE username = '$username'";
// execute query:
$result = mysql_query($sql)
or die('A error occured: ' . mysql_error());
// get result count:
$count = mysql_num_rows($result);
print "Showing $count rows:<hr/>";
// fetch results:
while ($row = mysql_fetch_assoc($result)) {
$row_id = $row['id'];
$row_text = $row['text'];
print "#$row_id: $row_text<br/>\n";
}
/*
** Do a insert query:
*/
// create SQL query:
$sql = "INSERT INTO logs (timestamp, text) VALUES (NOW(), 'some text here!')";
// execute query:
$result = mysql_query($sql) or die('A error occured: ' . mysql_error());
// get the new ID of the last insert command
$new_id = mysql_insert_id();
/*
** Do a update query:
*/
// create SQL query:
$sql = "UPDATE logs SET text='New text!' WHERE id='1'";
// execute query:
$result = mysql_query($sql) or die('A error occured: ' . mysql_error());
/*
** Do a delete query:
*/
// create SQL query:
$sql = "DELETE FROM logs WHERE id='1'";
// execute query:
$result = mysql_query($sql) or die('A error occured: ' . mysql_error());
// Have fun!
/*** 代码来自四海网(www.q1010.com) ***/
本文来自:http://www.q1010.com/173/339-0.html
注:关于php 操作mysql数据库的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:数据库
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。