显示我的数据库中的用户列表

时间:2017-06-03 07:42:55

标签: php sql mysqli

嘿伙计们,我试图在我的数据库中显示所有用户的列表。不知道如何相处。与mysqli和sql混淆.. 我的dadtabase连接文件是:

    class Dbconnect extends PDO
    {

        private $dbengine   = 'mysql';
        private $dbhost     = 'localhost';
        private $dbuser     = 'root';
        private $dbpassword = '';
        private $dbname     = 'test';


        public $dbh = null;

        function __construct()
        {
            try{


                $this->dbh = new PDO("".$this->dbengine.":host=$this->dbhost; dbname=$this->dbname", $this->dbuser, $this->dbpassword);


                $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

            }
            catch (PDOException $e){

                $e->getMessage();
            }
        }
      }

>

我对此感到厌倦但却出错了。

    $sqlget = "SELECT * FROM user";
    $sqldata = mysqli_query($dbh, $sqlget) or die('error users');
    echo "<table>"; 
    echo " <th>User Name </th> <th>Email</th> <th> Some Name</th>";

     while($row = mysql_fetch_array($sqldata, MYSQL_ASSOC)) {

      echo "<tr><td>";
      echo $row['name'];
      echo "</td><td>";
      echo $row['email'];
       echo "</td><td>";
      echo $row['some'];
       echo "</td>";
                    }

     echo "</table>"
      ?>

我还创建了一个表来插入数据。         用户名 。电邮。 lastName的

不确定如何继续。特别是当我刚进入php ..帮助将非常感谢为我提出一个PHP处理文件。 以及删除或编辑用户的任何进一步提示...在预先感谢人们.. !!!!!!

2 个答案:

答案 0 :(得分:1)

您必须使用PDO功能:

$db = new PDO("".$dbengine.":host=$dbhost; dbname=$dbname", $dbuser, $dbpassword);
$q = "SELECT * FROM users";
$result = $db->query($q)->fetchall();
foreach ($result as $user) {
    echo $user['name'];
}

答案 1 :(得分:0)

试试这个:

$dbh = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

$sqlget = "SELECT * FROM user";
$sqldata = $dbh->query($sqlget) or die('error users');
echo "<table>"; 
echo " <th>User Name </th> <th>Email</th> <th> Some Name</th>";

while($row = $sqldata->fetch(PDO::FETCH_ASSOC)) {
  echo "<tr><td>";
  echo $row['name'];
  echo "</td><td>";
  echo $row['email'];
  echo "</td><td>";
  echo $row['some'];
  echo "</td>";
}

echo "</table>"