Jump to content

Reading from database


Tandem

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
Link to comment
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.
Link to comment
Share on other sites

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.