准备好的声明不会使用php smarty返回任何值吗?

时间:2014-04-30 15:27:30

标签: php mysqli smarty

我在我的项目中使用smarty (php template engine)

然而,我一直在努力解决这么多问题,而且他们的文档或他们的支持并不是无法解决。

任何方式,我试图在我的PHP代码中使用mysqli prepared statement它只是不返回任何值,但如果我使用normal mysqli functions它工作得很好。

我不认为问题来自我的prepared statement,因为我在项目的其他部分使用它并且工作正常。所以我只能认为这个问题来自smarty

这有效:

$sql = "SELECT DISTINCT category FROM $storeShop";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
    while($line = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        $cvalue[] = $line;
    }

// Assign this array to smarty...

$smarty->assign('category', $cvalue);



// Assign this array to smarty...
$smarty->assign('$category', $cvalue);

这是我准备好的声明,不起作用:

$query = "SELECT DISTINCT category FROM $storeShop";

if ($stmt = mysqli_prepare($db_conx, $query)) {

    /* execute statement */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $category);

    /* fetch values */
    while ($line = (mysqli_stmt_fetch($stmt))) {
        $cvalue[] = $line;
    }

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($db_conx);

// Assign this array to smarty...

$smarty->assign('category', $cvalue);



// Assign this array to smarty...
$smarty->assign('$category', $cvalue);

这就是我在模板页面中的内容:

{section name=category loop=$category}
        <li class="odd"><a href="#">{$category[category].category}</a></li>
        {/section}

这个问题与smarty有什么共同之处,解决这个问题的解决方案是什么?

由于

修改

我们现在越来越近了。我使用以下代码,it will return the first letter of each category这很奇怪。示例:如果类别bookcar,则会返回bc

$query = "SELECT DISTINCT category FROM $storeShop";

if ($stmt = mysqli_prepare($db_conx, $query)) {

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $category);

        /* execute statement */
    mysqli_stmt_execute($stmt);

    /* fetch values */
    while (mysqli_stmt_fetch($stmt)) {
        $cvalue[] = $category;
    }

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($db_conx);

// Assign this array to smarty...

$smarty->assign('category', $cvalue);



// Assign this array to smarty...
$smarty->assign('$category', $cvalue);

我认为$linesmarty function,会返回whole value of the mysql table。 smarty是一种完全不同的语言......

第二次编辑:

我使用了var_dump,它会返回正确的类别,但在我的页面中,我只会得到每个类别的第一个字母。

var_dump($cvalue);将返回array(1) { [0]=> string(3) "GFD" } array(2) { [0]=> string(3) "book" [1]=> string(11) "car" } "

var_dump($category);将返回string(3) "book" string(11) "car"

1 个答案:

答案 0 :(得分:0)

/*
    execute & bind will be only work if $stmt is OKAY!
    Here you get an SQL error
*/
if ($stmt = mysqli_prepare($db_conx, $query)) {
    mysqli_stmt_bind_result($stmt, $category);
    /* ... */
} else {
    print "ERROR!!!!";
    var_dump($stmt);
}

我希望它有所帮助。这只是一个逻辑问题。 Bind将不会通话,但$stmt不是&#34; true&#34;或者给出一个&#34; false&#34;回来。

相关问题