简体   繁体   English

从同一行的数据库中获取数据

[英]Get data from database in the same row

I have a list of numbers on a table like this: 我在这样的桌子上有一个数字列表:

n | n | Title--- | 标题--- | Content 内容
1 | 1 | Page1 | 第1页 Page one content 第一页内容
2 | 2 | Page2 | 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 第1页-第1页的内容

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. 当您执行SELECT * FROM查询时,它返回一个信息数组,如果您意识到该数组是什么样的,便可以从中获取信息。

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. 这将从Page = $ page的行中获取Header和Content。

Sorry if you miss understood the question. 对不起,如果您错过了理解的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:MY​​SQLI-从同一行获取数据 - PHP: MYSQLI - Get data from same row 我有一个鼠标悬停功能,可以根据每一行的值从数据库获取数据。 但是,它为所有行返回相同的值 - I have a mouseover function to get data from the database based on the value of each row. However, it is returning the same values for all row 如何从数据库中的相同数据中获取第一个数据和最后一个数据? - How to get the first data and last data from the same data in the database? php从所有行的同一列中获取特定数据 - php get specific data from the same column in all row 从2种不同形式获取数据并继续同一表行 - Get data from 2 different forms and continue the same table row 从数据库中逐行获取值 - Get values from database from row to row 在特定的div中显示数据库中具有相同数据的行 - Display row in database with same data in particular div 如何在单行中获取数据 <td> ,具有相同ID的数据库中的多个条目 - how to get data in single row in <td>, multiple entry in database with same id 当我使用jQgrid单击同一行中的链接时,我需要从jqGrid获取整个行数据 - I need to get the entire row data from jqGrid when i click the link in same row using jQgrid 从中获取数据 <td> 在由行ID指定的同一表行上执行计算 - Get data from <td> on the same table row specified by row id and perform computation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM