我从教程中选择了这段代码并进行了编辑,使其适合我的需要。
<?php
//if we got something through $_POST
if (isset($_POST['search'])) {
// here you would normally include some database connection
include('config2.php');
$db = new db();
// never trust what user wrote! We must ALWAYS sanitize user input
$word = mysql_real_escape_string($_POST['search']);
// build your search query to the database
$sql = "SELECT name FROM $tbl_name WHERE name LIKE '%" . $word . "%'";
// get results
$row = $db->select_list($sql);
if(count($row)) {
$end_result = '';
foreach($row as $r) {
$result = $r['title'];
// we will use this to bold the search word in result
$bold = '<span class="found">' . $word . '</span>';
$end_result .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';
}
echo $end_result;
} else {
echo '<li>No results found</li>';
}
}
?>
运行代码时出现此错误:
Fatal error: Class 'db' not found in /home/peltdyou/public_html/do_search.php on line 6
我对PHP很陌生,所以任何人都可以解决我的问题..
更新:
<?php
class db {
function __construct()
{
global $dbh;
if (!is_null($dbh)) return;
$dbh = mysql_pconnect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);
mysql_query('SET NAMES utf8');
}
function select_list($query)
{
$q = mysql_query($query);
if (!$q) return null;
$ret = array();
while ($row = mysql_fetch_array($q, MYSQL_ASSOC)) {
array_push($ret, $row);
}
mysql_free_result($q);
return $ret;
}
}
?>
这是db.php代码..我想用我自己的config2.php文件替换它,我可以把它连接到我的数据库..显然不是
答案 0 :(得分:5)
我想我找到了您使用的示例脚本,并在其中说:
// here you would normally include some database connection
include('db.php');
在示例中,没有描述db.php,假设您有一个可以使用的数据库文件。进一步搜索,我找到了这个database class,他们称之为“database.php”,但你可以很容易地将它用于你正在尝试使用的代码。