doutuo3935 2016-02-08 09:16
浏览 25
已采纳

使用MySQL和PHP创建和使用数组

I'm trying to create a SQL query that takes the values of an entire table and dumps them into an array that I can call based the value of a URL parameter.

The parameter passed into the url will be ?username=User1.

I need the query to filter results in the database that are related to the that user (for example - their name, email address, interests etc).

I want to then be able to store them in an array that I can use to call and display the values, for example;

<?php echo htmlentities($row['profiles']['username'], ENT_QUOTES, 'UTF-8'); ?>
<?php echo htmlentities($row['profiles']['location_city'], ENT_QUOTES, 'UTF-8'); ?>

I use the following PHP to set the $u variable in PHP

My SQL query so far is as follows

 $query = " 
        SELECT 
            user_id,
            username, 
            displayname, 
            displayage,
            location_city,
            language
        FROM profiles WHERE username='$u'
    "; 

I then use the following PHP code to try and pass the data into an array;

try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll();

My full code for profile.php;

<?php $_GET['u'] = 'u'; ?>

<?php 

    // First we execute our common code to connection to the database and start the session 
    require("common.php"); 

    // At the top of the page we check to see whether the user is logged in or not 
    if(empty($_SESSION['user'])) 
    { 
        // If they are not, we redirect them to the login page. 
        header("Location: index.php"); 

        // Remember that this die statement is absolutely critical.  Without it, 
        // people can view your members-only content without logging in. 
        die("Redirecting to index.php"); 
    } 

    // Everything below this point in the file is secured by the login system 

    // We can retrieve a list of members from the database using a SELECT query. 
    // In this case we do not have a WHERE clause because we want to select all 
    // of the rows from the database table. 
    $query = " 
        SELECT 
            id, 
            username, 
            email 
        FROM profiles WHERE username='$u'
    "; 

    try 
    { 
        // These two statements run the query against your database table. 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        // Note: On a production website, you should not output $ex->getMessage(). 
        // It may provide an attacker with helpful information about your code.  
        die("Failed to run query: " . $ex->getMessage()); 
    } 

    // Finally, we can retrieve all of the found rows into an array using fetchAll 
    $rows = $stmt->fetchAll(); 
?> 

<?php include('header.php') ?>

<div class="pages navbar-through toolbar-through">
<div class="page" data-page="profile">

<div class="page-content">

<div class="content-block">
<div class="content-block-inner">

<p>Profile content will go here</p>


<a href="private.php">Go Back</a><br />
</div>

</div>
</div>

</div>
</div>

<?php include('footer.php') ?>
  • 写回答

1条回答 默认 最新

  • doude4201 2016-02-08 09:56
    关注

    Change profile.php file contents as shown below:

    <?php $username = (isset($_GET['username']))? trim(strip_tags($_GET['username'])) : ""; ?>
    
    <?php 
    
        // First we execute our common code to connection to the database and start the session 
        require("common.php"); 
    
        // At the top of the page we check to see whether the user is logged in or not 
        if(empty($_SESSION['user'])) 
        { 
            // If they are not, we redirect them to the login page. 
            header("Location: index.php"); 
    
            // Remember that this die statement is absolutely critical.  Without it, 
            // people can view your members-only content without logging in. 
            die("Redirecting to index.php"); 
        } 
    
        // Everything below this point in the file is secured by the login system 
    
        // We can retrieve a list of members from the database using a SELECT query. 
        // In this case we do not have a WHERE clause because we want to select all 
        // of the rows from the database table. 
        $query = " 
            SELECT 
                user_id,
                username, 
                displayname, 
                displayage,
                location_city,
                language
            FROM profiles WHERE username = '$username'
        "; 
    
        try 
        { 
            // These two statements run the query against your database table. 
            $stmt = $db->prepare($query); 
            $stmt->execute(); 
        } 
        catch(PDOException $ex) 
        { 
            // Note: On a production website, you should not output $ex->getMessage(). 
            // It may provide an attacker with helpful information about your code.  
            die("Failed to run query: " . $ex->getMessage()); 
        } 
    
        // Finally, we can retrieve all of the found rows into an array using fetchAll 
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    ?> 
    
    <?php include('header.php') ?>
    
    <div class="pages navbar-through toolbar-through">
    <div class="page" data-page="profile">
    
    <div class="page-content">
    
    <div class="content-block">
    <div class="content-block-inner">
    
    <p>Profile content will go here</p>
    <?php foreach($rows as $row): ?>
       <div>Username: <?php echo $row['username'] ?></div>
       <div>Location: <?php echo $row['location_city'] ?></div>
    
    <?php endforeach; ?>
    
    <a href="private.php">Go Back</a><br />
    </div>
    
    </div>
    </div>
    
    </div>
    </div>
    
    <?php include('footer.php') ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀