[英]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.