使用mysql查询从php中获取数据库中的数据

时间:2013-05-30 10:11:27

标签: php javascript mysql

使用查询超过4个查询来获取数据库这四个查询运行正常但我再使用一个查询来获取hotel_id和这个hotel_id在不同的查询中使用但是当我运行查询时,查询运行正常但没有在while循环中得到输出,代码中的错误是什么......这个查询工作正常,但$ query1 =“从menu_master中选择hotel_id,其中menu_id =”。$ id;但在$ hid = $ row ['hotel_id'];不能在$ hid中存储任何值..

$query1 = "select hotel_id from menu_master where menu_id=".$id;
                    $res = mysql_query( $query1);
                    while($row=mysql_fetch_array($res))
                    {
                        $hid = $row['hotel_id'];
                    }

                    $query = "select set_rank from menu_master where menu_id = $row_id and hotel_id='".$_REQUEST['hotel_id']."'";
                    //echo $query."<br/>";
                    $result = mysql_query($query);

                    while($row=mysql_fetch_array($result))
                    {
                        $current_rank = $row['set_rank'];
                    }

                    $current_id = $row_id;
                    $new_rank =$_REQUEST['set_rank'];

                     $sql = "select * from menu_master where set_rank = '$new_rank ' and hotel_id='".$_REQUEST['hotel_id']."'" ;    
                     $rs = mysql_query($sql);

                    while($row = mysql_fetch_array($rs))
                         {  
                                $menu_id = $row['menu_id'];
                                $sql="update menu_master 
                                set set_rank=$current_rank where menu_id= $menu_id and hotel_id='".$_REQUEST['hotel_id']."'";

                                mysql_query($sql);

                         }

                                    $sql="update menu_master set 
                                    hotel_id           = '".mysql_real_escape_string($_REQUEST['hotel_id'])."',
                                    menu_name          = '".mysql_real_escape_string($_REQUEST['menu_name'])."',
                                    menu_name_ar       = '".mysql_real_escape_string($_REQUEST['menu_name_ar'])."',
                                    is_active          = '".$is_active."',
                                    set_rank=$new_rank where menu_id= '$current_id' and hotel_id='".$_REQUEST['hotel_id']."'";
                                    mysql_query($sql);
                }

2 个答案:

答案 0 :(得分:0)

试试此代码

$query1 = "select hotel_id from menu_master where menu_id=".$id;
$row = mysql_fetch_array(mysql_query($query1));
$hid = $row['hotel_id'];

答案 1 :(得分:0)

$ query1 =“从menu_master中选择hotel_id,其中menu_id =”。$ id;

                $res = mysql_query( $query1);

                while($row1=mysql_fetch_array($res))
                {


                    $hid = $row1['hotel_id'];

                $query = "select set_rank from menu_master where menu_id = $row_id and hotel_id='".$hid."'";
                //echo $query."<br/>";
                $result = mysql_query($query);

                while($row=mysql_fetch_array($result))
                {
                    $current_rank = $row['set_rank'];
                }

                $current_id = $row_id;
                $new_rank =$_REQUEST['set_rank'];

                 $sql = "select * from menu_master where set_rank = '$new_rank ' and hotel_id='".$hid."'" ;    
                 $rs = mysql_query($sql);

                while($row = mysql_fetch_array($rs))
                     {  
                            $menu_id = $row['menu_id'];
                            $sql="update menu_master 
                            set set_rank=$current_rank where menu_id= $menu_id and hotel_id='".$hid."'";

                            mysql_query($sql);

                     }

                                $sql="update menu_master set 
                                hotel_id           = '".mysql_real_escape_string($_REQUEST['hotel_id'])."',
                                menu_name          = '".mysql_real_escape_string($_REQUEST['menu_name'])."',
                                menu_name_ar       = '".mysql_real_escape_string($_REQUEST['menu_name_ar'])."',
                                is_active          = '".$is_active."',
                                set_rank=$new_rank where menu_id= '$current_id' and hotel_id='".$hid."'";
                                mysql_query($sql);
            }