DEV Community

Robert Look
Robert Look

Posted on

How To Fetch And Update Query In MySQL - PHP

we learn to update query in MySQL. many time need to Fetch and update the data from the MySQL database in PHP. This example demonstrates to you how you can easily fetch data and update query in SQL server, the data from the MySQL database in PHP.

Here, we will show you a simple and easy way on how to fetch and update query in MySQL database in PHP.

How To Fetch And Update Query In MySQL - PHP

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <title>PHP code to retrieve and update data from MySQL database - phpcodingstuff.com</title>
      <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Varela+Round|Open+Sans">
      <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
      <style type="text/css">
         .bs-example{
         margin: 20px;
         }
      </style>
      <script type="text/javascript">
         $(document).ready(function(){
         $('[data-toggle="tooltip"]').tooltip();   
         });
      </script>
   </head>
   <body>
      <div class="bs-example">
         <div class="container">
          <div class="row">PHP code to retrieve and update data from MySQL database - phpcodingstuff.com</div>
            <div class="row">
               <div class="col-md-12">
                  <div class="page-header clearfix">
                     <h2 class="pull-left">Users List</h2>
                  </div>
                  <?php
                     include_once 'db.php';
                     $result = mysqli_query($conn,"SELECT * FROM users");
                     ?>
                  <?php
                     if (mysqli_num_rows($result) > 0) {
                     ?>
                  <table class='table table-bordered table-striped'>
                     <tr>
                        <td>Name</td>
                        <td>Email id</td>
                        <td>Mobile</td>
                        <td>Action</td>
                     </tr>
                     <?php
                        $i=0;
                        while($row = mysqli_fetch_array($result)) {
                        ?>
                     <tr>
                        <td><?php echo $row["name"]; ?></td>
                        <td><?php echo $row["email"]; ?></td>
                        <td><?php echo $row["mobile"]; ?></td>
                        <td><a href="update-action.php?userid=<?php echo $row["id"]; ?>">Update</a></td>
                     </tr>
                     <?php
                        $i++;
                        }
                        ?>
                  </table>
                  <?php
                     }
                     else{
                     echo "No result found";
                     }
                     ?>
               </div>
            </div>
         </div>
      </div>
   </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Read More: https://www.phpcodingstuff.com/blog/how-to-fetch-and-update-query-in-mysql-php.html

Top comments (2)

Collapse
 
marcusatlocalhost profile image
Marcus

Nobody should follow this tutorial by Robert Look or Ajay kumar (who is the author of the liked article).

It's insecure and an open door for taking over your database.

$result = mysqli_query($conn,"SELECT * FROM users WHERE id='" . $_GET['userid'] . "'");
Enter fullscreen mode Exit fullscreen mode

I'm sorry but this low quality content is very discouraging using this site.

Nothing against beginner tutorials, but this is code that was already wrong 10 years ago!

Collapse
 
_garybell profile image
Gary Bell

Yes, bad practices. But it also only gives the code without explaining what it does. It might as well just say "copy and paste this, then hack it to fit your needs. Good luck"