我已经阅读过类似标题的先前问题,似乎没有人能够为我提供这种特殊情况的答案。我收到上面提到的关于特定功能的错误。我不确定是什么让它弹出来。这是我的第一个开发,所以,除非它专门解决这个bug,请忽略我应该使用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不是作为实例创建的。有什么想法吗?
这是我的第一个项目和第一个问题,请温柔。
答案 0 :(得分:-2)
<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-&gt; get_array的结果; 。我的错误消息现在消失了,但结果没有出现在搜索中。我假设因为操作在同一页面上,它导致页面重新加载,当它重新加载时,它基本上是重置。我正在寻找使用AJAX来显示结果,但我从未使用异步javascript,只是简单地看到了XML。任何不需要AJAX的指针或想法?