php 操作mysql数据库的解决办法
内容摘要
这篇文章主要为大家详细介绍了php 操作mysql数据库的简单示例,具有一定的参考价值,可以用来参考一下。
php操作mysql的简单范例,对此感兴趣的朋友,看看idc笔记做的技术笔记。经
php操作mysql的简单范例,对此感兴趣的朋友,看看idc笔记做的技术笔记。经
文章正文
这篇文章主要为大家详细介绍了php 操作mysql数据库的简单示例,具有一定的参考价值,可以用来参考一下。
php操作mysql的简单范例,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <code class = "php" > /** * 操作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) ***/ </code> |
注:关于php 操作mysql数据库的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释