首頁  >  文章  >  後端開發  >  用PHP向資料庫中實作簡單的增刪改查(純程式碼,待完善)

用PHP向資料庫中實作簡單的增刪改查(純程式碼,待完善)

WBOY
WBOY原創
2016-08-08 09:21:071276瀏覽

<?<span>php
    </span><span>$con</span> = <span>mysql_connect</span>("localhost:3306","root",""<span>);

    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>$result</span> = <span>mysql_query</span>("SELECT * FROM user"<span>);

    </span><span>echo</span> "<span><table border='1'>
    <tr>
    <th>Username</th>
    <th>Password</th>
    </tr>"<span>;

    </span><span>while</span>(<span>$row</span> = <span>mysql_fetch_array</span>(<span>$result</span><span>)) {
        </span><span>echo</span> "<tr>"<span>;
        </span><span>echo</span> "<td>" . <span>$row</span>['username'] . "</td>"<span>;
        </span><span>echo</span> "<td>" . <span>$row</span>['password'] . "</td>"<span>;
        </span><span>echo</span> "</tr>"<span>;
    }
    </span><span>echo</span> "</table>"<span>;

    </span><span>mysql_close</span>(<span>$con</span><span>);

</span>?>
從伺服器中獲取使用者所有資訊(SQLc

<?<span>php
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>mysql_query</span>("DELETE FROM user WHERE username = '<span>$_POST</span>[username]'"<span>);

    </span><span>mysql_close</span>(<span>$con</span><span>);
</span>?>

註冊一個新用戶insert.php

<?<span>php
    </span><span>$con</span> = <span>mysql_connect</span>("localhost:3306","root",""<span>);

    </span><span>if</span> (!<span>$con</span><span>) {
        </span><span>die</span>('Could not connect: ' . <span>mysql_error</span><span>());
    }

    </span><span>mysql_select_db</span>("test", <span>$con</span><span>);

    </span><span>$sql</span> = "<span>INSERT INTO user (username,password)
    VALUES
    ('</span><span>$_POST</span>[username]','<span>$_POST</span>[password]')"<span>;

    </span><span>if</span> (!<span>mysql_query</span>(<span>$sql</span>,<span>$con</span><span>)) {
        </span><span>die</span>('Error: ' . <span>mysql_error</span><span>());
    }

    </span><span>echo</span> "1 record added"<span>;

    </span><span>mysql_close</span>(<span>$con</span><span>);

</span>?>

修改一個用戶密碼update.php

reee

以上就介紹了用PHP向資料庫中實現簡單的增刪改查(純程式碼,待完善),包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

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