Jump to content


Photo

Data depending on Variable


  • Please log in to reply
7 replies to this topic

#1 conker87

conker87
  • Members
  • PipPipPip
  • Advanced Member
  • 504 posts
  • LocationDerby

Posted 30 August 2006 - 12:00 PM

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:
<?
$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>

WG4G - Articles, Opinions, Podcasts and Reviews.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 12:04 PM

Based on your description, shouldn't that be ... WHERE `essay`='$essay'" not WHERE `id`=$essay"
Legend has it that reading the manual never killed anyone.
My site

#3 conker87

conker87
  • Members
  • PipPipPip
  • Advanced Member
  • 504 posts
  • LocationDerby

Posted 30 August 2006 - 12:17 PM

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.
WG4G - Articles, Opinions, Podcasts and Reviews.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 12:20 PM

.. so has that resolved the problem?
Legend has it that reading the manual never killed anyone.
My site

#5 conker87

conker87
  • Members
  • PipPipPip
  • Advanced Member
  • 504 posts
  • LocationDerby

Posted 30 August 2006 - 12:33 PM

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.
WG4G - Articles, Opinions, Podcasts and Reviews.

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 12:48 PM

This should help when adapted to your output needs:

$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

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.
Legend has it that reading the manual never killed anyone.
My site

#7 conker87

conker87
  • Members
  • PipPipPip
  • Advanced Member
  • 504 posts
  • LocationDerby

Posted 30 August 2006 - 01:05 PM

That's excellent. Works like a charm.

Thank you so much.
WG4G - Articles, Opinions, Podcasts and Reviews.

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 August 2006 - 01:18 PM

Clearly I think better with caffeine than without.  Glad it's all working for you.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users