【问题标题】:PHP MySQLI Object-Oriented with WildcardPHP MySQLI 面向对象的通配符
【发布时间】:2016-05-14 00:33:28
【问题描述】:

我正在转换为 Mysqli 面向对象(或尝试)。我有各种类别的页面。我想在包含中使用参数占位符'?',然后在类别页面上调用正确的类别。 这是我所得到的。如何在我的页面上指明类别?如果我指出WHERE category = apples,一切正常。

我在类别页面的顶部有这个包含

<?php require_once 'maincats_mysqli.php' ?>

下面是:

<?php 
$db = new mysqli('host', 'userName', '', 'dbName');
if ($db->connect_error) {
    $error = $db->connect_error;
} else { 
$sql = "SELECT pageName, gImage, prodName, prodPrice 
FROM tableName 
WHERE category = '?' 

ORDER BY dtList DESC";

$stmt->bind_param('s', ['$category']);

$result = $db->query($sql);
if ($db->error) {
    $error = $db->error;
    }
}
function getItem($result) {
return $result->fetch_assoc();
    }
?>

以下是一个类别页面的一部分。如何指明是哪个类别?任何帮助将不胜感激。

<?php 
if (isset($error)) {
echo "<p>$error</p>";
    }
?>

<?php 
while ($item = getItem($result)) { 
?>

<a href="http://www.example.com/<?php echo $item['pageName']; ?>">
<img src="http://www.example.com/<?php echo $item['gImage'];     ?>"</a>
<a href="http://www.example.com/<?php echo $item['pageName']; ?>">
<?php echo $item['prodName']; ?></a>
<?php echo $item['prodPrice']; ?>

<?php
   }
?>

【问题讨论】:

  • 1) 从... category = '?' ... 中删除单引号。 2)看到这个声明$stmt-&gt;bind_param('s', ['$category']);,应该是$stmt-&gt;bind_param('s', $category);。 3) 你没有执行你准备好的语句。 4)RTM,http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
  • 不要在变量上使用单引号。另见上面的注释^。它也被称为placeholder,而不是wildcard
  • 哦,我差点错过了,您甚至没有准备您的查询。

标签: php oop mysqli wildcard


【解决方案1】:

首先,你没有声明$stmt

其次,? 在这种情况下不是通配符,它​​是一个参数占位符。您可以在准备查询时使用此类占位符,例如$mysqli-&gt;prepare($sql)。见文档:http://php.net/manual/en/mysqli-stmt.bind-param.php

$sql = "SELECT pageName, gImage, prodName, prodPrice 
    FROM tableName 
    WHERE category = ?
    ORDER BY dtList DESC";

$stmt = $db->prepare($sql);

第三,您将变量封装在单引号中,因此它是一个带有美元和变量名称的字符串,而不是其内容。并且不能在数组中:

$stmt->bind_param('s', $category);

最后:$category 来自哪里?它没有在您向我们展示的脚本中定义。我猜是来自$_GET,所以上一行应该是:

$stmt->bind_param('s', $_GET['category']);

最后,您需要执行包含查询的语句:

$stmt->execute();

编辑

要获取结果,您不需要 getItem() 函数。只需将其删除。

$result = $stmt->get_result();

然后你可以遍历$result 并获取每一行:

while ($item = $result->fetch_assoc()):

// do you stuff

endwhile;

请注意,我在这里使用PHP control structure alternative syntax,这在您的情况下更清楚(endwhile} 更明确)

【讨论】:

    【解决方案2】:

    您缺少prepare()。看PHP manual page中的第一个例子:

    <?php
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $city = "Amersfoort";
    
    /* create a prepared statement */
    $stmt =  $mysqli->stmt_init();
    if ($stmt->prepare("SELECT District FROM City WHERE Name=?")) {
    
        /* bind parameters for markers */
        $stmt->bind_param("s", $city);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($district);
    
        /* fetch value */
        $stmt->fetch();
    
        printf("%s is in district %s\n", $city, $district);
    
        /* close statement */
        $stmt->close();
    }
    
    /* close connection */
    $mysqli->close();
    ?>
    

    请注意,您不能引用绑定参数。

    【讨论】:

    • 我很难接受这个。该信息已经在我的数据库表中。我只需要将我的电子商务网站从 MySQL 转换为 MySQLi。我曾经在每个产品页面和类别页面上都有“选择”来调用我需要的信息。以为我可以将 Select 放入 MySQLi 的包含中。我已经尝试了上述所有建议,但似乎没有任何效果。
    猜你喜欢
    • 2017-11-27
    • 2017-06-04
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2010-12-13
    相关资源
    最近更新 更多