Jump to content

Archived

This topic is now archived and is closed to further replies.

xux

Need help with displaying data from database!

Recommended Posts

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
[code]
<?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

[/code]
Thanks in advance

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Hi

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

try

[code]
echo mysql_num_rows($result)
echo mysql_error();
[/code]

and see if it is returning results/or an errror

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


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

Share this post


Link to post
Share on other sites
Hey to the gurus out there,help is still needed.

Share this post


Link to post
Share on other sites
Hi... i just ran this code (slightly modded) on my end and it works perfectly:

[code]<?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);
?>
[/code]

and here is the output (***ed)

[code]<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>[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

[code]
while($row = mysql_fetch_row($result))
{
    print_r($row);
}
[/code]

see if there is any variables in $row

cheers,
tdw

Share this post


Link to post
Share on other sites
Tdw,
      Ok.Let me try that out.I hope it is work.Thanks

Share this post


Link to post
Share on other sites
Tdw,
    Here is the code[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');
?>[/code]

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

Share this post


Link to post
Share on other sites
No closing bracket for... (that's why indenting your code is very important)

[code]if (mysql_num_rows($result) > 0)
{[/code]

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


me!

Share this post


Link to post
Share on other sites
hehe... when you stare at the same code, everything starts to go fuzzy!!!

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

Share this post


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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
tdw,
      this is becoming interesting,it returned the id but the email adresses were just blanks something like
Email Address of Subscribers
1
2
What do you think is wrong?Thanks
My Regards,
XUX

Share this post


Link to post
Share on other sites
Hi,
    This is the form am using to collect input from a form
[code]

<?php
include('header1.tpl');
    $email=$HTTP_POST_VARS['email'];
    $email=trim($email);
  $email = addslashes($email);

 
// open connection to MySQL server
$connection = mysql_pconnect('localhost', '', '')
or die ('Unable to connect!');
// select database for use
mysql_select_db('DB') or die ('Unable to select database!');
$query="insert into newsletter values (null, '$email')";
$result=mysql_query($query);
if ($result)
echo'<br/>';
echo'<center>';
echo 'Thank you for signing up for ';
echo'</center>';


?>
</div>

<?php
include('footer.tpl');
?>
[/code]
please HELP is Needed
Thanks

Share this post


Link to post
Share on other sites
... starting to make sense  :)

well, is id an auto_increment field?

the insert statement i would use would be

[code] $query="INSERT INTO newsletter (email) VALUES ('$email')";[/code]

also, instead of
[code]$HTTP_POST_VARS['email'][/code]try[code]$_POST['email'][/code]though i dont know whether that will make a difference.

also... you need brackets on the if ($result), cos at the moment it will be saying successful regardless of whether it worked or not.
[code]
if ($result)
{
echo'<br/>';
echo'<center>';
echo 'Thank you for signing up for ';
echo'</center>';
}
[/code]

Share this post


Link to post
Share on other sites
Thanks,
          I have implemented the changes but it is still ouputting just numbers.I appreciate your assistance.Thanks
XUX

Share this post


Link to post
Share on other sites
Hi,
    Maybe I should drop the table and create another one,what do you think?The current table is auto increment,do you think it should be changed?Thanks men
My Regards,
XUX

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.