topflight Posted October 8, 2008 Share Posted October 8, 2008 Hello all, I have created this code to display information from the MySQL database and when I go to the page all I get is a white page. May somebody please help me or give me advise how to do this in the future. This is my code. <? $query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'"; $mquery = mysql_query($query); $num = mysql_num_rows($mquery) or die(mysql_error()); ?> <table width="10" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="row">Author Name</th> </tr> <tr> <th scope="row">Date</th> </tr> <tr> <th scope="row">Topic</th> </tr> <tr> <th scope="row">Articale</th> </tr> </table> <tr bgcolor=#EEEEEE> <td><?= "$num[authorname]"; ?></td> <td><?= "$num[date]"; ?></td> <td><?= "$num[newstitle]"; ?></td> <td><?= "$num[news];" ?></td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 1) Don't use short tags! 2) When posting code on the forums, please use , not . 3) Add these lines to the top of the opening PHP tag: ini_set('display_errors', 1); error_reporting(E_ALL); Show us any new errors that are outputted. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 I have modified the code to this: <? ini_set('display_errors', 1); error_reporting(E_ALL); $query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'"; $mquery = mysql_query($query); $num = mysql_num_rows($mquery) or die(mysql_error()); ?> <table width="10" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="row">Author Name</th> </tr> <tr> <th scope="row">Date</th> </tr> <tr> <th scope="row">Topic</th> </tr> <tr> <th scope="row">Articale</th> </tr> </table> <tr bgcolor=#EEEEEE> <td><?= "$num[authorname]"; ?></td> <td><?= "$num[date]"; ?></td> <td><?= "$num[newstitle]"; ?></td> <td><?= "$num[news];" ?></td> </tr> </table> and now I am receiving the following message Notice: Undefined variable: newstitle in C:\xampp\htdocs\viewnews.php on line 29 And what are short tags? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted October 8, 2008 Share Posted October 8, 2008 Yeah, because nowhere in your code do you actually define $newstitle, yet you try to use it in the query. Also, change <? to <?php and <?= to the proper '<?php echo .... ?>' stuff. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 I will use <?php instead thanks, now about the variable situation newtilte is a field name in the table in the database. I want to try have like a news system where I can input news in and it will show I have the database set up correctly? So I how can I pull the information from the database to show like a regular news system I am still trying to learn all this php lol. Also when I post code on here how can I have in the PHP colors. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 Also their is information in the database Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 8, 2008 Share Posted October 8, 2008 <?php ini_set('display_errors', 1); error_reporting(E_ALL); $query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'"; $mquery = mysql_query($query) or die(mysql_error()); //added this to see if problem with query $num = mysql_num_rows($mquery) or die(mysql_error()); //this tells you how many rows from your database were returned, not the actual data ?> <table width="10" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="row">Author Name</th> </tr> <tr> <th scope="row">Date</th> </tr> <tr> <th scope="row">Topic</th> </tr> <tr> <th scope="row">Articale</th> </tr> <?php foreach($mquery as $data){ //this will loop through your row data returned from the db echo "<tr bgcolor=#EEEEEE>\n"; echo "<td>{$data["authorname"]}</td>\n"; echo "<td>{$data["date"]}</td>\n"; echo "<td>{$data["newstitle"]}</td>\n"; echo "<td>{$data["news"]}</td>\n"; echo "</tr>\n"; } ?> </table> Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks but I am still receiving an undefined viable. for $newstitle however that is in the table Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 8, 2008 Share Posted October 8, 2008 its because of this line: $query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'"; You use a variable called $newstitle, but it is not defined in the code that you posted. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 oo ok well how can I define it so it will recognize it in the database becuase that is the exact name I have it under the database. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 8, 2008 Share Posted October 8, 2008 How do I know what you are looking for? How are you using this? Is there a form or something where you select the newstitle that you are searching for? Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 Well so far I created 3 pages page I is the actual form where u submit the news basic html. Page II check page I to make sure that the user put everything in and then insert it into the database. Page III which is this one displays the article like a regular CMS system. What I am trying to do is basically pull the information out of the database and display it like on a homepage something like that. If I am not making sence forgive me I am still trying to learn php. I have two books and I still don't know as much as I should. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 8, 2008 Share Posted October 8, 2008 So you want to list all articles from the database and not one with a specific title? Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 yes however I only want a limit to the 5 recent added. Quote Link to comment Share on other sites More sharing options...
holiks Posted October 8, 2008 Share Posted October 8, 2008 topflight waht cronix means by "not defined" is that $newstitle does not have a value. Therefore you are asking the db to return ONE news result labeled with with an empty title. Also the wrong array is used in your initial code ... you should be using the following... <tr bgcolor=#EEEEEE> <td><?= "$mquery[authorname]"; ?></td> <td><?= "$mquery[date]"; ?></td> <td><?= "$mquery[newstitle]"; ?></td> <td><?= "$mquery[news];" ?></td> </tr> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 8, 2008 Share Posted October 8, 2008 @holics: foreach($mquery as $data){ topflight: do you have a date or timestamp column in your database so you can tell which the last 5 added were? Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 See PHP is just so confusing. lol well then what do you think I should assign to do I want it to do. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 8, 2008 Author Share Posted October 8, 2008 yes I have a date field in the table. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 8, 2008 Share Posted October 8, 2008 whats its name? Quote Link to comment Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 date lol Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 9, 2008 Share Posted October 9, 2008 change: $query = "SELECT * FROM `news` WHERE newstitle = '$newstitle'"; to: $query ="SELECT * FROM `news` ORDER BY `date` DESC LIMIT 5"; Quote Link to comment Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 ok now I am receiving this message. Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\viewnews.php on line 48 also may you guys please tell me where did u learn so I can help people out I tried W3SCHOOLS,php.net,youtube(lol) Php and mysql for dummies sam teach your self php and mysql ALL in one. And I still fill dumb lol. Quote Link to comment Share on other sites More sharing options...
trq Posted October 9, 2008 Share Posted October 9, 2008 We need to see your code. also may you guys please tell me where did u learn so I can help people out I tried W3SCHOOLS,php.net,youtube(lol) Php and mysql for dummies sam teach your self php and mysql ALL in one. And I still fill dumb lol. Theres a good (free) book in my sig. Hudzilla. Don't try and skim though, If you take your time and read it all (cover to cover so to speak) you will have a good foundation. Quote Link to comment Share on other sites More sharing options...
topflight Posted October 9, 2008 Author Share Posted October 9, 2008 Thanks <?php ini_set('display_errors', 1); error_reporting(E_ALL); $query ="SELECT * FROM `news` ORDER BY `date` DESC LIMIT 5"; $mquery = mysql_query($query) or die(mysql_error()); //added this to see if problem with query $num = mysql_num_rows($mquery) or die(mysql_error()); //this tells you how many rows from your database were returned, not the actual data ?> <table width="10" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="row">Author Name</th> </tr> <tr> <th scope="row">Date</th> </tr> <tr> <th scope="row">Topic</th> </tr> <tr> <th scope="row">Articale</th> </tr> </table> <?php foreach($mquery as $data){ //this will loop through your row data returned from the db echo "<tr bgcolor=#EEEEEE>\n"; echo "<td>{$data[authorname]}</td>\n"; echo "<td>{$data[date]}</td>\n"; echo "<td>{$data[newstitle]}</td>\n"; echo "<td>{$data[news]}" ?>}</td>\n"; <? echo "</tr>\n"; } ?> </table> Quote Link to comment Share on other sites More sharing options...
CroNiX Posted October 9, 2008 Share Posted October 9, 2008 My bad, I made an error... <?php ini_set('display_errors', 1); error_reporting(E_ALL); $query ="SELECT * FROM `news` ORDER BY `date` DESC LIMIT 5"; $mquery = mysql_query($query) or die(mysql_error()); //added this to see if problem with query $num = mysql_num_rows($mquery) or die(mysql_error()); //this tells you how many rows from your database were returned, not the actual data ?> <table width="10" border="1" cellspacing="0" cellpadding="0"> <tr> <th scope="row">Author Name</th> </tr> <tr> <th scope="row">Date</th> </tr> <tr> <th scope="row">Topic</th> </tr> <tr> <th scope="row">Articale</th> </tr> <?php if($num>0){ while($data = mysql_fetch_assoc($mquery){ //this was changed echo "<tr bgcolor=#EEEEEE>\n"; echo "<td>{$data["authorname"]}</td>\n"; echo "<td>{$data["date"]}</td>\n"; echo "<td>{$data["newstitle"]}</td>\n"; echo "<td>{$data["news"]}</td>\n"; echo "</tr>\n"; } } ?> </table> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.