简体   繁体   中英

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";
$result=mysql_query($sql);
//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.

When you make a SELECT * FROM query, it returns an array of information which you can draw from if you realise what the array looks like.

try

$page = 1;
$rows = mysql_fetch_array(mysql_query("SELECT * FROM $tbl_name"));
// Retrieve data from database 
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

echo "<pre>";
print_r($result);
echo "</pre>";

to see the array for yourself, then you will see that the array contains the names of the column headers from your table, so your next bit should have been

while($page == mysql_fetch_array($result)){
    $head = $rows['Title'];
    $content = $rows['Content'];
}

instead of header+content ;)

You have used

while($page == mysql_fetch_array($result)){
    $head = $rows['Header'];
    $content = $rows['Content'];
}

You should use

while ($page = mysql_fetch_array($result)) {
    $head = $page['Title']; // get Title field
    $content = $page['Content']; // get Content field
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM