getting all data echo´d out from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

getting all data echo´d out from database

Post by Goofan »

Hi I was wondering how do u echo out all of the containt of the database.
I got a simple code where i get a users id "unique number for everyone" and what i would like to get is so that when i:

Code: Select all

 
<html>
 <head>
 <title>Two By Two Game</title>
 </head>
 <body bgcolor="#A4A8B0">
 <center><h1>Users</h1>
<?php
include "../login/database.php";
 
  $users =(isset($_GET['users'])) ? (int)$_GET['users'] : false;
    if($users !== false)
 {
    $sqls="SELECT * FROM konto WHERE saved_id=$users";      //selecting all from DB "Konto" where saved_id is the same as in the array $id
    
 }
    else
 {
    echo "NO saved_id!"."<br>"; 
    
 
 }
$results = mysql_query($sqls) or die("Kunde inte lägga till ny text:<br />" . mysql_error());//Skicka info till tabell.
                 
while($rows = mysql_fetch_array( $results ))        //fetching info from file and putting it in $row
{
      echo $All Of The Database."<br>";
}
 
 
?> 
 
 
</body>
</html>
 
now where it sais echo "$All Of The Database" there i want it to echo out all that is within the database where id is id.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: getting all data echo´d out from database

Post by social_experiment »

Depending on what fields are inside your database table you will use something along the lines of

Code: Select all

 
<?php
 while($rows = mysql_fetch_array( $results ))        //fetching info from file and putting it in $row
 {
       echo $rows['field1'];
       echo $rows['field2'];
 }
?>
 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: getting all data echo´d out from database

Post by JakeJ »

If you want to print everything in the database, then you have to iterate through each table OR use various join queries to make a meaningful data stream.

In any case, here is how you iterate through a table. The same thing would apply to any other type of join query. Experiment to meet your needs.

Code: Select all

 
<?php
$sql = "SELECT * FROM table";
$query = mysql_query($sql);
 
While ($row = mysql_fetch_array($query) {
    Echo 'Field 1: ',$row['field1'];
    Echo 'Field 2: ',$row['field2'];
      #etc
}
 
?>
 
 
 
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: getting all data echo´d out from database

Post by Goofan »

Ok so that is the basic echo the fields one by one but say like i create one new field every "min"(Hypothesis) now i want 1 command to echo out all the tables all new as for the old. Is there a way?

I am looking for a echo whole database command. Is there or do i need to insert a new code snippet everytime there is a new field in the database?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: getting all data echo´d out from database

Post by Eran »

There is no built in command, you need to write it yourself. You can use the example Jake gave you, but instead of outputting the fields manually you can iterate over the returned row.

Code: Select all

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result) ) {
    foreach($row as $key => $val) {
         echo $key . ': ' . $val . "<br />";
    }
}
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: getting all data echo´d out from database

Post by Goofan »

Thanks precisly what i needed. Thanks to all ofcourse for all the help.
Post Reply