php 简单的MySQL例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单的MySQL例子相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">/*
** Connect to database:
*/</span>
<span style="color: #808080; font-style: italic;">// connect to the database</span>
<span style="color: #0000ff;">$con</span> = <a href="http://www.php.net/mysql_connect"><span style="color: #000066;">mysql_connect</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'localhost'</span>,<span style="color: #ff0000;">'testuser'</span>,<span style="color: #ff0000;">'testpassword'</span><span style="color: #66cc66;">)</span>
or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Could not connect to the server!'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// select a database:</span>
<a href="http://www.php.net/mysql_select_db"><span style="color: #000066;">mysql_select_db</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'testdb'</span><span style="color: #66cc66;">)</span>
or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Could not select a database.'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/*
** Fetch some rows from database:
*/</span>
<span style="color: #808080; font-style: italic;">// read username from URL</span>
<span style="color: #0000ff;">$username</span> = <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'username'</span><span style="color: #66cc66;">]</span>;
<span style="color: #808080; font-style: italic;">// escape bad chars:</span>
<span style="color: #0000ff;">$username</span> = <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #000066;">mysql_real_escape_string</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// build query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"SELECT id, timestamp, text FROM logs WHERE username = '$username'"</span>;
<span style="color: #808080; font-style: italic;">// execute query:</span>
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">)</span>
or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'A error occured: '</span> . <a href="http://www.php.net/mysql_error"><span style="color: #000066;">mysql_error</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// get result count:</span>
<span style="color: #0000ff;">$count</span> = <a href="http://www.php.net/mysql_num_rows"><span style="color: #000066;">mysql_num_rows</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"Showing $count rows:<hr/>"</span>;
<span style="color: #808080; font-style: italic;">// fetch results:</span>
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$row</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #000066;">mysql_fetch_assoc</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<span style="color: #0000ff;">$row_id</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">]</span>;
<span style="color: #0000ff;">$row_text</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'text'</span><span style="color: #66cc66;">]</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"#$row_id: $row_text<br/><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span>
<span style="color: #808080; font-style: italic;">/*
** Do a insert query:
*/</span>
<span style="color: #808080; font-style: italic;">// create SQL query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"INSERT INTO logs (timestamp, text) VALUES (NOW(), 'some text here!')"</span>;
<span style="color: #808080; font-style: italic;">// execute query:</span>
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">)</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'A error occured: '</span> . <a href="http://www.php.net/mysql_error"><span style="color: #000066;">mysql_error</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// get the new ID of the last insert command</span>
<span style="color: #0000ff;">$new_id</span> = <a href="http://www.php.net/mysql_insert_id"><span style="color: #000066;">mysql_insert_id</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/*
** Do a update query:
*/</span>
<span style="color: #808080; font-style: italic;">// create SQL query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"UPDATE logs SET text='New text!' WHERE id='1'"</span>;
<span style="color: #808080; font-style: italic;">// execute query:</span>
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">)</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'A error occured: '</span> . <a href="http://www.php.net/mysql_error"><span style="color: #000066;">mysql_error</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">/*
** Do a delete query:
*/</span>
<span style="color: #808080; font-style: italic;">// create SQL query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">"DELETE FROM logs WHERE id='1'"</span>;
<span style="color: #808080; font-style: italic;">// execute query:</span>
<span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">(</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">)</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'A error occured: '</span> . <a href="http://www.php.net/mysql_error"><span style="color: #000066;">mysql_error</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Have fun!</span>
以上是关于php 简单的MySQL例子的主要内容,如果未能解决你的问题,请参考以下文章