【问题标题】:How to solve this issue of sql query to work perfectly?如何解决这个sql查询问题才能完美运行?
【发布时间】:2021-01-03 15:09:34
【问题描述】:

我有一个在 phpmyAdmin 上创建的数据库。 我有页面正在显示的问题

错误:无法执行 SELECT * FROM employees。

但我似乎把所有事情都做得很完美,不是吗?

所以,请看一下,这将是一个很大的帮助。

无法执行 SELECT * FROM employees。

这是我用来添加数据并在浏览器上显示数据的代码:

<?php
$dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "";
 $db = "HD";
 $pdo = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $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>

【问题讨论】:

  • 从 phpmyadmin 查询是否有效?
  • 另外,为什么要使用两种不同类型的 if() 子句——一种使用{},另一种使用:endif?这似乎让我感到不必要的困惑。
  • @droopsnoot ,是的,它正在为它工作
  • 尝试删除尽可能多的代码,同时仍然查询数据库。看看stackoverflow.com/help/minimal-reproducible-example
  • 如果适合您,请验证答案..

标签: php phpmyadmin


【解决方案1】:

使用以下作为您的 PDO 连接

<?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();
    }
?>

现在使用以下代码查询您的表。

<?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>';
?>

请您自己进行进一步的编码,但就部分而言,您在上面的代码中遇到的问题应该可以根据您的本地主机 PHPMyAdmin 数据库配置完美运行。 谢谢

【讨论】:

    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多