How to do a var dump?

Discussion in 'PHP' started by co.ador, Nov 30, 2009.

  1. #1
    What are the formed queries that eventually get sent to the SQL server (do a var_dump() at some point...)?

    What can I use to do a var dump to page.php X?
     
    co.ador, Nov 30, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    unable to determine exact purpose, however I feel you want to log all the queries before executing them. If it is so, then either you can log them in mysql or in text file.

    You can do simple write file for dumping things in text file and for mysql logging you need to simply use insert query which is better to analyze after since you can get various reports easily.
     
    mastermunj, Nov 30, 2009 IP
  3. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    to avoid shooting in the dark I will give you some explanation and codes

    <?php
    $strSQL = '';
    $arrRestaurants = array();
    
    // place holder form data variables
    $strName = '';
    $strZipCode = '';
    $strState = '';
    $arrFoodTypes = array();
    $arrOfferings = array();
    
    // food types and offerings drop down build arrays
    $arrRestaurantsFoodTypes = array('values'=>array(),'output'=>array());
    $arrStates = array('','AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY',);
    $arrRestaurantsOfferings = array();
    
    $arrResult = mysql_query('SELECT restaurant_food_types_id,name FROM RESTAURANT_FOOD_TYPES');
    while($arrRow = mysql_fetch_assoc($arrResult)) {
        $arrRestaurantsFoodTypes['values'][] = $arrRow['restaurant_food_types_id'];
        $arrRestaurantsFoodTypes['output'][] = $arrRow['name'];
    }
    
    // reset form
    $boolReset = isset($_POST['frmSearch']) && isset($_POST['frmSearch']['reset'])?true:false;
    
    if($boolReset === true && isset($_POST['frmSearch'])) {
    
        // Extract POST variables and escape
        $strName = isset($_POST['frmSearch']['name'])?/*mysql_real_escape_string(*/$_POST['frmSearch']['name']/*)*/:'';
        $strZipCode = isset($_POST['frmSearch']['zipcode'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['zipcode']/*)*/:'';
        $strState = isset($_POST['frmSearch']['state'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['state']/*)*/:'';
        $arrFoodTypes = isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['food_types']:array();
        $arrOfferings = isset($_POST['frmSearch']['offerings'])?$_POST['frmSearch']['offerings']:array();
    
        // WHERE clause filters
        $arrSQLFilters = array();
        
        // whether or not zip codes table needs to be included
        $boolIncludeZipCodes = false;
    
    
        // Zipcode filter
        if(!empty($strZipCode)) {
            $boolIncludeZipCodes = true;
        
            $arrSQLFilters[] = sprintf(
                "z.zip LIKE '%s'"
                ,"%$strZipCode%"
            );
        }
        
        // State filter
        if(!empty($strState)) {
            $boolIncludeZipCodes = true;
        
            $arrSQLFilters[] = sprintf(
                "z.state = '%s'"
                ,$strState
            );
        }
    
        // Restaurants name filter
        if(!empty($strName)) {
            $arrSQLFilters[] = sprintf(
                "r.restaurantname LIKE '%s'"
                ,"%$strName%"
            );
        }
     // Food types filter
        if(!empty($arrFoodTypes) && !empty($arrFoodTypes[0])) {
        $arrSQLFilters[] = sprintf(
              'r.restaurants_id IN
                   (SELECT
                         DISTINCT restaurants_id
                      FROM
                         restaurants_restaurant_food_types
                     WHERE
                         restaurants_food_types_id IN (%s)
                     GROUP
                        BY
                         restaurants_id
                    HAVING 
                         COUNT(DISTINCT restaurants_id) = %u)'
                ,/*mysql_real_escape_string(*/ implode(',',$arrFoodTypes) /*)*/
                ,count($arrFoodTypes)
            );
        }
    
        // Offerings Filter ie. eat-in, lunch, dinner, etc
        if(!empty($arrOfferings)) {
          $arrSQLFilters[] = sprintf(
              'r.restaurants_id IN
                   (SELECT
                         DISTINCT restaurants_id
                      FROM
                         restaurants_to_restaurant_offerings
                     WHERE
                         restaurant_offerings_id IN (%s)
                     GROUP
                        BY
                         restaurants_id
                    HAVING 
                         COUNT(DISTINCT restaurants_id) = %u)'
                ,/*mysql_real_escape_string(*/ implode(',',$arrOfferings) /*)*/
                ,count($arrOfferings)
            );
        }
    
        // Build search query and embed filters
      $strSQL = sprintf(
        'SELECT
              r.restaurants_id
              ,r.restaurantname
              ,r.image
           FROM
              restaurants r
             %s
             %s
             %s'
         ,$boolIncludeZipCodes === true?'INNER JOIN zip_codes z ON r.restaurants_id = z.restaurants_id':''
         ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters)  
         ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':''
        );
        
         
        
        $arrResult = mysql_query($strSQL);
        while($arrRow = mysql_fetch_assoc($arrResult)) {
            $arrRestaurants[] = $arrRow;
        }
            
    }
    
    
    ?>
    
    PHP:
    So the query we have been trying to var_dump, echo etc is all the way at the bottom of the above script, Now you can see that the query trying to be var_dump has a variable $boolIncludeZipCodes and has to be === true? it means that the user has enter a zipcode to [HIGHLIGHT="SQL"]'INNER JOIN zip_codes z ON r.restaurants_id = z.restaurants_id':''?

    right?

    and again to [HIGHLIGHT="SQL"]GROUP BY r.restaurants_id':''
    );

    if that's true then I have enter a zip code but still is displaying the $strSQL variable as

    well then it means that the filters

     // WHERE clause filters
        $arrSQLFilters = array();
        
        // whether or not zip codes table needs to be included
        $boolIncludeZipCodes = false;
    
    
        // Zipcode filter
        if(!empty($strZipCode)) {
            $boolIncludeZipCodes = true;
        
            $arrSQLFilters[] = sprintf(
                "z.zip LIKE '%s'"
                ,"%$strZipCode%"
            );
        }
        
        // State filter
        if(!empty($strState)) {
            $boolIncludeZipCodes = true;
        
            $arrSQLFilters[] = sprintf(
                "z.state = '%s'"
                ,$strState
            );
        }
    
        // Restaurants name filter
        if(!empty($strName)) {
            $arrSQLFilters[] = sprintf(
                "r.restaurantname LIKE '%s'"
                ,"%$strName%"
            );
        }
    PHP:
    has some kind of set up which it's not letting $strSQL to pull up the data from the database.

    And I think it's the $boolIncludeZipCodes variable that won't let user to get the results if the $boolIncludeZipCodes variable is not exactly true? what is true in this sense?

    Take a look at the $boolResset variable set up at the script too that $boolReset, at the top of the script and it maybe not letting some value to pass.

    
    $boolReset = isset($_POST['frmSearch']) && isset($_POST['frmSearch']['reset'])?true:false;
    
    if($boolReset === true && isset($_POST['frmSearch'])) {
    
    PHP:
     
    co.ador, Dec 1, 2009 IP
  4. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    var_dump($variable);
     
    stOK, Dec 1, 2009 IP
  5. taminder

    taminder Peon

    Messages:
    581
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $array = blah blah;
    var_dump($array);
    Code (markup):
    http://php.net/manual/en/function.var-dump.php
    Code (markup):
     
    taminder, Dec 1, 2009 IP