snookian Posted April 18, 2012 Share Posted April 18, 2012 Hi I am currently making a site where users can upload second hand books for sale, I have everything working as i wish apart from i am having trouble creating a account page where users can review the books they have posted. so far i use the following code to bring view a the data associated with an uploaded book based on its id public static function getById( $id ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $sql = "SELECT * FROM books WHERE id = :id"; $st = $conn->prepare( $sql ); $st->bindValue( ":id", $id, PDO::PARAM_INT ); $st->execute(); $row = $st->fetch(); $conn = null; if ( $row ) return new Book( $row ); } I think i can use a similar function for my user account however, each book entered by a user has their member id attached to it, which is gained from there id in the members table which in turn is stored in $_SESSION['id']. My question is, is there a way to rework the code above to have something like select * where member_id = $_SESSION['id']. I have tried a few things and get stuck manly due to the fact i dont know what to replace the current :id with. Thanks in advance, i hope i explained it well enough for you lot to understand. Ian Quote Link to comment https://forums.phpfreaks.com/topic/261199-user-accountdata-retrieval/ Share on other sites More sharing options...
snookian Posted April 18, 2012 Author Share Posted April 18, 2012 This is a function i use to select books based on the categoryid. category has its own table and the id is a foreign key in the books table public static function getList( $numRows=1000000, $categoryId=null, $order="id DESC" ) { $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $categoryClause = $categoryId ? "WHERE categoryId = :categoryId" : ""; $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM books $categoryClause ORDER BY " . mysql_escape_string($order) . " LIMIT :numRows"; $st = $conn->prepare( $sql ); $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT ); if ( $categoryId ) $st->bindValue( ":categoryId", $categoryId, PDO::PARAM_INT ); $st->execute(); $list = array(); while ( $row = $st->fetch() ) { $book = new Book( $row ); $list[] = $book; } Quote Link to comment https://forums.phpfreaks.com/topic/261199-user-accountdata-retrieval/#findComment-1338562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.