Jump to content

Archived

This topic is now archived and is closed to further replies.

Tandem

Reading from database

Recommended Posts

Ok, i'm trying ot set up a news system on my site. The form that inputs the data into the database table works completely fine, it's just displaying the news atricles that gets me confused, and all the google searches i try come up with explanations that are in a different context and hard for me to understand how to change it for my needs.

I want to display the news that is in the database on a page. How do i do this?

I'm not sure what information i need to give for you to be able to help me better, so please somebody say and i'll post whatever is needed.

Thanks,

-Tandem

Share this post


Link to post
Share on other sites
Post your code for the file that process the input from the form. Also I will need to know the primary key in the database table or auto incremented number

Share this post


Link to post
Share on other sites
you need to query the data. this is an example, it was to populate a select box but ill pull some parts out so maybe you can get an idea.

[code]<?php
$result = mysql_query($sql) or die("Error with query (".$sql."): ".mysql_error());
$show_Combo_Box = ""
."<SELECT name=\"".$cb_name."\" id=\"".$idname."\" class=\"".$css_class."\" ".$disable."> \n"
."<OPTION value=\"0\">Select</OPTION>\n";
WHILE ($row  = mysql_fetch_array($result)) {
$selection = "";
if($id){
if($row['indexi'] == $id){
$selection = " selected ";
}
}
$show_Combo_Box .= ""
."<OPTION value=\"".$row['indexi']."\" ".$selection.">"
.$row['indexi']." (".$row['code'].") "
."</OPTION> \n";
} // End WHILE
$show_Combo_Box .= ""
."</SELECT>\n";
mysql_free_result($result);
echo $show_Combo_Box;
?>[/code]

but basically pay attention to the part where it says mysql_query. $sql is the query that i made before. So it gets a database(s) then does an orderby etc... You can always put the query directly in there.

Im still new to mysql and php so there may be a better, but this is the only way i know how.

mysql_query("SELECT column FROM table ORDER BY table ASC")  you know something like that.

But before you do anything you need to "load" up your database.

require_once("./combo_box.conf.php");

i called on that which has this in it

$db['host'] = "localhost"; //mysql host/server
$db['user'] = "user"; // mysql username
$db['pass'] = "pass"; //mysql password
$db['name'] = "database"; //mysql database name


if (!mysql_connect($db['host'], $db['user'], $db['pass'])){
displayErrorPage("Cannot connect to ".$db['host']."!");
exit;
}
@mysql_select_db($db['name']) or die (mysql_error());

Hope that helps you get started. That $row[indexi] is a column i have as well as the code. You can also use 0 or 1 to get the first column.

Share this post


Link to post
Share on other sites
Don't worry i've figured out mysql_fetch_array from a tutorial! :)

Share this post


Link to post
Share on other sites

×

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.