php 操作mysql数据库的解决办法

内容摘要
这篇文章主要为大家详细介绍了php 操作mysql数据库的简单示例,具有一定的参考价值,可以用来参考一下。

php操作mysql的简单范例,对此感兴趣的朋友,看看idc笔记做的技术笔记。经
文章正文

这篇文章主要为大家详细介绍了php 操作mysql数据库的简单示例,具有一定的参考价值,可以用来参考一下。

php操作mysql的简单范例,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:

/**
 * 操作mysql
 *
 * @param 
 * @arrange (www.idcnote.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!


/***   代码来自php教程(www.idcnote.com)   ***/

注:关于php 操作mysql数据库的简单示例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!