selecting a whole table?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

selecting a whole table?

Post by irealms »

if i want to select and display a whole table is this correct?

Code: Select all

$query = "SELECT * FROM users"
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Yep
User avatar
coolpravin
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 12:56 am
Location: India

solution for you

Post by coolpravin »

$query="select * from TABLE" ;
$result=mysql_query($query);
$total=mysql_num_rows($result);
if($total)
{
while($row=mysql_fetch_object($result))
{
////write code to dispaly fielda whatever you like
///eg. echo $row["username"];
}
} :lol:
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

thanks

Post by irealms »

using this atm:

Code: Select all

$query = "SELECT * FROM users";
$result = mysql_query($query, $db_conn);
$num_results = mysql_num_rows($result);
for ($i=0; $i <$num_results; $i++)
&#123;
	echo "<table cellspacing="0" cellpadding="0"><tr><td>$row&#1111;'username']</td>";
	echo "<td>$row&#1111;'charname']</td></tr></table>";
&#125;
?>
and am getting missing T string error
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can't put array elements into a double quoted string like that, instead you can do:

Code: Select all

echo '<table cellspacing="0" cellpadding="0"><tr><td>'.$row['username'].'</td>'; 
echo '<td>'.$row['charname'].'</td></tr></table>';
or

Code: Select all

echo "<table cellspacing="0" cellpadding="0"><tr><td>$row[username]</td>"; 
   echo "<td>$row[charname]</td></tr></table>";
or

Code: Select all

echo "<table cellspacing="0" cellpadding="0"><tr><td>{$row['username']}</td>"; 
   echo "<td>{$row['charname']}</td></tr></table>";
Mac
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

hehe

Post by irealms »

thats great thanks :)

i've always wondered what those in the first case were for when i've seen them used. Which of the 3 would you advise to use
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

[]InTeR[] wrote:Yep
Hey, the query was correct to select the whole page... :(

I only forgot the show part :lol:
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

hehe

Post by irealms »

yup thanks :)

learning as i go, hehe i'm trying to make a member list where admin can approve or deny people that are not approved and then edit members already approved or promote them.
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

hmmm

Post by irealms »

it's calling out the same row each time
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

WHat's the source you have now?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: hehe

Post by twigletmac »

irealms wrote:thats great thanks :)

i've always wondered what those in the first case were for when i've seen them used. Which of the 3 would you advise to use
I tend to use single quotes or here document format as it means I don't have to escape quotes in HTML and with my syntax highlighter concenating the variables means I can see them easily. But it'll come down to what you find easiest to use:
http://www.php.net/manual/en/language.types.string.php

Mac
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Inter

Post by irealms »

Code: Select all

<table cellspacing="5" cellpadding="5"><tr><td><div class="log"><u>Username</u></div></td><td><div class="log"><u>Main character name</u></div></td></tr></table>
<?
ob_start();
session_start();
//show members

$query = "SELECT * FROM users";
$result = mysql_query($query, $db_conn);
$num_results = mysql_num_rows($result);
$row = mysql_fetch_array($result);
for ($i=0; $i <$num_results; $i++)
&#123;
	echo "<table cellspacing="5" cellpadding="5"><tr><td><div class="log">$row&#1111;username]</div></td>";
	echo "<td><div class="log">$row&#1111;charname]</div></td></tr></table>";
	&#125;
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You would need to put the call to mysql_fetch_array() within the loop in order to get a new row each time it is run.

Code: Select all

<table cellspacing="5" cellpadding="5"><tr><td><div class="log"><u>Username</u></div></td><td><div class="log"><u>Main character name</u></div></td></tr></table> 
<?php 
// if you can avoid having to use ob_start() by putting session_start() at the
// very top of the script (perhaps in a config file which is called before
// anything else) it would be better IMHO

ob_start(); 
session_start(); 
//show members 

// Personally I would specify all of the fields that I want to (or expect to)
// return from the table.
$query = "SELECT username, charname FROM users"; 
$result = mysql_query($query, $db_conn); 

// you're looping through the whole result so you can use a while loop
while ($row = mysql_fetch_assoc($result)) 
{ 
   echo "<table cellspacing="5" cellpadding="5"><tr><td><div class="log">$row[username]</div></td>"; 
   echo "<td><div class="log">$row[charname]</div></td></tr></table>"; 
} 
?>
Mac
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

thanks

Post by irealms »

That works great!

About the ob_start, i was having header already sent problems before so i used ob_start as suggested in one of your sticky posts. I do have session start in a config file though the members.php is in admin directory and it didn't seem to be linking right, i'll try what you suggested.
Post Reply