Using MySQLI how can I display data from my database on my website

馋奶兔 提交于 2019-12-25 09:34:42

问题


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.

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.

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

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>

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

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 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 . ']');
}
?>

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! =)


回答1:


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'];
}



回答2:


    $sql = "SELECT * FROM users WHERE user_first = 'mohamad';";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);
    if($resultCheck > 0){
        while($row = mysqli_fetch_assoc($result)){
            echo $row['user_uid'] . '<br>';
        }
    }

$conn = your data base connect code



来源:https://stackoverflow.com/questions/20437202/using-mysqli-how-can-i-display-data-from-my-database-on-my-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!