Page 1 of 1

getting all data echo´d out from database

Posted: Thu Jan 21, 2010 10:59 am
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.

Re: getting all data echo´d out from database

Posted: Thu Jan 21, 2010 11:11 am
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'];
 }
?>
 

Re: getting all data echo´d out from database

Posted: Thu Jan 21, 2010 11:18 am
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
}
 
?>
 
 
 

Re: getting all data echo´d out from database

Posted: Thu Jan 21, 2010 11:48 am
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?

Re: getting all data echo´d out from database

Posted: Thu Jan 21, 2010 12:01 pm
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 />";
    }
}

Re: getting all data echo´d out from database

Posted: Thu Jan 21, 2010 12:16 pm
by Goofan
Thanks precisly what i needed. Thanks to all ofcourse for all the help.