Jump to content

Data depending on Variable


conker87

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]
Link to comment
https://forums.phpfreaks.com/topic/19130-data-depending-on-variable/
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.
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.

Archived

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

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