This is a discussion on Inserting PHP multidimensional arrays into a mySQL database within the Coding Help forums, part of the Webmaster Help category; you are welcome. Glad it worked out for you in the end....
| |||||||
| Register | Forum Rules | FAQ | Donate | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||||
| you are welcome. Glad it worked out for you in the end.
__________________ :. Web Design & Development Web Design Ireland :. Search Engines Optimization Search Engines Optimization :. Directory Submission Directory Submission :. News & Press Release Ireland GiveItSocks.com :. Used Cars Ireland, Car Parts & Car Audio Cars For Sale, Car Parts & Accessories :. I Have 2 Find It Directory SEF Directory |
| ||||
| Not exactly sure where to start here.. I was asked as a favor to do this for a friend. I am doing a screen scrape w/class_http.php and its dumping the content to an array. I am trying to find a way to take that array content and dump it into a mysql db. Here's the next glitch/problem though. The way the screen scraper works it keys on a word in the HTML to start where it scrapes the table from.. So what infact I am getting is the field names in one array and the content in the next array.. I am really only interested in the content. ANY Help you can provide would be helpful. Basically I am trying to setup an IP Logging DB Here is the code/ printout of the array. Code: Array
(
[0] => Array
(
[0] => Kick
[1] => Ban
[2] => Name:
[3] => Team:
[4] => Ping:
[5] => Score:
[6] => IP
[7] => Global ID
)
[1] => Array
(
[0] =>
[1] =>
[2] => Bozo(Spectator)
[3] => Blue
[4] => 60
[5] => 0
[6] => 25.25.25.20
[7] => e3e6654a9232bf22218e5f2c39e72538
)
)
================= table structure and db ================= Code:
<?php
$mysqldate = date( ‘Y-m-d’, $phpdate );
$phpdate = strtotime( $mysqldate );
// Make a MySQL Connection
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("thedb") or die(mysql_error());
// Create a MySQL table in the selected database
mysql_query("CREATE TABLE playerdata (
id int(11) NOT NULL AUTO_INCREMENT,
date varchar(30) NOT NULL default '',
name varchar(60) NOT NULL default '',
score varchar(30) NOT NULL default '',
IP varchar(30) NOT NULL default '',
GUID varchar(60) NOT NULL default '',
PRIMARY KEY (id)
);")or die(mysql_error());
echo "Table Created!";
?>
scrape and insert ========================== Code: <?php /* Include the http class. Modify path according to where you put the class file. */ require_once(dirname(__FILE__).'/class_http.php'); /* First, instantiate a new http object. */ $h = new http(); /* Where do you want to store your cache files? Default is current dir. You can set it here, or hard-code in the class. You must end this value with a "/". */ $h->dir = "/cache/"; $url = "http://127.20.25.2/admin/players"; if (!$h->fetch($url, 0, null, "scrapeuser", "scrapepass")) { echo "<h2>There is a problem with the http request!</h2>"; echo "Status: ".$h->status; echo "<pre>".$h->header."</pre>"; echo $h->log; exit(); } $current_players = http::table_into_array($h->body, "Kick", 1, null); /* Print out the array so you can see the stats data. */ echo "<pre>"; print_r($current_players); echo "</pre>"; $mysqldate = date("Y-m-d"); echo $mysqldate; mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("thedb") or die(mysql_error()); $playerdata=serialize($current_players); $query=INSERT INTO playerdata VALUES('$mysqldate', '$playerdata'); ?> |
| ||||
| well the code below I didnt write, but it is how a solved my problem, baiscly what the code does is srcapes data from a table and then while's each row into a mysql_query, then just run them all at the one time, the system I use it for can run up to 200 insertion querys a day and it works find. PHP Code: Last edited by Ciarán Mc Cann; 03-08-2007 at 12:04 PM. |
| ||||
| Thanks for the quick reply, I will try and run this. But I believe the hosting provider I am using doesn't allow curl for some reason. So I don't think I can use your code, also I haven't seen any curl based php code that will allow me to login to a secured or protected site.. Like my code does. I will see if maybe I can adapt some of your sql statements though.. Unless of course anyone else has any ideas that would fit my current code. Last edited by riddler; 02-08-2007 at 09:36 PM. |
| ||||
| Sorry I couldnt be of more help. Just look at the code again, as the sql was abit wrong frist, but I edited. then i get an output of somthing that is almost a corect mysql_query , and then I use a str_replace fuction to clean up the out put. |
| ||||
| Actually your code gave me a couple ideas.. heres the WORKING end result.. w/my code. I have the page refresh every minute to give an updated push of current info into the DB. -Riddler Code: <META HTTP-EQUIV="refresh" content="60;URL=http://thehostedhost/php/dbPush.php"> <?php /*Include the http class. Modify path according to where you put the class file.*/ require_once(dirname(__FILE__).'/class_http.php'); /* First, instantiate a new http object. */ $h = new http(); $h->dir = "/cache/"; //Cache directory for Screen Scrape $url = "http://127.28.60.2/Admin"; // URL if (!$h->fetch($url, 0, null, "urluser", "urlpass")) { //Do the scrape with the username/password at the end to authenticate echo "<h2>There is a problem with the http request!</h2>"; echo "Status: ".$h->status; echo "<pre>".$h->header."</pre>"; echo $h->log; exit(); } $current_players = http::table_into_array($h->body, "Kick", 1, null); /* Print out the array so you can see the stats data. */ //echo "<pre>"; //print_r($current_players); //echo "</pre>"; $mysqldate = date("Y-m-d"); //DateStamp for entry into MySQL $size=sizeof($current_players); //Check the size of the array (See how many rows are returned by the scrape) $playernum=count($current_players)-1; //Scrub the Rows to remove the first Row of the Scrape.. Showing the Number of Players online. $dbh = mysql_connect("thehost", "thetableuser", "thepasswd") or die(mysql_error()); //Connect to your MySQL DB mysql_select_db("thedb") or die(mysql_error()); // Select the DB for($c = 1; $c <= sizeof($current_players); $c++) { //Insert in a loop the players that are online to the DB if ($current_players[$c][2] != ''){ $playername = html_entity_decode($current_players[$c][2]); $playerIP = html_entity_decode($current_players[$c][6]); $playerGUID = html_entity_decode($current_players[$c][7]); //check for duplicates $query="SELECT * FROM playerdata WHERE IP='$playerIP' AND GUID='$playerGUID' "; $result=mysql_query($query); if(mysql_num_rows($result)==0) { //end check for duplicate $query = "INSERT INTO playerdata (date, name, IP, GUID) VALUES ('$mysqldate', '$playername', '$playerIP', '$playerGUID')"; $result = mysql_query($query) or die(mysql_error()); } } } echo "<H2>Insert Complete</H2>"; ?> |
| Tags |
| arrays, database, inserting, multidimensional, mysql, php |
| Thread Tools | |
| Display Modes | |
|
|
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| calender script (php, mysql) | 7aken | Coding Help | 3 | 20-10-2006 06:57 AM |
| php, mysql & .asp from same laptop | alex | Server / Technical Administration Tips and Queries | 11 | 16-10-2006 06:01 PM |
| ||||||||
| | ![]() | |||||||