Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to write PHP code to select specific column and display rows respectfully??

    • 0
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 542
    Answer it

    I have wrote code for sqlite to select specific columns and display rows ..

    id   condition   lat   long
    1    Good        0.0  0.0
    2    Poor         0.0  0.0
    3    Good        0.0  0.0
    

    When I select Good, It should display my lat & long of id 1,3...so on

        public Cursor getSelectedLocations(String search){
         db = SQLiteHandler.getWritableDatabase();
         String[] selecteditems =                                   {SQLiteHandler.KEY_TLATITUDE,SQLiteHandler.KEY_TLONGITUDE,SQLiteHandler.KEY_TreeID};
          String where = SQLiteHandler.KEY_TCONDITION + "=?";
            String[] whereArgs = {search};
            Cursor cursor = db.query(SQLiteHandler.TABLE_TREE,selecteditems,where,whereArgs,null,null,null,null);
        return cursor;
    }
    

    Now I want to write in Mysqli PHP: Sample code of json..I want to change this code according to above concept

          <?php
    
         define('&#95;&#95;ROOT&#95;&#95;', dirname(dirname(&#95;&#95;FILE&#95;&#95;))); 
       require&#95;once(&#95;&#95;ROOT&#95;&#95;.'/public&#95;html/Config.php');
    
    
     // Connecting to mysql database
        $mysqli = new mysqli(DB&#95;HOST, DB&#95;USER, DB&#95;PASSWORD, DB&#95;DATABASE);
    
        // json response array
           $response = array("error" => FALSE);
    
    
    
    
       // get the tree details for google map marker
       if($stmt = $mysqli->query("SELECT * FROM tree")){
    
    
    
     if ($stmt->num&#95;rows) {
    
    
           while($tree = $stmt->fetch&#95;assoc()) {
    
            $response["error"] = FALSE;
    
            $response ["tree"] ["treeid"] = $tree['treeid'];
            $response ["tree"] ["treespecies"] = $tree['treespecies'];
            $response ["tree"] ["treelatitude"] = $tree['treelatitude'];
            $response ["tree"] ["treelongitude"] = $tree['treelongitude'];
    
           echo json&#95;encode($response),'<br>';
    
    
       } 
          }else {
             // user is not found with the credentials
        $response["error"] = TRUE;
        $response["error&#95;msg"] = "Tree list view credentials are wrong. Please try again!";
        echo json&#95;encode($response);
         }
         $mysqli->close();
    
     }
    

 3 Answer(s)

  • Hi Bharat,

    Use below code to get data from your database using mysqli and php:

    <?php
    // Create connection
    $mysqli = new mysqli(DB&#95;HOST, DB&#95;USER, DB&#95;PASSWORD, DB&#95;DATABASE);
    // Check connection
    if ($mysqli->connect&#95;error) {
        die("Connection failed: " . $mysqli->connect&#95;error);
    }
    
    $stmt = $mysqli->query("SELECT * FROM tree where `condition` = 'good'");
    
    if ($stmt->num&#95;rows > 0) {
        // output data of each row
        while($tree = $stmt->fetch&#95;assoc()) {
            $response["error"] = FALSE;
            $response ["tree"] ["treeid"] = $tree['id'];
            $response ["tree"] ["treespecies"] = $tree['condition'];
            $response ["tree"] ["treelatitude"] = $tree['lat'];
            $response ["tree"] ["treelongitude"] = $tree['long'];
            echo json&#95;encode($response),'<br>';
        }
    } else {
        $response["error"] = TRUE;
        $response["error&#95;msg"] = "Tree list view credentials are wrong. Please try again!";
        echo json&#95;encode($response);
    }
    $mysqli->close();
    
    ?>
    

    Make sure you use correct credentials for DB_HOST, DB_user, DB_PASSWORD and DB_DATABASE.

    I hope this will help.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: