我写了一个你可以学习的例子,如果你真的不想让一个成熟的 mySQL 服务器处理你的数据,那么这里是你如何使用内置的 sqlite2 函数来完成它,超级简单,对任何小数据都非常有用sql 类型总是比平面文件查询更快,插入更容易。希望能帮助到你:
<?php
//Create db or return connection
$db = setup('scores.db');
//form so you can enter data
echo '<h1>Put Score demo:</h1>
<form method="POST" action="">
<p>Name: <input type="text" name="name" size="20"></p>
<p>Score: <input type="text" name="m_score" size="20"><input type="submit" value="Submit"></p>
</form>';
//Validate score as a number and the name is set
if(isset($_POST['m_score']) && is_numeric($_POST['m_score']) && !empty($_POST['name'])){
$name = $_POST['name'];
$m_score = $_POST['m_score'];
//Put result into database
database("INSERT INTO scores (id,name , m_score) VALUES (NULL,'".sqlite_escape($name)."',".(int)$m_score.")",'put',$db);
//Show users score
echo '<table width="5%" border="5"><tr><th>Your Score</th><th>Your Name</th></tr>';
echo '<tr><td>'.htmlentities($m_score).'</td><td>'.htmlentities($name).'</td></tr>';
echo '</table>';
echo "<br />";
}else{
//Get Results from database
$result = database("SELECT DISTINCT name, m_score from scores ORDER BY m_score DESC",'get',$db);
//Show global score
echo '<table width="5%" border="1"><tr><th>Top Scores</th><th>Top Users</th></tr>';
foreach ($result as $row){
echo '<tr>';
echo "<td>".htmlentities($row['m_score'])."</td>";
echo "<td>".htmlentities($row['name'])."</td>";
echo "</tr>";
}
echo '</table>';
}
/**
* Simple function to setup the sqlite database or return a connection to that db
*
* @param string $db_name
* @return mixed (Connection or setup success)
*/
function setup($db_name){
global $db;
if(file_exists($db_name)){
if(isset($db)){return $db;}
return sqlite_open($db_name, 0666, $error);
}
$db = sqlite_open($db_name, 0666, $error);
(!$db)?die($error):null;
//Create new db
$query ="CREATE TABLE scores (id INTEGER PRIMARY KEY, name TEXT(255), m_score INTEGER)";
$ok = sqlite_exec($db, $query, $error);
(!$ok)?die("Cannot execute query. $error"):null;
echo 'Setup completed, Refresh page.';
die;
}
/**
* A simple sqlite CRUD with U & D missing ;p
*
* @param (sql query) $query
* @param (put|get) $do
* @param (Connection resource) $db
* @return result
*/
function database($query,$do,$db){
$error=null;
$return=null;
switch($do){
case "get":
$result = sqlite_query($db, $query, $error);
if (!$result) die("Cannot execute query {$query} ". $error);
$i=0;
while ($entry = sqlite_fetch_array($result, SQLITE_ASSOC)) {
foreach($entry as $key=>$value){
$return[$i][$key]=$value;
}
$i++;
}
return $return;
break;
case "put":
$result = sqlite_exec($db, $query, $error);
if ($result==false){ die("Cannot execute statement {$query} ". $error);}else{ return sqlite_changes($db);}
break;
default:
die("Missing 2nd argument for database function.");
break;
}
return true;
}
/**
* Function to escape user input for db
*
* @param unknown_type $data
* @return unknown
*/
function sqlite_escape($data) {
if(is_array($data)){return array_map("sqlite_escape_string", $data);}
return sqlite_escape_string($data);
}
?>