mysql 准备好的 Select 语句连接不工作
mysql prepared Select statement join not working
所以我准备好的语句有效,但我不知道如何告诉 php 加入两列。
我尝试了一个未经准备的陈述并且有效。我尝试向 bind_results.
添加更多变量
$stmt = mysqli_prepare($con, "Select tblcategory.CategoryName as catname,tblcategory.id as catid, tblwritter.Writter as writtername, tblwritter.writterdescription as writterdescription, tblwritter.PostingDate as writterpostingdate, tblwritter.UpdationDate as writterupdationdate, tblwritter.WritterId as writterid from tblwritter join tblcategory on tblwritter.CategoryId=tblcategory.id where tblwritter.Is_Active=1 and WritterId=?");
$stmt->bind_param("i", $writterid);
$writterid=intval($_GET['scid']);
$stmt->execute();
$stmt->bind_result($catname,$catid,$writtername,$writterdescription,$writterpostingdate,$writterupdationdate,$writterid);
$stmt->fetch();
<div class="form-group">
<label class="col-md-2 control-label">Category</label>
<div class="col-md-10">
<select class="form-control" name="category" required>
<option value="<?php echo $catid;?>"><?php echo $catname;?></option>
<?php
$stmt = $con -> prepare('select id,CategoryName from tblcategory where Is_Active=?');
$stmt -> bind_param('i', $Is_Active);
$Is_Active = 1;
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($id, $CategoryName);
$stmt -> fetch();
// Feching active categories
$ret=mysqli_query($con,"select id,CategoryName from tblcategory where Is_Active=1");
while($result=mysqli_fetch_array($ret))
{
?>
<option value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?></option>
<?php } ?>
</select>
</div>
</div>
预期的结果是类别应该下拉以编辑类别。
您的问题不明确,因为您未能准确找出您的代码失败的地方,也没有告诉我们您收到的错误是什么。 (在这种情况发生变化之前,我无法对您的问题投赞成票)
虽然你的问题很清楚想要达到的效果。我编写了一个未经测试的脚本,我相信 应该可以按要求工作。
<?php
$config = ['localhost', 'root', '', 'dbname'];
if (!$con = new mysqli(...$config)) {
die("MySQL Connection Error: <b>Check config values</b>"); // $con->connect_error
}
$query = "SELECT cat.CategoryName, cat.id,
writ.Writter, writ.writterdescription,
writ.PostingDate, writ.UpdationDate, writ.WritterId
FROM tblwritter AS writ
JOIN tblcategory AS cat ON writ.CategoryId = cat.id
WHERE writ.Is_Active = 1
AND writ.WritterId = ?"
if (!$stmt = $con->prepare($query)) {
echo "Prepare Error: "; // $con->error
} elseif (!$stmt->bind_param("i", $_GET['scid']) ||
!$stmt->execute() ||
!$stmt->bind_result($catname,
$catid,
$writtername,
$writterdescription,
$writterpostingdate,
$writterupdationdate,
$writterid)) {
echo "Statement Error: "; // $stmt->error
} elseif (!$stmt->fetch()) {
echo "[No qualifying row found]";
}
$stmt->close();
$catid = $catid ?? 0;
$query = "SELECT id, CategoryName
FROM tblcategory
WHERE Is_Active = 1
AND id != ?";
if (!$stmt = $con->prepare($query)) {
echo "Prepare Error: "; // $con->error
} elseif (!$stmt->bind_param("i", $catid) ||
!$stmt->execute() ||
!$stmt->bind_result($id, $name)) {
echo "Statement Error: "; // $stmt->error
} else { ?>
<div class="form-group">
<label class="col-md-2 control-label">Category</label>
<div class="col-md-10">
<select class="form-control" name="category" required>
<?php
if ($catid) {
echo '<option value=' , $catid , '>' , htmlentities($catname) , '</option>';
}
while ($stmt->fetch()) {
echo '<option value=' , $id , '>' , htmlentities($name) , '</option>';
}
?>
</select>
</div>
</div> <?php
}
- 维护面向对象的 mysqli 语法而不是混合。 OO 是我的推荐,因为它比程序更简洁。
- 我已经标记了我的代码片段以使其全部适合 Stack Overflow 代码片段框而无需水平滚动。
- 在查询中编写 MySQL 关键字时使用全部大写 - 这将提高可读性。
- 使用简短的 table 别名(例如 "cat" 和 "writ",这样您的查询就不会因为字符过多而臃肿
- 我在流程中包含了一堆检查点,以帮助您诊断可能出现的任何问题。出于安全考虑,切勿向最终用户显示原始错误消息。
- 我没有检查
$_GET['scid']
是否存在,我假设你在其他地方这样做。如果出于某种原因,提交的 scid 值没有 return 任何符合条件的行,则将其默认为零。这将使下一个查询始终如一地工作。
- 我更改了您的第二个查询,因此它不会在 select 字段中第二次提供 scid 值。
- 如果在数据库中找到 $catid,它将作为第一个选项显示在 select 字段中。
- 只要数据库列是 INT 数据类型(并且应该是),选项标记中作为
value
属性写入的整数值不需要 htmlentities()
。我还删除了整数上的双引号,因为它们不是必需的。
所以我准备好的语句有效,但我不知道如何告诉 php 加入两列。
我尝试了一个未经准备的陈述并且有效。我尝试向 bind_results.
添加更多变量$stmt = mysqli_prepare($con, "Select tblcategory.CategoryName as catname,tblcategory.id as catid, tblwritter.Writter as writtername, tblwritter.writterdescription as writterdescription, tblwritter.PostingDate as writterpostingdate, tblwritter.UpdationDate as writterupdationdate, tblwritter.WritterId as writterid from tblwritter join tblcategory on tblwritter.CategoryId=tblcategory.id where tblwritter.Is_Active=1 and WritterId=?");
$stmt->bind_param("i", $writterid);
$writterid=intval($_GET['scid']);
$stmt->execute();
$stmt->bind_result($catname,$catid,$writtername,$writterdescription,$writterpostingdate,$writterupdationdate,$writterid);
$stmt->fetch();
<div class="form-group">
<label class="col-md-2 control-label">Category</label>
<div class="col-md-10">
<select class="form-control" name="category" required>
<option value="<?php echo $catid;?>"><?php echo $catname;?></option>
<?php
$stmt = $con -> prepare('select id,CategoryName from tblcategory where Is_Active=?');
$stmt -> bind_param('i', $Is_Active);
$Is_Active = 1;
$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($id, $CategoryName);
$stmt -> fetch();
// Feching active categories
$ret=mysqli_query($con,"select id,CategoryName from tblcategory where Is_Active=1");
while($result=mysqli_fetch_array($ret))
{
?>
<option value="<?php echo htmlentities($result['id']);?>"><?php echo htmlentities($result['CategoryName']);?></option>
<?php } ?>
</select>
</div>
</div>
预期的结果是类别应该下拉以编辑类别。
您的问题不明确,因为您未能准确找出您的代码失败的地方,也没有告诉我们您收到的错误是什么。 (在这种情况发生变化之前,我无法对您的问题投赞成票)
虽然你的问题很清楚想要达到的效果。我编写了一个未经测试的脚本,我相信 应该可以按要求工作。
<?php
$config = ['localhost', 'root', '', 'dbname'];
if (!$con = new mysqli(...$config)) {
die("MySQL Connection Error: <b>Check config values</b>"); // $con->connect_error
}
$query = "SELECT cat.CategoryName, cat.id,
writ.Writter, writ.writterdescription,
writ.PostingDate, writ.UpdationDate, writ.WritterId
FROM tblwritter AS writ
JOIN tblcategory AS cat ON writ.CategoryId = cat.id
WHERE writ.Is_Active = 1
AND writ.WritterId = ?"
if (!$stmt = $con->prepare($query)) {
echo "Prepare Error: "; // $con->error
} elseif (!$stmt->bind_param("i", $_GET['scid']) ||
!$stmt->execute() ||
!$stmt->bind_result($catname,
$catid,
$writtername,
$writterdescription,
$writterpostingdate,
$writterupdationdate,
$writterid)) {
echo "Statement Error: "; // $stmt->error
} elseif (!$stmt->fetch()) {
echo "[No qualifying row found]";
}
$stmt->close();
$catid = $catid ?? 0;
$query = "SELECT id, CategoryName
FROM tblcategory
WHERE Is_Active = 1
AND id != ?";
if (!$stmt = $con->prepare($query)) {
echo "Prepare Error: "; // $con->error
} elseif (!$stmt->bind_param("i", $catid) ||
!$stmt->execute() ||
!$stmt->bind_result($id, $name)) {
echo "Statement Error: "; // $stmt->error
} else { ?>
<div class="form-group">
<label class="col-md-2 control-label">Category</label>
<div class="col-md-10">
<select class="form-control" name="category" required>
<?php
if ($catid) {
echo '<option value=' , $catid , '>' , htmlentities($catname) , '</option>';
}
while ($stmt->fetch()) {
echo '<option value=' , $id , '>' , htmlentities($name) , '</option>';
}
?>
</select>
</div>
</div> <?php
}
- 维护面向对象的 mysqli 语法而不是混合。 OO 是我的推荐,因为它比程序更简洁。
- 我已经标记了我的代码片段以使其全部适合 Stack Overflow 代码片段框而无需水平滚动。
- 在查询中编写 MySQL 关键字时使用全部大写 - 这将提高可读性。
- 使用简短的 table 别名(例如 "cat" 和 "writ",这样您的查询就不会因为字符过多而臃肿
- 我在流程中包含了一堆检查点,以帮助您诊断可能出现的任何问题。出于安全考虑,切勿向最终用户显示原始错误消息。
- 我没有检查
$_GET['scid']
是否存在,我假设你在其他地方这样做。如果出于某种原因,提交的 scid 值没有 return 任何符合条件的行,则将其默认为零。这将使下一个查询始终如一地工作。 - 我更改了您的第二个查询,因此它不会在 select 字段中第二次提供 scid 值。
- 如果在数据库中找到 $catid,它将作为第一个选项显示在 select 字段中。
- 只要数据库列是 INT 数据类型(并且应该是),选项标记中作为
value
属性写入的整数值不需要htmlentities()
。我还删除了整数上的双引号,因为它们不是必需的。