WilliamNova Posted August 5, 2013 Share Posted August 5, 2013 So, I'm trying to display information from a table into my database on one of my pages. However, everything I've tried isn't working. Obviously, it's me. I know I'm doing something wrong, just not sure what. I deleted the PHP code related to querying and getting the information, again it was obviously done wrong anyways. So I have a table named 'news' and I have some columns, named 'author', 'date', 'subject', 'body'. I have some areas where I want those values displayed on my website in $author $date $subject $body Seems like whenever I alter the code I always have a new problem. I'm still learning PHP, so please don't just give me the code, I want to LEARN it, so if anything if you could break it down, that would be great and I hope it's not asking for a lot. Quote Link to comment https://forums.phpfreaks.com/topic/280836-displaying-information-from-database-onto-website/ Share on other sites More sharing options...
.josh Posted August 5, 2013 Share Posted August 5, 2013 There are a million reasons why your code could have failed. We cannot even begin to help unless you post the code you tried and errors it output. Then you ask for us to not just *give* you code, but break it down and explain it.. well that's what tutorials are for. There are literally thousands of basic database interaction tutorials out there (including on this site). What tutorials have you looked at? What part of them don't you understand? To be clear, you're going to need to be a lot more specific than making general statements about how you failed and want help. Quote Link to comment https://forums.phpfreaks.com/topic/280836-displaying-information-from-database-onto-website/#findComment-1443495 Share on other sites More sharing options...
mac_gyver Posted August 5, 2013 Share Posted August 5, 2013 basically, displaying database data involves - 1.a) making a connection to the database server and testing that the connection worked. 1.b) selecting the correct database (when not performed as part of step 1.a). 2) forming your sql query statement that gets the row(s) you want in the order that you want them. this would also involve using any inputs for search terms/pagination ... and escaping/binding inputs being put into the query statement. 3) executing the query and testing that it ran without any errors. 4) testing how many rows the query matched and outputting a user message if none. 5) fetching the row(s) that the query matched and outputting the information the way you want it, where you want it on the page. which of these are you having a problem with and what sort of problem or error are you having? Quote Link to comment https://forums.phpfreaks.com/topic/280836-displaying-information-from-database-onto-website/#findComment-1443497 Share on other sites More sharing options...
WilliamNova Posted August 5, 2013 Author Share Posted August 5, 2013 $data = mysql_query("SELECT * FROM news") or die(mysql_error()); $info = mysql_fetch_array( $data ); while($info = mysql_fetch_array( $data )) This is what I have thus far. Which all works just fine and when I use print it displays the correct fields, so I know the database is connected and is relaying information fine. I guess I'm not sure where I put this hand dandy code. <div id="news"> <div class="newsBody"> <div class="newsHeader"> <div class="newsDate"> Posted: <?php echo"$date"; ?> </div> <div class="newsAuthor"> Posted By: <?php echo"$author"; ?> </div><br /> <?php echo"$subject"; ?> </div> <div class="newsSynop"> <?php echo"$body"; ?> </div> </div> </div> I want to have those variables echo the database values. So I can't figure out how to get the info for author in Posted By: <?php echo"$author"; ?> and same with date, subject, body. Quote Link to comment https://forums.phpfreaks.com/topic/280836-displaying-information-from-database-onto-website/#findComment-1443512 Share on other sites More sharing options...
chriscloyd Posted August 5, 2013 Share Posted August 5, 2013 here are two ways of doing what you want $data = mysql_query("SELECT * FROM news") or die(mysql_error()); $info = mysql_fetch_array( $data ); while($info = mysql_fetch_array( $data )){ foreach($info as $k => $v){ $$k = $v; } echo <<<EOT <div class="newsBody"> <div class="newsHeader"> <div class="newsDate"> Posted: {$date} </div> <div class="newsAuthor"> Posted By: {$author} </div> {$subject} </div> <div class="newsSynop"> {$body} </div> </div> EOT; } OR which I prefer: $data = mysql_query("SELECT * FROM news") or die(mysql_error()); $info = mysql_fetch_array( $data ); while($info = mysql_fetch_array($data)){ echo <<<EOT <div class="newsBody"> <div class="newsHeader"> <div class="newsDate"> Posted: {$info['date']} </div> <div class="newsAuthor"> Posted By: {$info['author']} </div> {$info['subject']} </div> <div class="newsSynop"> {$info['body']} </div> </div> EOT; } Quote Link to comment https://forums.phpfreaks.com/topic/280836-displaying-information-from-database-onto-website/#findComment-1443521 Share on other sites More sharing options...
mac_gyver Posted August 5, 2013 Share Posted August 5, 2013 $data = mysql_query("SELECT * FROM news") or die(mysql_error()); $info = mysql_fetch_array( $data ); while($info = mysql_fetch_array( $data )) sorry to point out the obvious, but that code isn't doing what you think. the third line is fetching and not using the first row from the result set. the first row that the while(){} loop will fetch is the second row from the result set. Quote Link to comment https://forums.phpfreaks.com/topic/280836-displaying-information-from-database-onto-website/#findComment-1443559 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.