0

Hey guys i am trying to display a list off all users in my database. not sure how to get along with it. Getting confused with mysqli and sql.. my dadtabase connection file is :

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

?>

I tired this on it but getting an error.

    $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>"
      ?>

I have also created a table to insert the data on them.. User Name . Email . lastName

not sure how to proceed. especially as i just got into php.. help will be much appreciaited to come up with a php handling file for me. and any further tips to delete or edit users...Thanks in Advance people..!!!!!!

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55

2 Answers2

1

You must use PDO functions:

$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'];
}
aidinMC
  • 1,415
  • 3
  • 18
  • 35
  • The idea of this post is that problem in OP's case is that the connection is being made using PDO, but the data is being fetched using mysql. You have to stick to one or the other! – Nigel Ren Jun 03 '17 at 07:53
0

Try this:

$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>"
VK321
  • 5,768
  • 3
  • 44
  • 47
  • I tried that it gives error..Fatal error: Uncaught Error: Call to undefined function query() in C:\xampp\htdocs\mypanel\all-users.php:18 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mypanel\all-users.php on line 18 which line 18 in my file is $sqldata = query($dbh, $sqlget) or die('error users'); – Michelle Dimwaite Jun 03 '17 at 08:33
  • I tried the updated. still shoots an error Notice: Undefined variable: db in C:\xampp\htdocs\mypanel\all-users.php on line 18 and Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\mypanel\all-users.php:18 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mypanel\all-users.php on line 18 . bare with me that line 18 is $sqldata = $db->query($dbh, $sqlget) or die('error users'); – Michelle Dimwaite Jun 03 '17 at 08:50
  • HI I changed db to dbh and get just an error this time which states ....Warning: PDO::query() expects parameter 1 to be string, object given in C:\xampp\htdocs\mypanel\all-users.php on line 18 error users @VK321: – Michelle Dimwaite Jun 03 '17 at 09:00
  • @MichelleDimwaite - this time it should work. all looks good to me – VK321 Jun 03 '17 at 09:02
  • i just did had a diferent error stating Warning: PDO::query(): SQLSTATE[00000]: No error: PDO constructor was not called in C:\xampp\htdocs\mypanel\all-users.php on line 12 error users line 12 still being the same $sqldata = $dbh->query($sqlget) or die('error users'); I just had to delete some tables .. – Michelle Dimwaite Jun 03 '17 at 09:05
  • @MichelleDimwaite - error is somewhere else can you update all code of users.php? – VK321 Jun 03 '17 at 09:07
  • @MichelleDimwaite and what is in maintemp.php? – VK321 Jun 03 '17 at 09:14
  • you have done not included file propery if you just want this to work try updated. – VK321 Jun 03 '17 at 09:19
  • the maintemp.php file is just a file in my include folder, just something like a master page i cant send it here coz its got plenty of things on it. mainly html and styleings for my top navigation bars and other divs.top bar menus, sidebar more than the limit for comments here. – Michelle Dimwaite Jun 03 '17 at 09:22
  • @MichelleDimwaite - no problem.. try update and see if you get any error. – VK321 Jun 03 '17 at 09:24
  • no luck, i s till get Uncaught Error: Call to a member function setAttribute() on null in C:\xampp\htdocs\mypanel\all-users.php:6 Stack trace: #0 {main} thrown does this had anything to do with my dbconnection.php file??? below – Michelle Dimwaite Jun 03 '17 at 09:39
  • I changed the line $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); to $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); and got close by only having ,my error users appearing.. after the die ie @VK321: – Michelle Dimwaite Jun 03 '17 at 09:44
  • Legend you are @VK321: it worked now. much appreciate it.. Glad for learning this from you. Hope I should be able to progress with my site. Much appreciated.. – Michelle Dimwaite Jun 03 '17 at 10:15