xux Posted October 3, 2006 Share Posted October 3, 2006 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]<?phpinclude('header1.tpl');// connecting to MySQL server$connection = mysql_connect('localhost', '', '') or die ('Unable to connect!');// selecting database for usemysql_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 returnedif (mysql_num_rows($result) > 0){// print HTML tableecho'<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 fieldwhile($row = mysql_fetch_object($result)){// prints in format "email"echo '<tr>';echo '<td>' . $row->email.'</td>';echo '</tr>';}echo '</table>';}else{// print error messageecho 'No rows found!';}// once processing is complete// free result setmysql_free_result($result);// close connection to MySQL servermysql_close($connection);?></div><?php[/code]Thanks in advance Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/ Share on other sites More sharing options...
thedarkwinter Posted October 3, 2006 Share Posted October 3, 2006 Hiinstead of $row->email use $row[0]Cheers,tdw Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-102957 Share on other sites More sharing options...
thedarkwinter Posted October 3, 2006 Share Posted October 3, 2006 ... and maybe mysq_fetch_row instead of my_sql_fetch_object( didn't read it 100% the first time!!) Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-102958 Share on other sites More sharing options...
xux Posted October 3, 2006 Author Share Posted October 3, 2006 I have done that but it is still not displaying the content of the database. Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-102964 Share on other sites More sharing options...
thedarkwinter Posted October 3, 2006 Share Posted October 3, 2006 HiIs 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 Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-102973 Share on other sites More sharing options...
JasonLewis Posted October 3, 2006 Share Posted October 3, 2006 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. Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-102978 Share on other sites More sharing options...
xux Posted October 3, 2006 Author Share Posted October 3, 2006 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. Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-102992 Share on other sites More sharing options...
thedarkwinter Posted October 3, 2006 Share Posted October 3, 2006 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 Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-103010 Share on other sites More sharing options...
xux Posted October 3, 2006 Author Share Posted October 3, 2006 tdw, I appreciate your efforts.The only field in the table is email,so do you think I have to change the query?Thanks Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-103024 Share on other sites More sharing options...
xux Posted October 4, 2006 Author Share Posted October 4, 2006 Hey to the gurus out there,help is still needed. Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-103627 Share on other sites More sharing options...
thedarkwinter Posted October 4, 2006 Share Posted October 4, 2006 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 usemysql_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 returnedif (mysql_num_rows($result) > 0){// print HTML tableecho'<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 fieldwhile($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 messageecho 'No rows found!';}// once processing is complete// free result setmysql_free_result($result);// close connection to MySQL servermysql_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 Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-103641 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 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.THanksXux Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104883 Share on other sites More sharing options...
thedarkwinter Posted October 6, 2006 Share Posted October 6, 2006 HiIt shouldn't make any difference at all...but my table is something likedd_contactscontid contname contemail contphone contfax contaddresssetc...also, try [code]while($row = mysql_fetch_row($result)){ print_r($row);}[/code]see if there is any variables in $rowcheers,tdw Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104904 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 Tdw, Ok.Let me try that out.I hope it is work.Thanks Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104924 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 Tdw, Here is the code[code]<?phpinclude('header1.tpl');// connecting to MySQL server$connection = mysql_connect('localhost', 'sme_root', 'justus') or die ('Unable to connect!');// selecting database for usemysql_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 returnedif (mysql_num_rows($result) > 0){// print HTML tableecho'<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 fieldwhile($row = mysql_fetch_row($result)){ print_r($row);}// once processing is complete// free result setmysql_free_result($result);// close connection to MySQL servermysql_close($connection);?></div><?phpinclude('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.ThanksMy RegardsXUX Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104940 Share on other sites More sharing options...
printf Posted October 6, 2006 Share Posted October 6, 2006 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! Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104946 Share on other sites More sharing options...
thedarkwinter Posted October 6, 2006 Share Posted October 6, 2006 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] Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104948 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 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 Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104950 Share on other sites More sharing options...
thedarkwinter Posted October 6, 2006 Share Posted October 6, 2006 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... Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104956 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 ok,let me try that out.Thanks Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104957 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 tdw, this is becoming interesting,it returned the id but the email adresses were just blanks something likeEmail Address of Subscribers12 What do you think is wrong?ThanksMy Regards,XUX Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104964 Share on other sites More sharing options...
xux Posted October 6, 2006 Author Share Posted October 6, 2006 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 usemysql_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><?phpinclude('footer.tpl');?>[/code]please HELP is NeededThanks Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104973 Share on other sites More sharing options...
thedarkwinter Posted October 6, 2006 Share Posted October 6, 2006 ... 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] Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-104983 Share on other sites More sharing options...
xux Posted October 9, 2006 Author Share Posted October 9, 2006 Thanks, I have implemented the changes but it is still ouputting just numbers.I appreciate your assistance.ThanksXUX Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-106204 Share on other sites More sharing options...
xux Posted October 9, 2006 Author Share Posted October 9, 2006 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 menMy Regards,XUX Link to comment https://forums.phpfreaks.com/topic/22852-need-help-with-displaying-data-from-database/#findComment-106211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.