为您的第一个元素调用mysql_fetch_array
一次:
function i_iframe($cadena){
$x=@mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i=mysql_fetch_array($x)){
$option = adios($i['idioma']);
echo "<li>".$option."</li>";
}
}
@mysql_free_result($x);
}
现在格式化您的代码:
function i_iframe($cadena) {
$x = @mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($i = mysql_fetch_array($x)) {
$option = adios($i['idioma']);
echo '<li class="selected">', $option, '</li>';
while($i = mysql_fetch_array($x)) {
$option = adios($i['idioma']);
echo '<li>', $option, '</li>';
}
}
@mysql_free_result($x);
}
停止忽略错误并使用更清晰的名称:
function i_iframe($cadena) {
$query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li class="selected">', $option, '</li>';
while($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li>', $option, '</li>';
}
}
mysql_free_result($x);
}
在使用它们的地方转义你的输入,特别是如果你还没有:
function i_iframe($cadena) {
$cadena = mysql_real_escape_string($cadena);
$query = mysql_query("SELECT * FROM videos WHERE id_peli = '$cadena' LIMIT 3");
if($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li class="selected">', $option, '</li>';
while($item = mysql_fetch_array($query)) {
$option = adios($item['idioma']);
echo '<li>', $option, '</li>';
}
}
mysql_free_result($x);
}
现在停止使用已弃用的扩展程序并享受 PDO 的生活:
function i_iframe($cadena) {
global $db;
$query = $db->prepare('SELECT idioma FROM videos WHERE id_peli = :cadena LIMIT 3');
$query->execute([':cadena' => $cadena]);
$videos = $query->fetchAll(PDO::FETCH_OBJ);
$first = array_shift($videos);
echo '<li class="selected">', htmlspecialchars(adios($first->idioma)), '</li>';
foreach($videos as $video) {
echo '<li>', htmlspecialchars(adios($first->idioma)), '</li>';
}
}