Jump to content

[SOLVED] Another newbie question- selecting data from sql


unitedintruth

Recommended Posts

I have a table in a mysql database for newsletters. The fields in the database are month, year, newsletter.

 

The newsletters are in pdf format and in an /admin/newsletter folder on the server but the filename is in the database.

 

How would I get a list showing the month, and year with a "Read" button linking to the newsletter?

 

I am a little lost on how to write the table to get the thing to show so people can access the data.

 

Thanks

$sql = mysql_query("SELECT * FROM table_name WHERE Criteria");//if you just want to select every entry on the table, leave the WHERE part out.

while ($row = mysql_fetch_assoc($sql)){//loop through the entries
$month = $row['month'];//should be $row[month column name]
$year = $row['year'];//should be $row[year column name]
$link = $row['newsletter'];//should be $row[newsletter column name]

echo "$month $year. <a href='$link'>Click here to view!</a>";
}

 

Something like that should work. Just make sure to change this based on your table name, and your column names. You may also need to change the a href stuff based on if the filename is the absolute url, or just the local location. You may have to make it a href='my/folder/etc/$link' or something like that.

 

Hope that helped

I have this added but it is not showing any data from the database.

 

$sql = mysql_query("SELECT * FROM 'newsletter'");//if you just want to select every entry on the table, leave the WHERE part out.

while ($row = mysql_fetch_assoc($sql)){//loop through the entries
$month = $row['month'];//should be $row[month column name]
$year = $row['year'];//should be $row[year column name]
$link = $row['newsletter'];//should be $row[newsletter column name]

echo "$month<&nbsp>$year. <a href='admin/newsletter/$link'>Click here to view!</a>";
}

I have this added but it is not showing any data from the database.

 

$sql = mysql_query("SELECT * FROM 'newsletter'");//if you just want to select every entry on the table, leave the WHERE part out.

while ($row = mysql_fetch_assoc($sql)){//loop through the entries
$month = $row['month'];//should be $row[month column name]
$year = $row['year'];//should be $row[year column name]
$link = $row['newsletter'];//should be $row[newsletter column name]

echo "$month<&nbsp>$year. <a href='admin/newsletter/$link'>Click here to view!</a>";
}

 

Well one thing comes to mind, take away the single quotes around newsletter

 

also, for future reference, when you write queries do something like

$query = mysql_query("STUFF") or die("ERROR ".mysql_error());

or something like that

No Problem! by the way instead of using code tags you may be interested in using the php tags. it just looks prettier =) instead of writing {code} write {php}. (with normal brackets instead of curly brackets of course)

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.