Jump to content


Photo

Need help with displaying data from database!


  • Please log in to reply
29 replies to this topic

#1 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 03 October 2006 - 11:04 AM

Hi,
  Please I need help,am trying to display the content of a database via  php (as a web interface),it is not reporting any error but it is not displaying the contrnts the codes are below
<?php
include('header1.tpl');
// connecting to MySQL server
$connection = mysql_connect('localhost', '', '') 
or die ('Unable to connect!');
// selecting database for use
mysql_select_db('DB') or die ('Unable to select database!');
// create and execute query
$query = 'SELECT email FROM table';
$result = mysql_query($query) 
or die ('Error in query: $query. ' . mysql_error());
// check if records were returned
if (mysql_num_rows($result) > 0)
{
// print HTML table
echo'<br/>';
echo '<table width=80% cellpadding=10 cellspacing=0 border=1 align=center>';
echo
'<tr><td><b><center>Email Address of Subscribers</center></b></td></tr>';
echo '<ul>';
// iterate over record set
// print each field
while($row = mysql_fetch_object($result))
{
// prints in format "email"
echo '<tr>';
echo '<td>' . $row->email.'</td>';
echo '</tr>';
}
echo '</table>';
}
else
{
// print error message
echo 'No rows found!';
}
// once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection);
?>
</div>
<?php

Thanks in advance

#2 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 11:08 AM

Hi

instead of $row->email use $row[0]

Cheers,
tdw
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#3 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 11:10 AM

... and maybe mysq_fetch_row instead of my_sql_fetch_object

( didn't read it 100% the first time!!)
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#4 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 03 October 2006 - 11:27 AM


I have done that but it is still not displaying the content of the database.

#5 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 11:52 AM

Hi

Is it atleast outputting the beggining of the table? And then is it giving you empty cells, or no cells?

try

echo mysql_num_rows($result)
echo mysql_error();

and see if it is returning results/or an errror
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#6 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 03 October 2006 - 12:00 PM

i would try replacing $row->email with $row['email];
also do what thedarkwinter said and change mysql_fetch_object to something like mysql_fetch_array.
Good luck with your coding.
Jason / ProjectFear / Jaysonic

#7 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 03 October 2006 - 12:21 PM

Hi,
    It is outputting the header of the table but it is leaving out the cells.When I did what you recommended it outputted the number of rows with no error but did not output the content of the database.

#8 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 03 October 2006 - 12:57 PM

is "SELECT email FROM table" the exact query?

i.e. if you are using "SELECT *" or "SELECT name,email" then adjust the index below in thr $row[];

while ($row = mysql_fetch_row($result))
{
// prints in format "email"
echo '<tr>';
echo '<td>' . $row[0] . '</td>';
echo '</tr>';
}

if you are still not coming right... the view the source of the output and post it in here... that might help me?

cheers,
tdw
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#9 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 03 October 2006 - 01:10 PM

tdw,
    I appreciate your efforts.The only field in the table is email,so do you think I have to change the query?Thanks

#10 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 04 October 2006 - 10:30 AM

Hey to the gurus out there,help is still needed.

#11 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 04 October 2006 - 11:37 AM

Hi... i just ran this code (slightly modded) on my end and it works perfectly:

<?php
// connecting to MySQL server
$connection = mysql_connect('localhost', '', '')
or die ('Unable to connect!');
// selecting database for use
mysql_select_db('dddom') or die ('Unable to select database!');
// create and execute query
$query = 'SELECT contemail FROM dd_contacts'; // note, i have a db with email addresses - this is my exact query
$result = mysql_query($query)
or die ('Error in query: $query. ' . mysql_error());
// check if records were returned
if (mysql_num_rows($result) > 0)
{
// print HTML table
echo'<br/>';
echo '<table width=80% cellpadding=10 cellspacing=0 border=1 align=center>';
echo
'<tr><td><b><center>Email Address of Subscribers</center></b></td></tr>';
echo '<ul>';
// iterate over record set
// print each field
while($row = mysql_fetch_row($result)) // using mysql_fetch_row
{
// prints in format "email"
echo '<tr>';
echo '<td>' . $row[0].'</td>'; // ... and $row[0];
echo '</tr>';
}
echo '</table>';
}
else
{
// print error message
echo 'No rows found!';
}
// once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection);
?>

and here is the output (***ed)

<br/><table width=80% cellpadding=10 cellspacing=0 border=1 align=center><tr><td><b><center>Email Address of Subscribers</center></b></td></tr><ul><tr><td></td></tr>
<tr><td>admin@****.com</td></tr>
<tr><td>postmaster@***.co.uk</td></tr>
<tr><td>postmaster@***.co.uk</td></tr>
<tr><td>simon@***.co.uk</td></tr>

I'm a bit confused about why its not working for you!!

what happens when you go into your mysql terminal and type "SELECT email FROM table";

cheers,
tdw
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#12 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 06 October 2006 - 10:19 AM

Tdw,
          I appreciate your effort.I am sort of confused too about the whole Issue Bcos It is Just the small part of a bigger Issue.Am working towards making an html newsletter.Can you give me insight into your database scheme?how many fields do you have?I will really appreciate.THanks
Xux

#13 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 06 October 2006 - 10:51 AM

Hi

It shouldn't make any difference at all...

but my table is something like

dd_contacts

contid  contname  contemail  contphone  contfax  contaddresss

etc...

also, try

while($row = mysql_fetch_row($result))
{
    print_r($row);
}

see if there is any variables in $row

cheers,
tdw
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#14 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 06 October 2006 - 11:12 AM

Tdw,
      Ok.Let me try that out.I hope it is work.Thanks

#15 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 06 October 2006 - 11:33 AM

Tdw,
    Here is the code
<?php
include('header1.tpl');
// connecting to MySQL server
$connection = mysql_connect('localhost', 'sme_root', 'justus') 
or die ('Unable to connect!');
// selecting database for use
mysql_select_db('sme_sme2') or die ('Unable to select database!');
// create and execute query
$query = 'SELECT email FROM newsletter';
$result = mysql_query($query) 
or die ('Error in query: $query. ' . mysql_error());
// check if records were returned
if (mysql_num_rows($result) > 0)
{
// print HTML table
echo'<br/>';
echo mysql_num_rows($result);
echo mysql_error();
echo '<table width=80% cellpadding=10 cellspacing=0 border=1 align=center>';
echo
'<tr><td><b><center>Email Address of Subscribers</center></b></td></tr>';
echo '<ul>';
// iterate over record set
// print each field

while($row = mysql_fetch_row($result))
{
    print_r($row);
}
// once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection);
?>
</div>
<?php
include('footer.tpl');
?>

but it is throwing this error
Parse error: syntax error, unexpected $end in /home/sme/public_html/subscriber.php on line 48
I wonder what is wrong.
Thanks
My Regards
XUX

#16 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 06 October 2006 - 11:39 AM

No closing bracket for... (that's why indenting your code is very important)

if (mysql_num_rows($result) > 0)
{

Place the closing } after your closing while(db result) {} <- bracket!


me!

#17 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 06 October 2006 - 11:43 AM

hehe... when you stare at the same code, everything starts to go fuzzy!!!

you aren't closing the "if (mysql_num_rows($result) > 0)"
while($row = mysql_fetch_row($result))
{
    print_r($row);
}
echo '</table>';
}


Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#18 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 06 October 2006 - 11:46 AM

tdw,
      Thanks,I guess when you spend a long time working on codes you sometimes make simple mistakes.It generated this
Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => )
What do you think about this?
My Regards

#19 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 06 October 2006 - 11:52 AM

to me - it sats that the sql query worked fine -  but there are no values for email in the database.

try using that exact code but change the SQL statement to "select *" and see if anything at all comes out...
Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#20 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 06 October 2006 - 11:55 AM

ok,let me try that out.
Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users