How to echo database fields with PHP?

Nemanja

Premium Member
Joined
Nov 23, 2012
Messages
304
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,960
Replies
4
Views
3,489
Replies
13
Views
5,468
Newer threads
Replies
7
Views
3,357
Replies
4
Views
4,471
Replies
3
Views
1,967
Latest threads
Replies
1
Views
88
Replies
0
Views
404
Replies
4
Views
1,732
Replies
2
Views
739
Recommended threads

Latest postsNew threads

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