How to echo database fields with PHP?

Nemanja

Premium Member
Joined
Nov 23, 2012
Messages
293
Points
28
So, I'm learning basics of PHP and Mysql and I am trying to print my database data to a PHP page. I have a table username, email and URL fields. I'm trying to select this table and echo field values but the result returned is a blank page. What does this mean?
Could you please post some sample of showing data from a database by echo them using PHP?

Thanks in advance.
 

Ruesnam

Member
Joined
Mar 6, 2017
Messages
59
Points
8
If I got your point, then let me give you an example.
Let's say you have a table called 'user', and you want to print a field called 'username'

PHP:
<?php
     try{
        $database = new PDO($host, $user, $password);//database credentials
    }
   catch(Exception $e){
        die('Failure : '.$e->getMessage());
   }
   $request = $database->prepare('SELECT * FROM user ');
   $request->execute();
   $result = $request->fetch();
   if($result){
     echo $result['username']; 
  }
  
?>
Hope it helped :)
 

Mike001

New member
Joined
Apr 27, 2016
Messages
578
Points
0
Ruesnam,

I know that you mean well but if Nemanja does not understand the structural layout of PHP, there is very little chance that he will understand what you have displayed in classes.

My guess is, he is trying to fill a variable with multiple fields. That happens a lot to new programmers that do not have a good understanding of variables vs. arrays.

Nemanja if you add your code I can help you displaying the data. Or if you are really motivated you can watch a bunch of free videos on my website, under the PHP section that will explain exactly what you are trying to do.

Just a thought.
 
Older threads
Replies
3
Views
1,653
Replies
4
Views
3,195
Replies
13
Views
5,266
Newer threads
Replies
7
Views
3,212
Replies
4
Views
4,337
Replies
3
Views
1,856
Latest threads
Replies
4
Views
685
Replies
0
Views
118
Replies
1
Views
154
Replies
1
Views
149

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top