如何在html中加入查询,查询数据并在html表中显示

如果您使用像AlientWebguy建议的CMS,您将永远不会学习......我假设您在这里学习。你应该从教程开始,因为这是非常基本的东西。

但要完成你想要做的事情 - 如果我是新手 - 这就是我要做的。我不会给你你的代码,但它肯定足够接近,你应该能够弄明白。

我会创建一个名为functions.php的文件,其中包括以下内容:

// these are freebies. You don't need to understand them yet.

function sqlarr($sql, $numass=MYSQL_BOTH) {

// MYSQL_NUM MYSQL_ASSOC MYSQL_BOTH

$got = array();

$result=mysql_query($sql) or die("$sql: " . mysql_error());

if(mysql_num_rows($result) == 0)

return $got;

mysql_data_seek($result, 0);

while ($row = mysql_fetch_array($result, $numass)) {

array_push($got, $row);

}

return $got;

}

// Sql fetch assoc

function sqlassoc($sql){

$query = mysql_query($sql) or die("$sql:". mysql_error());

$row = mysql_fetch_assoc($query);

return $row;

}

function sqlrow($sql){

$query = mysql_query($sql) or die("$sql:". mysql_error());

$row = mysql_fetch_row($query);

return $row;

}

function sqlquery($sql){

$query = mysql_query($sql) or die("$sql:". mysql_error());

return $row;

}

?>然后在我要输出数据的文件上,我会提出以下内容:

// It sounds like you are already connected to your database, so I'm going to skip that. If you need it, add a comment.

$sql = "SELECT `colNames`, `colName2` FROm `tableName` WHERE `col` = 'condition' ";

// obviously change this to your names, such as `itemNumber`

$results = sqlarr( $sql ); // Now results is going to automatically contain a 2D array.

echo '

'; print_r( $results ); echo '
';

/* this is just to show you what is happening so far. You should get in the habit of using things like this to debug. A lot of people prefer var_dump instead of print_r. I use both because var_dump is harder to read.

// Result should be returning something like this:

// array(

[0] => array(

[0] => 'ABC123',

["itemNumber"] => 'ABC123',

[1] => 'http://www.abc.com',

["link"] => 'http://www.abc.com' ),

[1] => array( ... )

)

// the first level - the [0] => array( or the [1] => array( part - corresponds to a row in your database

// so now we need a way to filter through those rows. Look up php.net/for or php.net/foreach to see how to accomplish that. A lot of people use php.net/while too, but I don't prefer that personally. */

?>

foreach( $results as $row ){ // this is turning $result[0] => array( into $row. So now we can access $result[0]['linkName'] as $row['linkName']

echo '

'.$row['linkName'].'';

} // foreach $row - dont forget to close your curly bracket. Good practice is to always close it as soon as you open it, and to put a comment after it like I just did letting you know what it goes to

?>

如果这里没有任何意义,请发表评论。我很乐意解释。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值