php 致命错误,未找到类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10986348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal Error, Class not found
提问by user1449737
I have picked up this code from a tutorial and have edited it make it suitable for my needs.
我从教程中挑选了这段代码并对其进行了编辑,使其适合我的需要。
<?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>';
}
}
?>
I am getting this error when I run the code:
运行代码时出现此错误:
Fatal error: Class 'db' not found in /home/peltdyou/public_html/do_search.php on line 6
I am very new to PHP so could anyone shed some light onto my problem..
我对 PHP 很陌生,所以任何人都可以对我的问题有所了解。
UPDATE:
更新:
<?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;
}
}
?>
This is the db.php code.. I thought by replacing this with my own config2.php file I could get it to connect to my database.. Obviously not
这是 db.php 代码..我想通过用我自己的 config2.php 文件替换它我可以让它连接到我的数据库..显然不是
回答by GDP
I think I found the sample script you used, and in it, they say:
我想我找到了您使用的示例脚本,在其中,他们说:
// here you would normally include some database connection
include('db.php');
In the example, the db.php is not described, assuming that you have such a database file that you would use. A little further searching, I found this database class, which they call "database.php", but you could easily use it for the code that you're trying work with.
在该示例中,没有描述 db.php,假设您有这样一个要使用的数据库文件。进一步搜索,我找到了这个数据库类,他们称之为“database.php”,但您可以轻松地将它用于您尝试使用的代码。

![php SQLSTATE[HY093]:无效的参数号:未定义参数](/res/img/loading.gif)