php - 查询数据并显示在html表格中

标签 php mysql

我喜欢把链接放到一个html表中,这些链接是从mysql表中查询的

------------------------------
item Number |  Item purchased
------------------------------
1           |  a vase
2           |  a candle

etc.....
10
------------------------------
           <<1,2,...100|next>>

这对我来说很难。有人可以给我一个想法或一个图书馆让我开始吗? 非常感谢

最佳答案

如果您像 AlientWebguy 所建议的那样使用 CMS,您将永远学不会……我假设您是来这里学习的。不过,您应该从教程开始,因为这是非常基础的内容。

但要完成您正在尝试做的事情 - 如果我是新手 - 这就是我会做的。我不会给你你的代码,但它绝对足够接近你应该能够弄明白。

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

<?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;
}

?>

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

    <?PHP  include('./functions.php'); 
    // 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 '<pre>'; print_r( $results ); echo '</pre>';
    /* 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. */
    ?>
    <html>
    <body>
    <table>
    <?PHP
    foreach( $results as $row ){  // this is turning $result[0] => array(  into $row.  So now we can access $result[0]['linkName'] as $row['linkName']
       echo '<tr><td>'.$row['linkName'].'</td></tr>';
    }  // 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
    ?>
    </table>
    </body>
    </html>

如果这里有什么不明白的地方,请发表评论。我很乐意解释。

关于php - 查询数据并显示在html表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9155113/

相关文章:

php - Linkedin 配置文件 API 与公开可用的 URI

php - 在 OpenCart 中加速 MySQL 查询

mysql - 在 Yii 中从数据库生成模型?

php - 在 Laravel 5.5 中测试授权策略时遇到问题

php - 警告 : require(. ..):无法打开流:没有这样的文件或目录

php - Codeigniter count_all_results 然后得到返回错误号 1066

mysql - 按随机非空条目分组

php - 防止 mysql_result($sql_result, 0) 抛出错误

php - 插入后更新数据库的列

mysql - 在 Odoo 中上传 mySQL 或 XML 中的现有数据库