Jump to content
Larry Ullman's Book Forums

Taking a array and trying to load it in to a mysql database


Recommended Posts

I'm trying to help the Friends of My Local Library with a project to make it easier to deal with donated books.  There is a program that will take and either buy used books or recycle them. We have to put together a list of books with specific information and send it to the program sponsor.  Right now it has our members of the "friends" hand entering the information into an Excel spreadsheet and sending it to the program sponsor.

I've figured out how to get the information from the ISBNdb.com using "curl".  It comes as a json array that looks like this when I search using an input form like this:


<form action="add_bt_book20h.php" method="get">
ISBN: <input type="text" name="isbn"><br>
<br>
<input type="submit">
</form>
 

I get back this information:

************************************

{"book":{"publisher":"Peachpit Press",

","language":"en_US","format":"Paperback","image":"https://images.isbndb.com/covers/60/15/9780321376015.jpg","title_long":"PHP 5 Advanced: Visual QuickPro Guide","edition":"2nd","dimensions":"Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches","pages":608,"date_published":"2007-03-15T00:00:01Z","subjects":["Computers & the Internet","Internet"],"authors":["Ullman, Larry"],"title":"PHP 5 Advanced: Visual QuickPro Guide","isbn13":"9780321376015","msrp":"39.99","binding":"Paperback","related":{"ePub":"0132712288"},"isbn":"0321376013","other_isbns":[{"isbn":"9780132712286","binding":"print"}]}}

***********************************

Yes, that's Larry's PHP5 Advanced book. for space reasons I pulled "synopsis" and it's value out

I can then  get it to look like what follows by trimming stuff, assigning a $json = $book and then using JSON decode and a loop to look like the following:

************************************************

publisher=>Peachpit Press
language=>en_US
format=>Paperback
image=>https://images.isbndb.com/covers/60/15/9780321376015.jpg
title_long=>PHP 5 Advanced: Visual QuickPro Guide
edition=>2nd
dimensions=>Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches
pages=>608
date_published=>2007-03-15T00:00:01Z
subjects=>Array
authors=>Array
title=>PHP 5 Advanced: Visual QuickPro Guide
isbn13=>9780321376015
msrp=>39.99
binding=>Paperback
related=>Array
isbn=>0321376013
other_isbns=>Array

*****************************

I've also built a database in Mysql to take this all  in with the goal of then selecting just the keys/values that we need to send to the program sponsor. My question is how do I send( insert ) that in to my database.  I've built forms to collect and insert info into databases, but how do I upload this stuff or can i somehow plug it automatically into a form(I've built one to manually do this and it works)

Any solutions would be most appreciated.

dave

(kn6koo)

Here's what I've done so far to get to where I am.  Not pretty, but I'm not sure what I have that would work.

****************************

<?php

$url = "https://api2.isbndb.com/book/" . $_GET["isbn"] ."";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "accept: application/json",
   "Authorization: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);

$book = $resp;

echo $book . '<br><br>';
$book2 = ltrim($book, '{"book":' );

echo $book2 . "<br>";
$book3 = rtrim($book2,"}");

$str4='{"';
$str5= '}';
$book5 = ($str4 . "" . $book3 . "" .$str5) ;

echo($str4 . "" . $book3 . "" .$str5) ;

'<br><br>';

'<br><br>';

echo $book5;
'<br><br>';
'<br><br>';
?>

<br />
<br><br>
<br><br>
<br><br>
<?php
// Assign JSON encoded string to a PHP variable
$json = $book5 ;
 
// Decode JSON data to PHP associative array
$arr = json_decode($json, true);
 
// Loop through the associative array
foreach($arr as $key=>$value){
    echo $key . "=>" . $value . "<br>";
}
echo "<hr>";
// Decode JSON data to PHP object
$obj = json_decode($json);
 
// Loop through the object
foreach($obj as $key=>$value){
    echo $key . "=>" . $value . "<br>";
}
?>

</body>

********************************

Here's the output of all that:

*******************************

{"book":{"publisher":"Peachpit Press","synopsys":"

PHP is currently one of the most popular server-side, HTML-embedded scripting language on the Web. It's specifically designed for Web site creation and is frequently being used to replace the functionality created by Perl to write CGI scripts. PHP's popularity and easier-to-learn appeal has spawned a new breed of programmer, those who are only familiar with and only use PHP.

Sharpen your PHP skills with the fully revised and updated, PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide! Filled with fifteen chapters of step-by-step content and written by best-selling author and PHP programmer, Larry Ullman, this guide teaches specific topics in direct, focused segments, shows how PHP is used in real-world applications, features popular and most-asked-about scripts, and details those technologies that will be more important in the future. You'll learn about object-oriented programming, PHP interactions with a server,

","language":"en_US","format":"Paperback","image":"https://images.isbndb.com/covers/60/15/9780321376015.jpg","title_long":"PHP 5 Advanced: Visual QuickPro Guide","edition":"2nd","dimensions":"Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches","pages":608,"date_published":"2007-03-15T00:00:01Z","subjects":["Computers & the Internet","Internet"],"authors":["Ullman, Larry"],"title":"PHP 5 Advanced: Visual QuickPro Guide","isbn13":"9780321376015","msrp":"39.99","binding":"Paperback","related":{"ePub":"0132712288"},"isbn":"0321376013","other_isbns":[{"isbn":"9780132712286","binding":"print"}]}}

publisher":"Peachpit Press","synopsys":"

PHP is currently one of the most popular server-side, HTML-embedded scripting language on the Web. It's specifically designed for Web site creation and is frequently being used to replace the functionality created by Perl to write CGI scripts. PHP's popularity and easier-to-learn appeal has spawned a new breed of programmer, those who are only familiar with and only use PHP.

Sharpen your PHP skills with the fully revised and updated, PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide! Filled with fifteen chapters of step-by-step content and written by best-selling author and PHP programmer, Larry Ullman, this guide teaches specific topics in direct, focused segments, shows how PHP is used in real-world applications, features popular and most-asked-about scripts, and details those technologies that will be more important in the future. You'll learn about object-oriented programming, PHP interactions with a server,

","language":"en_US","format":"Paperback","image":"https://images.isbndb.com/covers/60/15/9780321376015.jpg","title_long":"PHP 5 Advanced: Visual QuickPro Guide","edition":"2nd","dimensions":"Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches","pages":608,"date_published":"2007-03-15T00:00:01Z","subjects":["Computers & the Internet","Internet"],"authors":["Ullman, Larry"],"title":"PHP 5 Advanced: Visual QuickPro Guide","isbn13":"9780321376015","msrp":"39.99","binding":"Paperback","related":{"ePub":"0132712288"},"isbn":"0321376013","other_isbns":[{"isbn":"9780132712286","binding":"print"}]}}
{"publisher":"Peachpit Press","synopsys":"

PHP is currently one of the most popular server-side, HTML-embedded scripting language on the Web. It's specifically designed for Web site creation and is frequently being used to replace the functionality created by Perl to write CGI scripts. PHP's popularity and easier-to-learn appeal has spawned a new breed of programmer, those who are only familiar with and only use PHP.

Sharpen your PHP skills with the fully revised and updated, PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide! Filled with fifteen chapters of step-by-step content and written by best-selling author and PHP programmer, Larry Ullman, this guide teaches specific topics in direct, focused segments, shows how PHP is used in real-world applications, features popular and most-asked-about scripts, and details those technologies that will be more important in the future. You'll learn about object-oriented programming, PHP interactions with a server,

","language":"en_US","format":"Paperback","image":"https://images.isbndb.com/covers/60/15/9780321376015.jpg","title_long":"PHP 5 Advanced: Visual QuickPro Guide","edition":"2nd","dimensions":"Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches","pages":608,"date_published":"2007-03-15T00:00:01Z","subjects":["Computers & the Internet","Internet"],"authors":["Ullman, Larry"],"title":"PHP 5 Advanced: Visual QuickPro Guide","isbn13":"9780321376015","msrp":"39.99","binding":"Paperback","related":{"ePub":"0132712288"},"isbn":"0321376013","other_isbns":[{"isbn":"9780132712286","binding":"print"}]}{"publisher":"Peachpit Press","synopsys":"

PHP is currently one of the most popular server-side, HTML-embedded scripting language on the Web. It's specifically designed for Web site creation and is frequently being used to replace the functionality created by Perl to write CGI scripts. PHP's popularity and easier-to-learn appeal has spawned a new breed of programmer, those who are only familiar with and only use PHP.

Sharpen your PHP skills with the fully revised and updated, PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide! Filled with fifteen chapters of step-by-step content and written by best-selling author and PHP programmer, Larry Ullman, this guide teaches specific topics in direct, focused segments, shows how PHP is used in real-world applications, features popular and most-asked-about scripts, and details those technologies that will be more important in the future. You'll learn about object-oriented programming, PHP interactions with a server,

","language":"en_US","format":"Paperback","image":"https://images.isbndb.com/covers/60/15/9780321376015.jpg","title_long":"PHP 5 Advanced: Visual QuickPro Guide","edition":"2nd","dimensions":"Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches","pages":608,"date_published":"2007-03-15T00:00:01Z","subjects":["Computers & the Internet","Internet"],"authors":["Ullman, Larry"],"title":"PHP 5 Advanced: Visual QuickPro Guide","isbn13":"9780321376015","msrp":"39.99","binding":"Paperback","related":{"ePub":"0132712288"},"isbn":"0321376013","other_isbns":[{"isbn":"9780132712286","binding":"print"}]} 






publisher=>Peachpit Press
synopsys=>

PHP is currently one of the most popular server-side, HTML-embedded scripting language on the Web. It's specifically designed for Web site creation and is frequently being used to replace the functionality created by Perl to write CGI scripts. PHP's popularity and easier-to-learn appeal has spawned a new breed of programmer, those who are only familiar with and only use PHP.

Sharpen your PHP skills with the fully revised and updated, PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide! Filled with fifteen chapters of step-by-step content and written by best-selling author and PHP programmer, Larry Ullman, this guide teaches specific topics in direct, focused segments, shows how PHP is used in real-world applications, features popular and most-asked-about scripts, and details those technologies that will be more important in the future. You'll learn about object-oriented programming, PHP interactions with a server,


language=>en_US
format=>Paperback
image=>https://images.isbndb.com/covers/60/15/9780321376015.jpg
title_long=>PHP 5 Advanced: Visual QuickPro Guide
edition=>2nd
dimensions=>Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches
pages=>608
date_published=>2007-03-15T00:00:01Z
subjects=>Array
authors=>Array
title=>PHP 5 Advanced: Visual QuickPro Guide
isbn13=>9780321376015
msrp=>39.99
binding=>Paperback
related=>Array
isbn=>0321376013
other_isbns=>Array
publisher=>Peachpit Press
synopsys=>

PHP is currently one of the most popular server-side, HTML-embedded scripting language on the Web. It's specifically designed for Web site creation and is frequently being used to replace the functionality created by Perl to write CGI scripts. PHP's popularity and easier-to-learn appeal has spawned a new breed of programmer, those who are only familiar with and only use PHP.

Sharpen your PHP skills with the fully revised and updated, PHP 5 Advanced for the World Wide Web: Visual QuickPro Guide! Filled with fifteen chapters of step-by-step content and written by best-selling author and PHP programmer, Larry Ullman, this guide teaches specific topics in direct, focused segments, shows how PHP is used in real-world applications, features popular and most-asked-about scripts, and details those technologies that will be more important in the future. You'll learn about object-oriented programming, PHP interactions with a server,


language=>en_US
format=>Paperback
image=>https://images.isbndb.com/covers/60/15/9780321376015.jpg
title_long=>PHP 5 Advanced: Visual QuickPro Guide
edition=>2nd
dimensions=>Height: 9 Inches, Length: 7 Inches, Weight: 2.09880073424 Pounds, Width: 1.151 Inches
pages=>608
date_published=>2007-03-15T00:00:01Z
subjects=>Array
authors=>Array
title=>PHP 5 Advanced: Visual QuickPro Guide
isbn13=>9780321376015
msrp=>39.99
binding=>Paperback

************************************************

Thanks in advance

Link to comment
Share on other sites

You've already done all the hard work. Kudos! You have the values you need, as an associative array. All you need to do now is pop those values into an INSERT query, something like

$arr = json_decode($json, true);
if (!empty($arr['isbn'])) { // Basic validation that data was returned...
  INSERT INTO books (ISBN, title) VALUES ('{$arr['isbn']}', '{$arr['title']}');

Although it'd be best if you use prepared statements or somehow sanitized the values before using them in the query.

Link to comment
Share on other sites

 Share

×
×
  • Create New...