Page 1 of 1

selecting a whole table?

Posted: Mon May 19, 2003 4:41 am
by irealms
if i want to select and display a whole table is this correct?

Code: Select all

$query = "SELECT * FROM users"

Posted: Mon May 19, 2003 5:22 am
by []InTeR[]
Yep

solution for you

Posted: Mon May 19, 2003 5:56 am
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:

thanks

Posted: Mon May 19, 2003 8:27 am
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

Posted: Mon May 19, 2003 8:34 am
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

hehe

Posted: Mon May 19, 2003 8:42 am
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

Posted: Mon May 19, 2003 9:00 am
by []InTeR[]
[]InTeR[] wrote:Yep
Hey, the query was correct to select the whole page... :(

I only forgot the show part :lol:

hehe

Posted: Mon May 19, 2003 9:02 am
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.

hmmm

Posted: Mon May 19, 2003 9:57 am
by irealms
it's calling out the same row each time

Posted: Mon May 19, 2003 10:07 am
by []InTeR[]
WHat's the source you have now?

Re: hehe

Posted: Mon May 19, 2003 10:14 am
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

Inter

Posted: Mon May 19, 2003 10:24 am
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;
?>

Posted: Mon May 19, 2003 10:57 am
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

thanks

Posted: Tue May 20, 2003 3:27 am
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.