首頁  >  文章  >  後端開發  >  PHP學習-1

PHP學習-1

WBOY
WBOY原創
2016-07-29 09:10:45910瀏覽

入門小實驗

<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>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了PHP學習-1,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn