conker87 Posted August 30, 2006 Share Posted August 30, 2006 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] Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 30, 2006 Share Posted August 30, 2006 Based on your description, shouldn't that be ... WHERE `essay`='$essay'" not WHERE `id`=$essay" Quote Link to comment Share on other sites More sharing options...
conker87 Posted August 30, 2006 Author Share Posted August 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 30, 2006 Share Posted August 30, 2006 .. so has that resolved the problem? Quote Link to comment Share on other sites More sharing options...
conker87 Posted August 30, 2006 Author Share Posted August 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 30, 2006 Share Posted August 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
conker87 Posted August 30, 2006 Author Share Posted August 30, 2006 That's excellent. Works like a charm.Thank you so much. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 30, 2006 Share Posted August 30, 2006 Clearly I think better with caffeine than without. Glad it's all working for you. Quote Link to comment 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.