链接数据库mysql_connect($hostname, $username, $password);
选择数据库mysql_select_db($database);
执行SQL语句$sql = "SELECT * FROM news"; $result = mysql_query ($sql);
获取数据集中的一行$row = mysql_fetch_assoc($result)
链接数据库示例:
Code
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
?>
escape()函数来保证它被恰当的编码。$this->escape($this->title);其中去掉$this->escape()也没什么问题。