Slulego Slulego - 4 years ago 96
SQL Question

Get data from database in the same row

I have a list of numbers on a table like this:

n | Title--- | Content

1 | Page1 | Page one content

2 | Page2 | Page two content

I'm tying to get the title and content from the number for example:

if (1) {
echo Title;
echo Content;

And get:

Page1 - Page one content

The code I have so far after I connect to the database is:

$page = 1;
$rows = mysql_fetch_array(mysql_query("SELECT * FROM $tbl_name"));
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
//check data
while($page == mysql_fetch_array($result)){
$head = $rows['Title'];
$content = $rows['Content'];

It's not to good but I don't know where to start.

Answer Source

I found out how to do it..

$result = mysql_query("SELECT COUNT(*) FROM `pages` WHERE `Page` = '$page' ");
if (mysql_result($result, 0, 0) > 0) {
   $head = mysql_result(mysql_query("SELECT `Header` FROM `pages` WHERE `Page` = '$page' "), 0, 'Header');
   $content = mysql_result(mysql_query("SELECT `Content` FROM `pages` WHERE `Page` = '$page' "), 0, 'Content');
echo $head;
echo $content;

This gets the Header and Content from the row where Page = $page.

Sorry if you miss understood the question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download