Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to fetch data from database in php ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 152
    Comment on it

    Welcome to findnerd, today we are going to discuss fetch data from database in php .
    In this blog you can fetch data from database .

    Firstly establish a database connection

    Use the mysqli( ) function to established connection to the MySQL server.

    <?php
    $servername = "localhost";      // Here write the servername  or an IP address
    $username = "abc";              // Here write the username of your phpmyadmin
    $password = "123456";           // Here write the password of your phpmyadmin password
    $databasename = "databasename";  // Here write the database name of your database
    
    // Create a connection from your databse
    $Getconnection = new mysqli($servername, $username, $password, $databasename);
    
    // Check connection
    if ($Getconnection->connect&#95;error) {
    
        die("Connection not successful: " . $Getconnection->connect&#95;error);
    } 
    
    // Get data from database in city table
    $GetData = "SELECT id, citycode, cityname FROM city";
    
    $result = $Getconnection->query($GetData);
    
    if ($result->num&#95;rows > 0) {
    
        // Get output data of each row
        while($row = $result->fetch&#95;assoc()) {
            echo "id: " . $row["id"]. " - citycode: " . $row["citycode"]. " -cityname: " . $row["cityname"]. "<br>";
        }
    } else {
        echo "empty data";
    }
    $Getconnection->close();
    ?>
    

 0 Comment(s)

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: