gpt4 book ai didi

PHP 从查询结果中截断文本

转载 作者:行者123 更新时间:2023-11-29 00:11:02 26 4
gpt4 key购买 nike

我创建了一个准备好的语句来从 MySQL 数据库中获取一些结果,然后我想在同一页面上的多个框中显示这些结果。

我多次使用相同的准备语句来获取不同的结果集,然后使用一个片段将它们变成一个菜单。

出了什么问题:在某些选择框中,文本被随机截断。假设数据库中的文本显示“This is a long text”,我得到“This is a long t”。在另一个字段上,它只给出首字母“E”,而不是包含的 3 个单词。

如果我删除 $stmtPedibles->store_result(); , 文本将不再被截断。然而,如果我这样做,我就不能使用 <?php if ($executed_stmt->num_rows > 0) : ?>在选择框片段中,这对我很重要,因为我需要知道选择框是否为空。有解决方法吗?

代码:

1。主文档

   <?php 
$root = "";
$thisfile = "index.php";
require_once($root."includes/setup/user.php");
require($root."includes/snippets/prepared_queries.php");
require_once($root."includes/layout/doc_head.php");
$thisGerencia = "Produccion"; //Ventas, Almacen, Produccion

//Variables que se repiten en toda la página
$ped_gerencia = $thisGerencia;
$ped_usuario = $thisUsr;
?>
<body>
<h2>Elementos disponibles</h2>
<h3><?php echo strtoupper($thisGerencia);?> - Diálogos disponibles: </h3>
<p>Prep statement + build function</p>

<?php //PEDIDO 1
$ped_tipo = "Q";
$stmtPedibles->execute(); //4. Execute
$stmtPedibles->bind_result($selText, $selValue, $nada1, $nada2); //5. Bind results (for selects, always include $selValue and $selCodigo)

//Vars para select:
$executed_stmt = $stmtPedibles; //Esto hace que el siguiente snippet funcione.
$select_name = "dialogos_pedibles";
$select_required = TRUE;
$select_selected = FALSE;
$default_option_text = "Hacer una pregunta...";

include($root."includes/snippets/select_box.php");

?>
<?php //PEDIDO 2

$ped_tipo = "I";
$stmtPedibles->execute(); //4. Execute
$stmtPedibles->bind_result($selText, $selValue, $nada1, $nada2); //5. Bind results (for selects, always include $selValue and $selCodigo)

//Vars para select:
$executed_stmt = $stmtPedibles; //Esto hace que el siguiente snippet funcione.
$select_name = "dialogos_pedibles";
//$select_required = TRUE;
$select_selected = FALSE;
$default_option_text = "Hacer una pregunta...";

include($root."includes/snippets/select_box.php");

?>
<h3><?php echo strtoupper($thisGerencia);?> - Informes disponibles: </h3>
<?php //PEDIDO 3
$ped_tipo = "P";
$stmtPedibles->execute(); //4. Execute
$stmtPedibles->bind_result($selText, $selValue, $nada1, $nada2); //5. Bind results (for selects, always include $selValue and $selCodigo)

//Vars para select:
$executed_stmt = $stmtPedibles; //Esto hace que el siguiente snippet funcione.
$select_name = "informes_pedibles";
//$select_required = TRUE;
$select_selected = FALSE;
$default_option_text = "Solicitar un informe...";
include($root."includes/snippets/select_box.php");

$executed_stmt->close();
$stmtPedibles->close();
?>
<?php include($root."includes/setup/user_footer.php");?>
</body>
</html>

2。准备好的声明

<?php 
$prep_elementos_pedibles = "SELECT EvePar_Pregunta, EvePar_EventoCodigo, EvePar_EventoNombre, EvePar_CodigoInforme
FROM Estado_Jugador JOIN Eventos_Parametros ON Estado_Codigo = EvePar_EventoCodigo
WHERE EvePar_EventoLugar = ? AND Estado_Habilitado = 1 AND Evento_EventoTipo = ? AND Estado_usr_name = ?";

$stmtPedibles = $connection->prepare($prep_elementos_pedibles); //1. Prepare
if (!$stmtPedibles) { //2. Check for errors
trigger_error('Wrong SQL: ' . $prep_elementos_pedibles . ' Error: ' . $conn->error, E_USER_ERROR);
}
$stmtPedibles->bind_param("sss", $ped_gerencia, $ped_tipo, $ped_usuario); //3. Bind
?>

3。选择框代码段

    <?php  
//THIS WAY; TEXT GETS CUT OFF
$executed_stmt->store_result();
if ($executed_stmt->num_rows > 0) : ?>
<select name="<?php echo $select_name;?>" class="<?php if ($select_required) {echo "required";} ?>">
<?php if ($default_option_text !="") : ?>
<option value=""><?php echo $default_option_text;?></option>
<?php endif;?>
<?php while ($executed_stmt->fetch()) :
if ($select_selected == $selValue) {
$selected = " selected='selected' ";
} else {
$selected = "";
}
?>
<option value="<?php echo $selValue;?>" <?php echo $selected;?> ><?php echo $selText;?></option>
<?php endwhile;?>
</select>
<?php endif;
$executed_stmt->free_result();
?>

编辑 1:我刚刚清理了代码,因为 bad_boy 提到我不应该创建一些变量。我搬了store_resultfree_result到选择框片段,希望它们会有所作为,但事实并非如此。

我还附上了表结构的屏幕截图: This is EventoParametros, where the cut off text comes from This is Estado_Jugador, used in the JOIN

我应该放弃使用 num_rows 吗?那将意味着我真的放弃了准备好的语句...如果我不使用准备好的语句,MySQLi 是否比 MySQL 语句更安全?真是令人沮丧... :-(

最佳答案

我真的不知道它为什么要进行随机文本切断,但如果您可以稍微重新组织选择框的构建方式,那么您似乎可以消除检查 num_rows 的需要。我想如果你制作一个包含所有 <option> 的字符串s,然后做你的while ... fetch...创建选项列表的东西,然后你可以构建其余的<select>如果它不是空的,则在它周围,或者如果它是,则不要。像这样:

<?php
$options = "";
while ($executed_stmt->fetch()) {
if ($select_selected == $selValue) {
$selected = " selected='selected' ";
} else {
$selected = "";
}
$options .= "<option value=\"$selValue\"$selected>$selText</option>";
}
?>
<?php if ($options) : ?>
<select name="<?php echo $select_name;?>" class="<?php if ($select_required) {echo "required";} ?>">
<?php if ($default_option_text !="") : ?>
<option value=""><?php echo $default_option_text;?></option>
<?php endif;?>
<?php echo $options;?>
</select>
<?php endif; ?>

关于PHP 从查询结果中截断文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25047563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com