Topic awaiting preservation: SQL & PHP Question |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |
posted 10-03-2008 16:17
This one may be reaching but I don't know where else to turn... |
Paranoid (IV) Inmate From: cell 3736 |
posted 10-03-2008 17:15
A few questions come to mind. |
Paranoid (IV) Inmate From: Lost Angeles Kalifornia, via Hawaii.... |
posted 10-03-2008 20:02
Well right now we input the content into a database that I didn't develop. One central location. We do the same with audio and video files to distribute them to all of the sites. I'd like to do the same with the text content but I didn't create it and the person that did, just had a baby, and doesn't have too much free time to put this together. |
Maniac (V) Mad Scientist From: Denver, CO, USA |
posted 10-03-2008 22:25
Though I've already emailed Radical Rob with regards to this, I thought I'd take this opportunity to give a very high level overview of the simplest way of accessing a MySQL database with PHP. code: +------+ | id | | date | | name | | text | +------+
code: <?php $username = 'twitch'; $password = 'twitchistotallysweet'; $hostname = 'localhost'; // This is often the case, but hostnames can vary wildly. $database = 'twitch-database'; mysql_connect($hostname, $username, $password); // Open a connection to the database mysql_select_db($database); // Select a database to access // Something happens here mysql_close(); // Close the connection to the database ?>
code: $sql = "SELECT id, date, name FROM articles ORDER BY date DESC";
code: // Something happens here $sql = "SELECT id, date, name FROM articles ORDER BY date DESC"; $result_set = mysql_query($sql); while($row = mysql_fetch_array($result_set)) { // More stuff happens here! }
code: // More stuff happens here! $name = $row['name']; // The brackets denote an array, and the 'name' is actually returning the name that MySQL is giving it, right from the table itself! $id = $row['id']; // It doesn't matter in which order you declare these variables. $date = $row['date']; // This is going to be a SQL datestamp, which you may want to modify if you don't like YYYY-MM-DD HH:MM:SS format. echo("<li><a href=\"detail.php?id=$id\">$name</a> - $date</li>"); // If you use double quotation marks, PHP will parse whatever happens inside it.
code: <?php $username = 'twitch'; $password = 'twitchistotallysweet'; $hostname = 'localhost'; // This is often the case, but hostnames can vary wildly. $database = 'twitch-database'; mysql_connect($hostname, $username, $password); // Open a connection to the database mysql_select_db($database); // Select a database to access $article_id = $_GET['id']; // Here's where you pull the article ID from the querystring $sql = "SELECT name, date, text FROM article WHERE id = " . mysql_real_escape_string($article_id) . " LIMIT 1"; // Please read the link there to begin to learn about SQL injection attacks $result_set = mysql_query($sql); $row = mysql_fetch_array($result_set); // You only have one result, so you don't need a while() loop echo("<h1>".$row['name']." - " . $row['date'] . "</h1>"); echo("<p>".nl2br($row['text'])."</p>"); // nl2br is a quick way of displaying line breaks as <br /> tags. mysql_close(); // Close the connection to the database ?>
|