使用php查看mysql的结果

使用php查看mysql的结果,php,mysql,Php,Mysql,对以前的问题进行排序后,我意识到我浪费了大量的时间和精力,因为我没有正确地使用while循环,但我正在努力解决它们。这里有一段代码,我对类型_id=1,2,3,4运行了4次,然后将结果输出到$vehicle1,$vehicles2,等等。有没有办法运行它来检查有多少type_id,然后以如下相同的输出结果运行它们 $result = mysqli_query($con,"SELECT * FROM vehicles WHERE type_id=1 AND verified='$yes' ORDE

对以前的问题进行排序后,我意识到我浪费了大量的时间和精力,因为我没有正确地使用while循环,但我正在努力解决它们。这里有一段代码,我对类型_id=1,2,3,4运行了4次,然后将结果输出到$vehicle1,$vehicles2,等等。有没有办法运行它来检查有多少type_id,然后以如下相同的输出结果运行它们

$result = mysqli_query($con,"SELECT * FROM vehicles WHERE type_id=1 AND verified='$yes' ORDER BY description");
while($row = mysqli_fetch_array($result)) {
$description = $row['description'];
$description = strtoupper($description);

$id = $row['id'];
$count++;
$vehicles1 .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description'   , 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
}
$vehicles1 .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
$vehicles1 .="<div style='margin-bottom: 5px'></div>";
$result=mysqli_query($con,“从车辆中选择*,其中类型_id=1且已验证='$yes'按说明排序”);
while($row=mysqli\u fetch\u数组($result)){
$description=$row['description'];
$description=strtoupper($description);
$id=$row['id'];
$count++;
$vehicles1.=”;
}
$vehicles1.=“添加行政车辆”;
$vehicles1.=”;

也许类似的东西对你有用

        $my_types = [1,2,3,4];
        foreach($my_types as $type){
            $result = mysqli_query($con,"SELECT * FROM vehicles WHERE type_id=$type AND verified='$yes' ORDER BY description");
            while($row = mysqli_fetch_array($result)) {
                $description = $row['description'];
                $description = strtoupper($description);

                $id = $row['id'];
                $count++;
                $vehicles1 .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description'   , 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
            }
            $vehicles1 .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
            $vehicles1 .="<div style='margin-bottom: 5px'></div>";
        }

然后把结果放在一个数组中,并在其中循环

<?

    $vehicles_array = array();

    $types = array(1,2,3,4);
    foreach($types as $value) {
        $result = mysqli_query($con, "SELECT * FROM vehicles WHERE `type_id`='{$value}' AND `verified`='{$yes}' ORDER BY description");
        while ($row = mysqli_fetch_array($result)) {
            $description = strtoupper($row['description']);
            $id          = $row['id'];
            $vehicles1 .= <<<STRING
                <a href="" onclick="onClickAction('{$id}', '{$description}', '{$type_id}')" class="btn new2 dodgerbluemenu">{$description}</a>
STRING;
        }
        $vehicles1 .= " <div>
                            <button id='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button>
                        </div>";
        $vehicles1 .= "<div style='margin-bottom: 5px'></div>";
        array_push($vehicles_array, $vehicles1);
    }
?>

<script>
    function onClickAction(id, description, type_id) {
        $('#entered-details').show(); 
        $('#vehicle_id').val(id);
        $('#vehicle_list').val($('#vehicle_list').val()+id+":");
        vehicle_selected.innerHTML = description;
        elt.tagsinput('add', { 'value': id , 'text': description, 'type': type_id, 'type_id': '1'});
        $('#show_new').show();
    }
</script>

一个循环和一个查询!比避免循环更好的是避免查询

$result = mysqli_query( $con, "SELECT * FROM vehicles WHERE verified='$yes' ORDER BY type_id ASC, description;"); // remove type_id here and add order

if( mysqli_num_rows( $result ) > 0 ) // Evaluate always, the notified too amount the execution time, display_errror=On in DEVEPOLMENT ENVIRONMENT
{
    $str_vehicles = "";
    $current_type_id = -1;
    $count = 0;

    while( $row = mysqli_fetch_array( $result ) )
    {
        if( $current_type_id != $row['type_id'] )
        {
            if( $current_type_id > -1 )
            {
                $str_vehicles .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
                $str_vehicles .="<div style='margin-bottom: 5px'></div>";
            }
            $current_type_id = $row['type_id'];
        }
        $id = $row['id'];
        $description = strtoupper( $row['description'] );
        $count++;
        $str_vehicles .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description', 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
    }
}
$result=mysqli\u query($con,“从已验证的车辆中选择*=“$yes”按类型排序\u id ASC,description;”);//在此处删除类型_id并添加订单
如果(mysqli_num_rows($result)>0)//始终求值,则通知的值也会增加执行时间,在开发环境中显示\u error=On
{
$str_vehicles=“”;
$current_type_id=-1;
$count=0;
while($row=mysqli\u fetch\u数组($result))
{
if($current\u type\u id!=$row['type\u id']))
{
如果($current\u type\u id>-1)
{
$STRU车辆=“添加行政车辆”;
$STRU车辆=“”;
}
$current_type_id=$row['type_id'];
}
$id=$row['id'];
$description=strtoupper($row['description']);
$count++;
$STRU车辆=“”;
}
}

此代码在当前状态下是否有效?可以,但从您之前所做的工作来看,我认为我需要了解一些循环方面的知识。您已经在使用循环,但我现在正在查看代码,看看是否可以改进。我知道,但是对不同类型的id使用相同的代码4次。如果我添加了另一个类型的id,我将不得不再次添加代码,您的方法将加载所有类型的id?这就是我陷入困境的地方,最初的循环警告是:当使用
mysqli
时,您应该使用并将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将
$\u POST
$\u GET
或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。这是一个良好的开端!如果没有在循环中回显输出,则需要为
车辆使用数组。像
$vehicles[$i]
而不是
$vehicles1
谢谢,我会尝试,但循环时$vehicles1必须更改为$vehicles2,3,4等?哈哈,我在发送时收到了响应!谢谢,我会像@BizzyBob说的那样尝试,你可以把它们放在一个数组中<代码>$vehicles[$type]
。之后,您将能够使用
$vehicles[1]
获取类型1的车辆,使用
$vehicles[2]
获取类型2的车辆,等等,但它不会输出任何内容!
$result = mysqli_query( $con, "SELECT * FROM vehicles WHERE verified='$yes' ORDER BY type_id ASC, description;"); // remove type_id here and add order

if( mysqli_num_rows( $result ) > 0 ) // Evaluate always, the notified too amount the execution time, display_errror=On in DEVEPOLMENT ENVIRONMENT
{
    $str_vehicles = "";
    $current_type_id = -1;
    $count = 0;

    while( $row = mysqli_fetch_array( $result ) )
    {
        if( $current_type_id != $row['type_id'] )
        {
            if( $current_type_id > -1 )
            {
                $str_vehicles .="<div><button id ='add_exec_button' type='button' class='btn btn-add-vehicles btn-danger'>Add executive vehicle</button></div>";
                $str_vehicles .="<div style='margin-bottom: 5px'></div>";
            }
            $current_type_id = $row['type_id'];
        }
        $id = $row['id'];
        $description = strtoupper( $row['description'] );
        $count++;
        $str_vehicles .= "<a href=\"#entered-details\" onclick=\"$('#entered-details').show(); document.getElementById('vehicle_id').value='$id';document.getElementById('vehicle_list').value=''+document.getElementById('vehicle_list').value+'$id'+':';vehicle_selected.innerHTML = '$description';elt.tagsinput('add', { 'value': $id , 'text': '$description', 'type': '$type_id', 'type_id': '1'     });$('#show_new').show();\" class=\"btn new2 dodgerbluemenu\">$description</a>";
    }
}