php 简单的MySQL例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单的MySQL例子相关的知识,希望对你有一定的参考价值。

<span style="color: #808080; font-style: italic;">/*
** Connect to database:
*/</span>
&nbsp;
<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;">&#40;</span><span style="color: #ff0000;">'localhost'</span>,<span style="color: #ff0000;">'testuser'</span>,<span style="color: #ff0000;">'testpassword'</span><span style="color: #66cc66;">&#41;</span> 
    or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Could not connect to the server!'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #ff0000;">'testdb'</span><span style="color: #66cc66;">&#41;</span> 
    or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Could not select a database.'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Fetch some rows from database:
*/</span>
&nbsp;
<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;">&#91;</span><span style="color: #ff0000;">'username'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// build query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;SELECT id, timestamp, text FROM logs WHERE username = '$username'&quot;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span> 
    or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</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;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Showing $count rows:&lt;hr/&gt;&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// fetch results:</span>
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</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;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$row_id</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #0000ff;">$row_text</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'text'</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    <a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;#$row_id: $row_text&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Do a insert query:
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// create SQL query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;INSERT INTO logs (timestamp, text) VALUES (NOW(), 'some text here!')&quot;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</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;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Do a update query:
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// create SQL query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;UPDATE logs SET text='New text!' WHERE id='1'&quot;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</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;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/*
** Do a delete query:
*/</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// create SQL query:</span>
<span style="color: #0000ff;">$sql</span> = <span style="color: #ff0000;">&quot;DELETE FROM logs WHERE id='1'&quot;</span>;
&nbsp;
<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;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #66cc66;">&#41;</span> or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</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;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">// Have fun!</span>

以上是关于php 简单的MySQL例子的主要内容,如果未能解决你的问题,请参考以下文章

想做个 网站 ,求一段PHP编程代码,PHP的MYSQL缓存怎么实现? 最好举个例子。

PHP + MySQL 事务示例

多对多关系示例

memcached +mysql+php 测试例子

php分页例子实现读取mysql数据分页显示

memcached +mysql+php 例子