我正在制作一个用于博客出版物的CRUD系统,但它有点奇怪,另一个开发人员(有更多经验)看着我的编码,对他来说也没关系,但这个错误(注意:试图获得财产第32行的C:\ xampp \ htdocs \ genial \ painel \ inc \ database.php中的非对象)仍然出现。
我的数据库代码:
Uncaught ReferenceError: IframeResizer is not defined
很抱歉,如果它没有被闲置。
我在
的“FROM”后面的代码上放了一个空格<?php
mysqli_report(MYSQLI_REPORT_STRICT);
function open_database() {
try {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
return $conn;
} catch (Exception $e) {
echo $e->getMessage();
return null;
}
}
function close_database($conn) {
try {
mysqli_close($conn);
} catch (Exception $e) {
echo $e->getMessage();
}
}
function find( $table = null, $id = null ) {
$database = open_database();
$found = null;
try {
if ($id) {
$sql = "SELECT * FROM" . $table . " WHERE id = " . $id;
$result = $database->query($sql);
if ($result->num_rows > 0) {
$found = $result->fetch_assoc();
}
}
else {
$sql = "SELECT * FROM " . $table;
$result = $database->query($sql);
if ($result->num_rows > 0) {
$found = $result->fetch_all(MYSQLI_ASSOC);
}
}
} catch (Exception $e) {
$_SESSION['message'] = $e->GetMessage();
$_SESSION['type'] = 'danger';
}
close_database($database);
return $found;
}
function find_all( $table ) {
return find($table);
}
function save($table = null, $data = null) {
$database = open_database();
$columns = null;
$values = null;
//print_r($data);
foreach ($data as $key => $value) {
$columns .= trim($key, "'") . ",";
$values .= "'$value',";
}
$columns = rtrim($columns, ',');
$values = rtrim($values, ',');
$sql = "INSERT INTO " . $table . "($columns)" . " VALUES " ($values);";
try {
$database->query($sql);
$_SESSION['message'] = 'Registro cadastrado com sucesso.';
$_SESSION['type'] = 'success';
} catch (Exception $e) {
$_SESSION['message'] = 'Nao foi possivel realizar a operacao.';
$_SESSION['type'] = 'danger';
}
close_database($database);
}
function update($table = null, $id = 0, $data = null) {
$database = open_database();
$items = null;
foreach ($data as $key => $value) {
$items .= trim($key, "'") . "='$value',";
}
$items = rtrim($items, ',');
$sql = "UPDATE " . $table;
$sql .= " SET $items";
$sql .= " WHERE id=" . $id . ";";
try {
$database->query($sql);
$_SESSION['message'] = 'Registro atualizado com sucesso.';
$_SESSION['type'] = 'success';
}
catch (Exception $e) {
$_SESSION['message'] = 'Nao foi possivel realizar a operacao.';
$_SESSION['type'] = 'danger';
}
close_database($database);
}
错误保持不变,但在第36行知道:
$sql = "SELECT * FROM" . $table . " WHERE id = " . $id;
将第36行的代码转为:
if ($result->num_rows > 0) {
$found = $result->fetch_all(MYSQLI_ASSOC);
}
错误消失,其他问题与此问题无关。
答案 0 :(得分:0)
行$sql = "SELECT * FROM" . $table . " WHERE id = " . $id;
在FROM之后是否有空格。
答案 1 :(得分:0)
如果此问题未被off-topic -> typo generated
关闭,我最好提供可接受的答案。
FROM
查询中的$table
和SELECT
之间添加空格。您的新代码可能如下所示:
$sql="SELECT * FROM `$table`";
if($id){
// assuming id has been properly sanitized
$sql.=" WHERE id=$id"; // concatenate with WHERE clause when appropriate
}
if($result=$database->query($sql)){ // only continue if result isn't false
if($result->num_rows){ // only continue if one or more row
$found=$result->fetch_all(MYSQLI_ASSOC); // fetch all regardless of 1 or more
}
}