Affichage des articles dont le libellé est php Search Data In MySQL Database By Id. Afficher tous les articles
Affichage des articles dont le libellé est php Search Data In MySQL Database By Id. Afficher tous les articles

PHP Code How To Search Data In MySQL Database By Id Using PDO

Php Code To Search Data In Mysql Database Table And Dispaly Results In Inputs Using PDO .


________________________________________________________

In this Php Tutorial we will Learn How To Find Data In MySQL Database Table By Id And Show The Results In Form Inputs In Php  using  PDO .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:


<?php

// php search data in mysql database using PDO
// set data in input text

$id = "";
$fname = "";
$lname = "";
$age = "";

if(isset($_POST['Find']))
{
        // connect to mysql
    try {
        $pdoConnect = new PDO("mysql:host=localhost;dbname=test_db","root","");
    } catch (PDOException $exc) {
        echo $exc->getMessage();
        exit();
    }
    
    // id to search
    $id = $_POST['id'];
    
     // mysql search query
    $pdoQuery = "SELECT * FROM users WHERE id = :id";
    
    $pdoResult = $pdoConnect->prepare($pdoQuery);
    
    //set your id to the query id
    $pdoExec = $pdoResult->execute(array(":id"=>$id));
    
    if($pdoExec)
    {
            // if id exist 
            // show data in inputs
        if($pdoResult->rowCount()>0)
        {
            foreach($pdoResult as $row)
            {
                $id = $row['id'];
                $fname = $row['fname'];
                $lname = $row['lname'];
                $age = $row['age'];
            }
        }
            // if the id not exist
            // show a message and clear inputs
        else{
            echo 'No Data With This ID';
        }
    }else{
        echo 'ERROR Data Not Inserted';
    }
}


?>

<!DOCTYPE html>

<html>

    <head>

        <title> PHP SEARCH DATA USING PDO </title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>

    <body>

        <form action="php_search_in_mysql_database_using_pdo.php" method="post">

            ID To Search : <input type="text" name="id" value="<?php echo $id;?>"><br><br>

            First Name : <input type="text" name="fname" value="<?php echo $fname;?>"><br><br>

            Last Name : <input type="text" name="lname" value="<?php echo $lname;?>"><br><br>

            Age : <input type="text" name="age" value="<?php echo $age;?>"><br><br>

            <input type="submit" name="Find" value="Find Data">

        </form>
    
    </body>

</html>



///////////////OUTPUT:



                     
php search mysql pdo
Php Search




PHP Code How To Search Data In MySQL Database By Id Using MySQLI

Php Code To Search Data In Mysql Database Table And Dispaly Results In Inputs Using MySQLI .


________________________________________________________

In this Php Tutorial we will Learn How To Find Data In MySQL Database Table By Id And Show The Results In Form Inputs In Php  using  MySQLI .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 




Php Source Code:


<?php

// php code to search data in mysql database and set it in input text

if(isset($_POST['search']))
{
    // id to search
    $id = $_POST['id'];
    
    // connect to mysql
    $connect = mysqli_connect("localhost", "root", "","test_db");
    
    // mysql search query
    $query = "SELECT `fname`, `lname`, `age` FROM `users` WHERE `id` = $id LIMIT 1";
    
    $result = mysqli_query($connect, $query);
    
    // if id exist 
    // show data in inputs
    if(mysqli_num_rows($result) > 0)
    {
      while ($row = mysqli_fetch_array($result))
      {
        $fname = $row['fname'];
        $lname = $row['lname'];
        $age = $row['age'];
      }  
    }
    
    // if the id not exist
    // show a message and clear inputs
    else {
        echo "Undifined ID";
            $fname = "";
            $lname = "";
            $age = "";
    }
    
    
    mysqli_free_result($result);
    mysqli_close($connect);
    
}

// in the first time inputs are empty
else{
    $fname = "";
    $lname = "";
    $age = "";
}


?>

<!DOCTYPE html>

<html>

    <head>

        <title> PHP FIND DATA </title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>

    <body>

    <form action="php_search_in_mysql_database.php" method="post">

        Id:<input type="text" name="id"><br><br>

        First Name:<input type="text" name="fname" value="<?php echo $fname;?>"><br>
<br>

        Last Name:<input type="text" name="lname" value="<?php echo $lname;?>"><br><br>

        Age:<input type="text" name="age" value="<?php echo $age;?>"><br><br>

        <input type="submit" name="search" value="Find">

           </form>

    </body>

</html>


///////////////OUTPUT:



                     
php mysql search mysqli
Php Search