Creating and using an Array with MySQL and PHP -
i'm trying create sql query takes values of entire table , dumps them array can call based value of url parameter.
the parameter passed url ?username=user1
.
i need query filter results in database related user (for example - name, email address, interests etc).
i want able store them in array can use call , display values, example;
<?php echo htmlentities($row['profiles']['username'], ent_quotes, 'utf-8'); ?> <?php echo htmlentities($row['profiles']['location_city'], ent_quotes, 'utf-8'); ?>
i use following php set $u
variable in php
my sql query far follows
$query = " select user_id, username, displayname, displayage, location_city, language profiles username='$u' ";
i use following php code try , pass data array;
try { // these 2 statements run query against database table. $stmt = $db->prepare($query); $stmt->execute(); } catch(pdoexception $ex) { // note: on production website, should not output $ex->getmessage(). // may provide attacker helpful information code. die("failed run query: " . $ex->getmessage()); } // finally, can retrieve of found rows array using fetchall $rows = $stmt->fetchall();
my full code profile.php;
<?php $_get['u'] = 'u'; ?> <?php // first execute our common code connection database , start session require("common.php"); // @ top of page check see whether user logged in or not if(empty($_session['user'])) { // if not, redirect them login page. header("location: index.php"); // remember die statement absolutely critical. without it, // people can view members-only content without logging in. die("redirecting index.php"); } // below point in file secured login system // can retrieve list of members database using select query. // in case not have clause because want select // of rows database table. $query = " select id, username, email profiles username='$u' "; try { // these 2 statements run query against database table. $stmt = $db->prepare($query); $stmt->execute(); } catch(pdoexception $ex) { // note: on production website, should not output $ex->getmessage(). // may provide attacker helpful information code. die("failed run query: " . $ex->getmessage()); } // finally, can retrieve of found rows 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 go here</p> <a href="private.php">go back</a><br /> </div> </div> </div> </div> </div> <?php include('footer.php') ?>
change profile.php
file contents shown below:
<?php $username = (isset($_get['username']))? trim(strip_tags($_get['username'])) : ""; ?> <?php // first execute our common code connection database , start session require("common.php"); // @ top of page check see whether user logged in or not if(empty($_session['user'])) { // if not, redirect them login page. header("location: index.php"); // remember die statement absolutely critical. without it, // people can view members-only content without logging in. die("redirecting index.php"); } // below point in file secured login system // can retrieve list of members database using select query. // in case not have clause because want select // of rows database table. $query = " select user_id, username, displayname, displayage, location_city, language profiles username = '$username' "; try { // these 2 statements run query against database table. $stmt = $db->prepare($query); $stmt->execute(); } catch(pdoexception $ex) { // note: on production website, should not output $ex->getmessage(). // may provide attacker helpful information code. die("failed run query: " . $ex->getmessage()); } // finally, can retrieve of found rows 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 go here</p> <?php foreach($rows $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') ?>
Comments
Post a Comment