PHP MySQLI 带通配符的面向对象
PHP MySQLI Object-Oriented with Wildcard
我正在转换为 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
}
?>
您错过了 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();
?>
请注意,您不能引用绑定参数。
首先,你没有声明$stmt
。
其次,?
在这种情况下不是通配符,它是一个参数占位符。您可以在准备查询时使用此类占位符,使用 $mysqli->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
比 }
更明确)
我正在转换为 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
}
?>
您错过了 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();
?>
请注意,您不能引用绑定参数。
首先,你没有声明$stmt
。
其次,?
在这种情况下不是通配符,它是一个参数占位符。您可以在准备查询时使用此类占位符,使用 $mysqli->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
比 }
更明确)