약간의 입문 실험
<code><html> <title>My First Hello World</title> <body> <span><?php</span><span>// ?和php之间不能有空格</span><span>if</span>(!<span>empty</span>(<span>$_POST</span>[<span>'name'</span>])) { <span>echo</span><span>"Hello, {$_POST['name']}, Welcome to Our Web"</span>; } <span>?></span> <form action=<span>"<?php $PHP_SELF; ?>" method=<span>"post"</span>> Enter Your Name:<input type=<span>"text"</span> name=<span>"name"</span>/> <input type=<span>"submit"</span>/> </form> <span><?php</span><span>$mysql_host</span> = <span>"8.8.8.8"</span>; <span>$mysql_user</span> = <span>"guess"</span>; <span>$mysql_pwd</span> = <span>"guess"</span>; <span>$connect</span> = mysql_connect(<span>$mysql_host</span>,<span>$mysql_user</span>,<span>$mysql_pwd</span>) <span>or</span><span>die</span>(<span>"connect failed"</span> . mysql_error()); mysql_select_db(<span>"md_guess"</span>, <span>$connect</span>); <span>$sql</span> = <span>"select * from t_user_1 limit ${_POST['name']};"</span>; <span>$result</span> = mysql_query(<span>$sql</span>, <span>$connect</span>); <span>if</span>(<span>$result</span>) { <span>// 获取列表名</span><span>$title</span> = <span>array</span>(); <span>for</span> (<span>$i</span>=<span>0</span>; <span>$i</span><mysql_num_fields(<span>$result</span>); <span>$i</span>++) { <span>$title</span>[<span>$i</span>] = mysql_field_name(<span>$result</span>, <span>$i</span>); } <span>$data</span> = <span>array</span>(); <span>$num</span> = <span>0</span>; <span>// 获取数据</span><span>while</span> (<span>$row</span>=mysql_fetch_row(<span>$result</span>)) { <span>$item</span> = <span>array</span>(); <span>for</span> (<span>$i</span>=<span>0</span>; <span>$i</span><mysql_num_fields(<span>$result</span>); <span>$i</span>++ ) { <span>$item</span>[<span>$i</span>] = <span>$row</span>[<span>$i</span>]; } <span>$data</span>[<span>$num</span>] = <span>$item</span>; <span>$num</span> += <span>1</span>; } <span>// 显示数据</span><span>echo</span><span>'<table border="1" cellpadding="1" cellspacing="2">'</span>; <span>for</span>(<span>$i</span>=<span>0</span>; <span>$i</span><count(<span>$title</span>); <span>$i</span>++) { <span>echo</span><span>"<tr>"; <span>echo</span><span>"<td>"</span>; <span>echo</span><span>$title</span>[<span>$i</span>]; <span>echo</span><span>"</td>"</span>; <span>for</span>(<span>$j</span>=<span>0</span>; <span>$j</span><count(<span>$data</span>); <span>$j</span>++) { <span>echo</span><span>"<td>"</span>; <span>echo</span><span>$data</span>[<span>$j</span>][<span>$i</span>]; <span>echo</span><span>"</td>"</span>; } <span>echo</span><span>"</tr>"</span>; <span>//echo "<br>";</span> } <span>echo</span><span>"</table>"</span>; } <span>else</span> { <span>echo</span><span>"Query Failed"</span>; } <span>if</span>(<span>$result</span>) { mysql_freeresult(<span>$result</span>); } mysql_close(<span>$connect</span>); <span>?></span> </body> </html></code>
이상으로 PHP 학습-1을 다양한 측면에서 소개하였습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되었으면 좋겠습니다.