这是我的代码,用于检索信息并发送回ajax:
else if(isset($_POST['act']) && $_POST['act']=='getbook'){
$book = normalize_str(htmlentities(preg_replace('/\s+/',' ', $_POST['book']), ENT_QUOTES,'UTF-8'));
$filecat = '../books/'.$book.'/'.$book.'.txt';
$catlist = file($filecat, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
$stmt = $mysqli->stmt_init();
$query = "SELECT `id`,`title`,`price`,`image`,`description`,`category`,`enablerating`,`rating` FROM ".$PapierTableName." WHERE book = ? ORDER BY position ASC, title ASC;";
$prepared = $stmt->prepare($query);
if($prepared){
$stmt->bind_param('s', $book);
$stmt->execute();
$result = $stmt->bind_result($iden, $title, $price, $image, $description, $category, $enrat, $rating);
if($result)
{
$rit = array();
while($stmt->fetch())
{
$rit[] = array(
invert_str(html_entity_decode($title, ENT_QUOTES, 'UTF-8')),
$price,
invert_str(html_entity_decode($image, ENT_QUOTES, 'UTF-8')),
invert_str(html_entity_decode($description, ENT_QUOTES, 'UTF-8')),
invert_str(html_entity_decode($category, ENT_QUOTES, 'UTF-8')),
$enrat,
$rating,
$iden
);
}
$stmt->close();
$count = count($catlist);
$ret = array();
for($i = 0; $i < $count ; $i++)
$ret= array_merge($ret, search($rit, 4, $catlist[$i]));
file_put_contents('tre.txt',print_r($ret,true));
echo json_encode($ret);
}
else
file_put_contents('binderror.txt', $stmt->error);
}
else
file_put_contents('connecterror.txt',$stmt->error);
}
我的问题是,如果我将$iden
添加到数组中它不再起作用,我的意思是我没有数组,如果我用print_r打印它我没有输出。我也试过使用(string)$iden
我可以连接到数据库,所有信息都已经过检查(价值和存在)
其他信息
没有$iden
。
Array
(
[0] => Array
(
[0] => DB9
[1] => 200031
[2] => //localhost/css/images/objectimg/Aston Martin/db9.jpg
[3] => some html
[4] => DB9
[5] => 1
[6] => 0
)
[1] => Array
(
[0] => Rapid S
[1] => 200000
[2] => //localhost/css/images/objectimg/Aston Martin/vanq.jpg
[3] => some html
[4] => Rapid S
[5] => 1
[6] => 0
)
)
WITH $iden
Array
(
)
最后与$iden
:
Array
(
[0] => Array
(
[0] => DB9
[1] => 200031
[2] => //localhost/css/images/objectimg/Aston Martin/db9.jpg
[3] => some html
[4] => DB9
[5] => 1
[6] => 0
[7] => 42
)
[1] => Array
(
[0] => Rapid S
[1] => 200000
[2] => //localhost/css/images/objectimg/Aston Martin/vanq.jpg
[3] => some html
[4] => Rapid S
[5] => 1
[6] => 0
[7] => 45
)
)
答案 0 :(得分:1)
让我们直截了当,你不是通过在一行中创建多个变量来保存任何时间(或使代码更漂亮),一旦你抛出你的分号,它真的是按“输入”按钮的时候了。
为什么没有人愿意给你一个正确的答案是因为,你的代码真的很难阅读。
首先,您应该从更好地构建代码开始,这样的事情应该被认为是:
如此结合,这样的结构会更加结构化:
<?php
$postedBook = isset($_POST['book']) ? $_POST['book'] : false;
if($postedBook != false)
{
$Hostname = 'localhost';
$Username = 'root';
$Password = '';
$DatabaseName = 'dbName';
$book = normalize_str(htmlentities(preg_replace('/\s+/',' ', $postedBook), ENT_QUOTES,'UTF-8'));
$filecat = '../books/'.$book.'/'.$book.'.txt';
$catlist = file($filecat, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
$stmt = $mysqli->stmt_init();
$query = "SELECT * FROM PapierTable WHERE book = ? ORDER BY position ASC, title ASC;";
$prepared = $stmt->prepare($query);
if($prepared)
{
$stmt->bind_param('s', $book);
$stmt->execute();
$result = $stmt->bind_result($iden, $title, $price, $image, $description, $category, $enrat, $rating);
if($result)
{
$rit = array();
while($stmt->fetch())
{
$rit[] = array(
invert_str(html_entity_decode($title, ENT_QUOTES, 'UTF-8')),
$price,
invert_str(html_entity_decode($image, ENT_QUOTES, 'UTF-8')),
invert_str(html_entity_decode($description, ENT_QUOTES, 'UTF-8')),
invert_str(html_entity_decode($category, ENT_QUOTES, 'UTF-8')),
$enrat,
$rating
);
}
$stmt->close();
$count = count($catlist);
$ret = array();
for($i = 0; $i < $count ; $i++)
{
$ret[] = array_merge($ret, search($rit, 4, $catlist[$i]))
}
echo json_encode($ret);
}
else
{
file_put_contents('binderror.txt', $stmt->error);
}
}
else
{
file_put_contents('connecterror.txt',$stmt->error);
}
}
?>
我不确定这是否能解决您的问题,但如果没有,那么至少应该更容易找出问题所在。
发生的大多数错误都是非常常见和基本的错误,但是未结构化的代码使它们难以找到。