Jump to content

Recommended Posts

Hi everyone,

 

I'm trying to create a site that allows me to click through the contents of a MYSQL database one record at a time using code along the lines of $row = mysql_fetch_object($result);.

 

I have created dynamic links at the top of the page as follows:

 

<?php echo <a href="record.php?wid-1">Previous Entry</a><br />

<?php echo <a href="record.php?wid+1">Next Entry</a><br />

 

(wid being the Primary Key)

 

However the first time the page is parsed I want it to default to record 1

 

Hopefully you can understand what I am trying to do from the following:

 

if ((!isset($_GET['wid']) || trim($_GET['wid']) == ''))

{

  wid=1

  }

 

The statement in braces looks wrong, but I can't find the correct syntax anywhere. Do I need to convert it to a string e.g $record first?

 

Any help appreciated.

 

Many thanks,

Gary

 

Link to comment
https://forums.phpfreaks.com/topic/98400-assigning-variable-default/
Share on other sites

Thanks for both those pieces of input.

 

I have now put the code together but my browser is giving me nothing despite me having records in my MYSQL database.

 

Would anyone mind scanning the code below to find any errors. I've checked and checked it, but apologies if it is a stray semi-colon. I have a feeling it may be discrepancies with my php tags as I have bastardised a tutorial as a template. If anyone has a rule for the correct opening and closing php that would be very welcome too.

 


<html>
<head></head>
<body>

<!-- standard page header  -->

<?php
// includes
include('conf.php');
include('functions.php');



// open database connection
$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect');

//select database
mysql_select_db($db) or die ('Unable to select database');

// generate and excecute query
// check for record ID
if ( empty($_GET['wid']) )
{
  $wid=1;
  }
else
{
$wid = $_GET['wid'];
}
$query = "SELECT word, category, tags, def, pub, author, submitted, amended, flagged FROM words WHERE wid = $wid";

$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// get resultset as object
$row = mysql_fetch_object($result);


//Create navigation links for top of page

<?php 
echo <a href=\"record.php?wid=".$wid-1."\">Previous Entry</a><br />";
echo <a href=\"record.php?wid=".$wid+1."\">Next Entry</a><br />";
?>

// print details
if ($row)
{
?>
<p>
<font size="1"><?php echo ($row->word); ?></font>
<p>
<b><?php echo "Word ID: ".$record." (Primary Key)" ?> </b>
<p>
<font size="-1"><?php echo "Category: "($row->category); ?></font>
<p>
<font size="-1"><?php echo "Tags: "($row->tags); ?></font>
<p>
<font size="-1"><?php echo "Definition: "($row->def); ?></font>
<p>
<font size="-1"><?php echo "Published?:  Feature to follow"; ?></font>
<p>
<font size="-1"><?php echo "Submitted by: "($row->author); ?></font>
<p>
<font size="-2"><?php echo "Date Submitted: "formatDate ($row->submitted); ?></font>
<p>
<font size="-1"><?php echo "Date Amended: Feature to Follow"; ?></font>
<p>
<font size="-1"><?php echo "Flagged: Feature to Follow"; ?></font>
<p>
<font size="-1"><?php echo "Votes: Feature to Follow"; ?></font>
<p>
<?php
}
else
{
?>
<p>
<font size="-1">That record could not be located in the database.</font>
<?php
}

// close database connection
mysql_close($connection);
?>

<!-- standard page footer -->

</body>
</html>

 

Cheers,

Gary

This is where things went wrong. The open and close php tags weren't needed here.  Also the echo statements here weren't right and the ones towards the bottom weren't right either.

<?php 
echo <a href="record.php?wid=".$wid-1."\">Previous Entry</a><br />";
echo <a href="record.php?wid=".$wid+1."\">Next Entry</a><br />";
?>

This corrects the syntax errors.

<html>
<head></head>
<body>

<!-- standard page header  -->

<?php
// includes
include('conf.php');
include('functions.php');



// open database connection
$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect');

//select database
mysql_select_db($db) or die ('Unable to select database');

// generate and excecute query
// check for record ID
if ( empty($_GET['wid']) )
{
  $wid=1;
  }
else
{
$wid = $_GET['wid'];
}
$query = "SELECT word, category, tags, def, pub, author, submitted, amended, flagged FROM words WHERE wid = $wid";

$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// get resultset as object
$row = mysql_fetch_object($result);


//Create navigation links for top of page


echo '<a href="record.php?wid='.$wid-1 .'">Previous Entry</a><br />';
echo '<a href="record.php?wid=' .$wid+1 .'">Next Entry</a><br />';


// print details
if ($row)
{
?>
<p>
<font size="1"><?php echo ($row->word); ?></font>
<p>
<b><?php echo "Word ID: ".$record." (Primary Key)" ?> </b>
<p>
<font size="-1"><?php echo "Category: " .$row->category; ?></font>
<p>
<font size="-1"><?php echo "Tags: " . $row->tags; ?></font>
<p>
<font size="-1"><?php echo "Definition: " . $row->def; ?></font>
<p>
<font size="-1"><?php echo "Published?:  Feature to follow"; ?></font>
<p>
<font size="-1"><?php echo "Submitted by: " . $row->author; ?></font>
<p>
<font size="-2"><?php echo "Date Submitted: " . formatDate ($row->submitted); ?></font>
<p>
<font size="-1"><?php echo "Date Amended: Feature to Follow"; ?></font>
<p>
<font size="-1"><?php echo "Flagged: Feature to Follow"; ?></font>
<p>
<font size="-1"><?php echo "Votes: Feature to Follow"; ?></font>
<p>
<?php
}
else
{
?>
<p>
<font size="-1">That record could not be located in the database.</font>
<?php
}

// close database connection
mysql_close($connection);
?>

<!-- standard page footer -->

</body>
</html>

Excellent. Thanks for that.

 

I had to put brackets around the $wid increments / decrements for the link to show properly thus:

 

echo '<a href="record.php?wid='.($wid-1) .'">Previous Entry</a><br />';
echo '<a href="record.php?wid=' .($wid+1) .'">Next Entry</a><br />';

 

Is this an issue with my text editor perhaps (into which I cut and pasted the code)?

Is this an issue with my text editor perhaps (into which I cut and pasted the code)?

It's an issue in that an editor could highlight thoses kinds of mistakes for you and make it easier to find.  Zend studio will do that and probably others as well.  I personally never had a problem with pasting code into an editor and it getting mangled.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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