bind_结果中的数字将破坏数组php

bind_结果中的数字将破坏数组php,php,mysql,arrays,Php,Mysql,Arrays,这是我检索信息并发送回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(


这是我检索信息并发送回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

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
    )

)

让我们直截了当地说,在一行中创建多个变量并不会节省任何时间(或使代码更漂亮),一旦抛出分号,是时候按下“回车”按钮了

没有人费心给你一个正确答案的原因是,你的代码真的很难读

首先,您应该从更好地构建代码开始,也应该考虑以下内容:

  • 实际上,确保发布了某些内容,不要假设存在$u POST['book']
  • 请向我们展示您请求帮助时使用的所有变量和函数,向我们展示$Hostname、$Username、$Password、$DatabaseName的值实际上可能会有所帮助
  • $PaperTableName?只需在查询中定义它,或者将名称也绑定到查询中
  • 将查询构造为字符串,然后准备保存查询的字符串
  • 查询中缺少分号
  • 使用$rit[$i]是没有意义的,使用$rit[]=将数据附加到数组中也同样有效
  • 看看utf8_编码,我认为您的代码过于复杂了
  • $ret=array\u merge($ret,search($rit,4,$catlist[$i])正在覆盖您所做的一切,您必须执行$ret[]=array\u merge($ret,search($rit,4,$catlist[$i])
因此,结合起来,类似这样的东西将更加结构化:

<?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);
    }
}
?>

我不确定这是否能解决你的问题,但如果不能,那么你至少应该更容易找到问题所在


发生的大多数错误都是非常常见和基本的,但非结构化代码使它们很难找到。

您在
binderror.txt
中有任何输出吗?没有,我甚至没有看到目录中的文件……您是否显示了所有错误<代码>错误报告(E_全部);ini_集('display_errors','1')我使用的是xampp,但这是我的“头”ini_集('display_errors','1');ini_集('session.gc_maxlifest','604800');需要_一次('../admin/dbi.php');会话_start();好的,问题是我不能在数组开始时做任何事情,在其他位置它可以工作。谢谢你的帮助和建议,但问题仍然存在,我在主帖子中添加了一些数组输出示例。错误:我还必须更改搜索函数内的索引位置…search($rit,5,$catlist[$I]);
<?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);
    }
}
?>