Jump to content

Archived

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

conker87

Data depending on Variable

Recommended Posts

I'm in the process of moving some of my pages to a MySQL database (on a sidenote, there doesn't seem to be a PHP and MySQL forum anymore - so I'm having to post here) and so I'm wanting to get the data from a row depending upon what is added into the URL.

The table consists of 6 columns:
id - primary key,
url - what the "essay" variable is for this essay to be displayed,
author - author of the essay,
content - actual essay,
copyright - general copyright information for the essay.

I'm wanting the content to be displayed in the PHP page when the `url` value of that row is added to the variable.
Example: essay.php?essay=bob - will get the information from the row which has the `url` column = to "bob" and display the value of the `content` column for that row.

Any help and suggestions appreciated.

My code is here, I know it's incomplete and I'm not sure how to finish it:
[code]<?
$username="XX";
$password="XX";
$database="XX";

$essay = $_REQUEST['essay'];

mysql_connect("XX",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `love_essays` WHERE `url`=$essay" or die ("Unable to find data");
$result=mysql_query($query);

?>
<body>

<table border="0" width="100%" class="content">
  <tr>
    <td width="100%" class="title2"><? echo $name; ?>, by <b><? echo $author; ?></b></td>
  </tr>
  <tr>
    <td width="100%" height="10"></td>
  </tr>
  <tr>
    <td width="100%"><p><? echo $content; ?></p>
    <p>[End of Document]</p>
    </font>
    </td>
  </tr>
  <tr>[/code]

Share this post


Link to post
Share on other sites
Based on your description, shouldn't that be ... WHERE `essay`='$essay'" not WHERE `id`=$essay"

Share this post


Link to post
Share on other sites
Actually, re-reading it, it should be `url`= $essay.
The `url` column holds the value that should be added to the url (such as essay.php?essay=bob) so the row with the value of "bob" in the `url` column is included with the <? echo $content; ?> part.

Share this post


Link to post
Share on other sites
.. so has that resolved the problem?

Share this post


Link to post
Share on other sites
Afraid it hasn't.

I think I'm missing something within the PHP code to fetch the field values from the row that is called. I really have no idea how to fetch these values.

Share this post


Link to post
Share on other sites
This should help when adapted to your output needs:

[code]$query = "SELECT * ... whatever your query is'";
$result = mysql_query($query) or die("error ". mysql_error(). " with query ". $query); // just in case
$row = mysql_fetch_array($result); // acquire result array
echo "<h3>". $row['author']. "</h3>"; // or any other named field[/code]

If you have a database field that's text with line breaks, you might want to use the nl2br() function on it prior to output.

Share this post


Link to post
Share on other sites
Clearly I think better with caffeine than without.  Glad it's all working for you.

Share this post


Link to post
Share on other sites

×

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.