Skip to content
Advertisement

How to solve this issue of sql query to work perfectly?

I have a database that I have made on phpmyAdmin. I have and issue that Page is showing

ERROR: Could not able to execute SELECT * FROM employees.

But it seems that I have done everything perfectly, isn’t it?

So, please have a look at it and it will be a great help.

Could not be able to execute SELECT * FROM employees.

This is the code that I use to add in the data and display the data on the browser:

<?php
$dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "";
 $db = "HD";
 $pdo = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %sn". $conn -> error);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
    <style type="text/css">
        .wrapper{
            width: 650px;
            margin: 0 auto;
        }
        .page-header h2{
            margin-top: 0;
        }
        table tr td:last-child a{
            margin-right: 15px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function(){
            $('[data-toggle="tooltip"]').tooltip();   
        });
    </script>
</head>
<body>
    <div class="wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="page-header clearfix">
                        <h2 class="pull-left">Employees Details</h2>
                        <a href="create.php" class="btn btn-success pull-right">Add New Employee</a>
                    </div>
                    <?php
                    // Include config file
                    require_once "config.php";

                    // Attempt select query execution
                    $sql = "SELECT * FROM employees";
                    ?>
                    <?php if($result = $pdo->query($sql)) {
                        if($result->rowCount() > 0)  :?>
                        
                        <table class='table table-bordered table-striped'>
                        <thead>
                               <tr>
                               <th>#</th>
                                        <th>Name</th>
                                        <th>Address</th>
                                        <th>Salary</th>
                                        <th>Designation</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                               <?php while($row = $result->fetch()): ?>   
                                    <tr>
                                        <td> <?php echo $row['id'] ?> </td>
                                        <td> <?php echo $row['name'] ?> </td>
                                        <td> <?php echo $row['address'] ?> </td>
                                        <td> <?php echo $row['salary'] ?> </td>
                                        <td> <?php echo $row['designation'] ?> </td>
                                        <td>
                                            <a href='read.php?id= <?php echo $row['id'] ?>' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>
                                            <a href='update.php?id=<?php echo $row['id']?>' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>
                                            <a href='delete.php?id=<?php echo $row['id'] ?>' title='Delete Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>
                                        </td>
                                    </tr>
                                <?php endwhile ?>
                               </tbody>
                                </table>
                            <?php 
                            // Free result set
                            unset($result);
                            ?>
                        <?php else: ?>
                        
                           <p class='lead'><em>No records were found.</em></p>
                        <?php endif ?>
                        <?php
                    } else{
                        echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
                    }
                    
                    // Close connection
                    unset($pdo);
                    ?>
                </div>
            </div>        
        </div>
    </div>
</body>
</html>

Advertisement

Answer

Use the following as your PDO connection

<?php
    $srvr = "localhost:3308";
    $user = "root";
    $pass = "";
    $database = "HD";
    try{
        $cn = new PDO("mysql:host=$srvr; dbname=$database", $user, $pass);
        $cn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }catch(PDOException $ex){
        echo "Code Error: ". $ex->getMessage();
    }
?>

Now to query your table use the following codes.

<?php
     require_once("config.php");
     $stmt = $cn->query('SELECT * FROM employees');
     echo '<table border=1 id="mytbl"><tr>';
     echo '<th>#</th>
           <th>Name</th>
           <th>Address</th>
           <th>Salary</th>
           <th>Designation</th>
           <th>Action</th></tr>';
     while($row = $stmt->fetch()){
         echo '<tr><td>'. $row['id'].'</td>';
         echo '<td>'. $row['Name'].'</td>';
         echo '<td>'. $row['Address'].'</td>';
         echo '<td>'. $row['Salary'].'</td>';
         echo '<td>'. $row['Designation'].'</td>';
         echo '<td>'. $row['Action'].'</td></tr>';
     }
     echo '</table>';
?>

Please do further codding by yourself but for the part, you were having a problem above codes should work perfectly based on your localhost PHPMyAdmin database configuration. Thank you

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement