Newbie89 Posted December 6, 2012 Share Posted December 6, 2012 Example such like as shown at the bottom: <?php$dbHandle = new PDO("sqlite:/var/databases/testsite/database.sqlite");$dbHandle->exec("CREATE TABLE mybooks (id INTEGER PRIMARY KEY, author NOT NULL, title NOT NULL)");$mybooks = array( "Mass Effect: Revelation" => "Drew Karpyshyn", "Mass Effect: Ascension" => "Drew Karpyshyn", "Dragon Age: The Stolen Throne" => "David Gaider");while (list($title,$author) = each($mybooks)) { $author = $dbHandle->quote($author); $title = $dbHandle->quote($title); $result = $dbHandle->exec("INSERT INTO mybooks (author,title) VALUES ($author,$title)");}?> Link to comment Share on other sites More sharing options...
Newbie89 Posted December 6, 2012 Author Share Posted December 6, 2012 and also this: <?php$random = rand(1,3);$dbHandle = new PDO("sqlite:/var/databases/testsite/database.sqlite");$result = $dbHandle->query("SELECT * FROM mybooks WHERE id=$random");$book = $result->fetch(PDO::FETCH_ASSOC);echo "Random peek into my libary:<br><br>$book[title] by $book[author]";?> Link to comment Share on other sites More sharing options...
birbal Posted December 6, 2012 Share Posted December 6, 2012 and also this: <?php $random = rand(1,3); $dbHandle = new PDO("sqlite:/var/databases/testsite/database.sqlite"); $result = $dbHandle->query("SELECT * FROM mybooks WHERE id=$random"); $book = $result->fetch(PDO::FETCH_ASSOC); echo "Random peek into my libary:<br><br>$book[title] by $book[author]"; ?> 1) Generating a random number between 1 to 32) Initialize Database connection3) Execute query get all fields from mybook where id is the generated random number4) fetch the PDOStatement object as associative array Example such like as shown at the bottom: <?php $dbHandle = new PDO("sqlite:/var/databases/testsite/database.sqlite"); $dbHandle->exec("CREATE TABLE mybooks (id INTEGER PRIMARY KEY, author NOT NULL, title NOT NULL)"); $mybooks = array( "Mass Effect: Revelation" => "Drew Karpyshyn", "Mass Effect: Ascension" => "Drew Karpyshyn", "Dragon Age: The Stolen Throne" => "David Gaider"); while (list($title,$author) = each($mybooks)) { $author = $dbHandle->quote($author); $title = $dbHandle->quote($title); $result = $dbHandle->exec("INSERT INTO mybooks (author,title) VALUES ($author,$title)");} ?> Initialize Database connectionexecute the query which creates tablecreate an array of book name and author key value pairloop through and make variable out of the array to store title and author escape the inputs of author and title insert it in DB You could figure it out on your own if you look into the corresponding class and functions. check the php manual for corresponding refference. 1 Link to comment Share on other sites More sharing options...
Newbie89 Posted December 6, 2012 Author Share Posted December 6, 2012 Thanks for your help^^ birbal Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now