Jump to content

Displaying information from database onto website


WilliamNova

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.