Using $_GET varibale to output data

Hello,

First let me explain my problem, I have 2 pages the first page pull a list of Guide titles from a database for example if i had a guide called “Install windows” it would be pulled from the database with a link next to the guide called “read this Guide” now that works fine.

the problem that i am having is when a user clicks the “Read this guide” it will goto the next page and pull the content from the database that releates to the guide. however nothing is being displayed on the next page. My code is shown below for the 2 pages and my database structure.

Guides.php (1st page)

<?php


/**
 * @author Joe Moore
 * @copyright 2010
 */

//Include the database connection details, to allow us to communicate with the database
include 'dbcon.php';


echo '<p> Here are all the articles in the database </p>';

$result = @mysql_query('SELECT guidetitle, id FROM guides');

if (!$result) 
{
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

// Display the text of each guide in a paragraph
//with a "Delete this Guide" link next to each

while ($row = mysql_fetch_array($result))
{
   $id = $row['id'];
    $guidetitle = $row['guidetitle'];
    echo "<p>$guidetitle " .
         "<a href=read.php?id=$id>Read this guide</a></p>";
   
}

?>
read.php (2nd page)
<?php
//include('auth.php');

/**
 * @author Joe Moore
 * @copyright 2010
 */
 ?>
<h1>Welcome <?php // echo $_SESSION['SESS_FIRST_NAME'];?></h1>
<?php
//Include the database connection details, to allow us to communicate with the database
include 'dbcon.php';

$id = $_GET['id'];

$guide = mysql_query("SELECT guidetext, id FROM guides WHERE id='$id'");
    
if (!$guide) 
{
    exit('<p>Error Fetching Guide Details: ' .
        mysql_error() . '</p>');
}

$guide = mysql_fetch_array($guide);
?>

my database structure (table)

id
guidetext
guidedate
guidetitle
author

Thanks

  • Joe

define “nothing is being displayed”. You see blank page with no even word “Welcome” on it?
You have syntax error then and should set up your server to see it when it occurs.

I dont get any errors and Welcome is displayed what I am saying is that no database records are being outputted to the screen

I have modifed my code on the read.php page to this.


<?php
//include(‘auth.php’);

/**

  • @author Joe Moore
  • @copyright 2010
    */
    ?>
    <h1>Welcome <?php // echo $_SESSION[‘SESS_FIRST_NAME’];?></h1>
    <?php
    //Include the database connection details, to allow us to communicate with the database
    include ‘dbcon.php’;

$id = $_GET[‘id’];

$guide = mysql_query(“SELECT guidetext FROM guides WHERE id=‘$id’”);

if (!$guide)
{
exit('<p>Error Fetching Guide Details: ’ .
mysql_error() . ‘</p>’);

}

while ($guide = mysql_fetch_array($guide))
{
$read = $guide[‘guidetext’];
echo “$read”;
}
?>


it works but I get an error which says this

Warning: mysql_fetch_array() expects parameter 1 to be resource, array given in C:\wamp\www\blog\read.php on line 25

well remove that unnecessary unnecessary while statement from your code.

As it became clear to me, your first code were fine, you just didn’t print out anything
so, just add there

$read = $guide['guidetext'];
echo $read;

(note that variables in PHP should be addressed without quotes)

Also, it is very bad practice to use same names for the different variables.
So, name your array something different, like $guide_array for example

$guide_array = mysql_fetch_array($guide);
echo $guide_array['guidetext'];

Many thanks Shrapnel_N5 :slight_smile: