使用MySQLI如何显示我的数据库在我的网站上的数据 [英] Using MySQLI how can I display data from my database on my website

查看:91
本文介绍了使用MySQLI如何显示我的数据库在我的网站上的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经长时间拖延从MySQL切换到MySQLI。我已经开始一个新的项目,并决定我宁愿去开始它的好习惯,而不是坏的,与已弃用的MySQL。

I have long procrastinated switching to MySQLI from MySQL. I have started a new project and decided I'd rather go ahead and start it off with good habits instead of bad ones with deprecated MySQL.

我想弄清楚如何遍历数据库中的表,并在我的网站上显示该数据。这个过程,我相信,是直接的,但解释,所以你会明白,问题不是那么。我会尽量简短。

I am trying to figure out how to iterate through a table in my database and display that data on my website. The process, I am sure, is straight forward but the explanation so you will understand the question is less so. I will try to be brief.

示例:我有一个名为DATABASE的数据库,其中包含一个名为TABLE的表。在我的网站上,我有5个类别。每个类别有5个项目的列表。每个列表中的每个项目依次具有分配给它的5个值。 IE:名称,dob,年,月,日期。

Example: I have a database named DATABASE that contains a table called TABLE. On my website I have 5 categories. Each category has a list of 5 items each. Each item in each list in turn has 5 values assigned to it. IE: name, dob, year, month, date. Those 5 values I have stored in my database for each list item in each category.

视觉表示: http://imgur.com/PMmbOV6 (每个列表项都有5个值)

Visual Representation: http://imgur.com/PMmbOV6 (Each one of the list items has 5 values )

代码:[COLUMN_# ]表示数据库表中对应于我想要拉入并插入到页面/ html中的值的列。

Code: [COLUMN_#] represents the column in the db table corresponding to the value I'd like to pull and insert into the page/html

<div class="category_1"> <!-- First loop iteration -->
    <div class="item_1"> 
        <a title="[COLUMN_1]" href="[COLUMN_2]">[COLUMN_3]</a>
        <span class="class">[COLUMN_4]</span>
        <span class="class"> [COLUMN_5]</span>
    </div>
</div>

<div class="category_1"> <!-- Second loop iteration -->
    <div class="item_2"> 
        <a title="[COLUMN_1]" href="[COLUMN_2]">[COLUMN_3]</a>
        <span class="class">[COLUMN_4]</span>
        <span class="class"> [COLUMN_5]</span>
    </div>
</div>

<div class="category_1"> <!-- Third loop iteration -->
    <div class="item_3"> 
        <a title="[COLUMN_1]" href="[COLUMN_2]">[COLUMN_3]</a>
        <span class="class">[COLUMN_4]</span>
        <span class="class"> [COLUMN_5]</span>
    </div>
</div>

对类别1的第5次循环迭代等等(每个列表项的一次迭代)。

And so on to the 5th loop iteration for category 1. (one iteration for each list item.)

所有5个类别的所有列表项的所有值都在同一个数据库中,因此我假设如果我想将值从类别1的db代码将需要类似 - 选择所有从表其中category等于1 - 我写一个循环。然后当循环没有找到类别等于1的更多列表项时,它结束并移动到下面的下一个脚本以查询类别2的列表项。等等。

All the values for all list items from all 5 categories are in the same database so I assume if I wanted to pull the values into the page from the db for category 1 the code would need to be something like - select all from table where category is equal to 1 - I write a loop. Then when the loop finds no more list items with category equal to 1 it ends and moves on to the next script below that to query list items for category 2. And so on.

PHP代码到目前为止:

PHP Code so far:

<?php
// Connect to and select a database
$db = new mysqli('localhost', 'root', '', 'DATABASE');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

// Query the table 'TABLES' (I'm also assuming I only need to query just the table from here)
$sql = <<<SQL
    SELECT *
    FROM `TABLE`
SQL;

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}
?>

感谢提前这个问题真的让我,我不想回到MySQL。这里的每个人都告诉我不要! =)

Thanks in advance this problem is really getting me and I don't want to go back to MySQL. Everyone here tells me not to! =)

推荐答案

mysqli 当你得到 $ result 时,它也是一个对象。所以你可以用

The big thing to remember in mysqli OOP is that when you get your $result, it too is an object. So you would perform operations with that instead

while($row = $result->fetch_assoc()) {
    echo $row['fieldname'];
}

这篇关于使用MySQLI如何显示我的数据库在我的网站上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆