Jump to content

Need help with displaying data from database!


xux

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
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
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
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
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
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
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
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
... 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]

Archived

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

×
×
  • Create New...

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.