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') ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?