我已经阅读了之前类似标题的问题,似乎没有一个能给我提供这个特殊情况的答案。我收到一个特定的功能上面提到的错误。我不知道是什么让它突然出现。这是我的第一个开发,所以,除非它是专门解决错误,请忽略我应该使用PDO或mysqli的事实。
这是我试图实例化的函数。当SQL命令单独执行时,它将返回正确的结果。
public function search_for_candidates_by_technology($technology, $seniority){
$technology = $this->real_escape_string($technology);
$seniority = $this->real_escape_string($seniority);
$this->query("SELECT * FROM candidates WHERE technology LIKE ". $technology ." AND seniority LIKE ". $seniority ."");
}
函数所属的类是tecnoDB在我尝试实例化的实际页面中,代码如下:
<form name="buscarBase" action="buscarCV.php" method="POST">Que technologia:<input type="text" name="usertech" value=""/><br/>
Que seniority:<input type="text" name="userSeniority" value="" />
<input type="submit" name="buscar" value="Buscar" />
<input type="submit" name="back" value="Panel de Control"/>
</form>
<table border="black">
<tr><th>Technology</th><th>Seniority</tr>
<?php
$search = tecnoDB::getInstance()->search_for_candidates_by_technology($_POST['usertech'], $_POST['userSeniority']);
while($searchResult = mysql_fetch_array($search)){
echo "<tr><td>" . htmlentities($searchResult['technology']) ."</td>";
echo "<td>". htmlentities($searchResult['seniority']) . "</td></tr>";
}
?>
</table>
错误即将出现:while($searchResult = mysql_fetch_array($search))....这让我认为问题是$search没有被创建为实例。什么好主意吗?
这是我的第一个项目和第一个问题,请温柔。
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
session_start();
if (!array_key_exists("user", $_SESSION)) {
header('Location: index.php');
exit;
}
require_once("Includes/tecnoDB.php");
$company_id = tecnoDB::getInstance()->get_company_id_by_name($_SESSION['user']);
if ($_SERVER['REQUEST_METHOD'] == "POST"){
if (array_key_exists("back", $_POST)) {
header('Location: companyControlPanel.php' );
exit;
}
else{
$service_user = tecnoDB::getInstance()->verify_service_status($company_id);
$access = $service_user->fetch_row();
if (array_key_exists ("buscar", $_POST)){
if($access[0] < 2 ){
header("Location: selectServicePackage.php" );
exit;
}
}
}
}
// put your code here ?>
<form name="buscarBase" action="buscarCV.php" method="POST">Que tecnologia:<input type="text" name="usertech" value=""/><br/>
Que seniority:<input type="text" name="userSeniority" value="" />
<input type="submit" name="buscar" value="Buscar" />
<input type="submit" name="back" value="Panel de Control"/>
</form>
<table border="black">
<tr><th>Technology</th><th>Seniority</tr>
<?php
$search = tecnoDB::getInstance()->search_for_candidates_by_technology($_POST['usertech'], $_POST['userSeniority']);
while($searchResult = mysql_fetch_array($search)){
echo "<tr><td>" . htmlentities($searchResult['technology']) ."</td>";
echo "<td>". htmlentities($searchResult['seniority']) . "</td></tr>";
}
?>
</table>
</body>
</html>
here goes the tecnoDB class:
class tecnoDB extends mysqli {
// single instance of self shared among all instances
private static $instance = null;
// db connection config vars
private $user = "phpuser";
private $pass = "phpuserpw";
private $dbName = "tecnosearch";
private $dbHost = "localhost";
//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
// private constructor
private function __construct() {
parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
if (mysqli_connect_error()) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
parent::set_charset('utf-8');
}
public function get_company_id_by_name($name) {
$name = $this->real_escape_string($name);
$company = $this->query("SELECT id FROM company WHERE name = '"
. $name . "'");
if ($company->num_rows > 0){
$row = $company->fetch_row();
return $row[0];
} else
return null;
}
public function get_searches_by_company_id($company_id) {
return $this->query("SELECT id, description, technology FROM searches WHERE company_id=" . $company_id);
}
public function create_company ($name, $password){
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$this->query("INSERT INTO company (name, password) VALUES ('" . $name . "', '" . $password . "')");
}
public function verify_company_credentials ($name, $password){
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$result = $this->query("SELECT 1 FROM company
WHERE name = '" . $name . "' AND password = '" . $password . "'");
return $result->data_seek(0);
}
public function verify_service_status ($company_id){
$company_id = $this->real_escape_string($company_id);
$service = $this->query("SELECT service FROM company WHERE id = '". $company_id ."'");
return $service;
}
function insert_search($company_id, $description, $technology){
$description = $this->real_escape_string($description);
$technology = $this->real_escape_string($technology);
$this->query("INSERT INTO searches (company_id, description, technology)" .
" VALUES (" . $company_id . ", '" . $description . "','" .$technology. "')");
}
public function search_for_candidates_by_technology($technology, $seniority){
$technology = $this->real_escape_string($technology);
$seniority = $this->real_escape_string($seniority);
$this->query("SELECT * FROM candidates WHERE technology LIKE ". $technology ." AND seniority LIKE ". $seniority ."");
}
}
?>
我通过在search_for_candidates_by_technology = $variable中设置查询并返回该变量以及在需要指定此函数的文件的实际页面中修复了该错误。我将search_for_candidates_by_technology的实例设置为$variable1,并创建另一个对象作为$variable1->get_array的结果;. 我的错误信息现在消失了,但结果没有出现在搜索中。我假设,因为这个动作在同一个页面上,它导致页面重新加载,当它重新加载时,它本质上是重置。我正在考虑使用AJAX来显示结果,但我从未使用过异步javascript,并且只简要地看到过xml。有没有不需要AJAX的指针或想法?