|
-
PHP + MySQL = ljLog
Date: 02/10/05
Keywords: php, mysql, browser, database, sql
[Error: Irreparable invalid markup (' ') in entry. Owner must fix manually. Raw contents below.]
I've written little PHP/MySQL script to parse HTTP requests from hidden <img> tags in LJ posts (e.g. <img src="http://ohsonline.no-ip.com/ljview.php?post-id=74765" style="display:none;" />), archiving it all in a MySQL database and providing a little analysis. So far it's basic, and only really provides the IP, post ID, referring user, timestamp, browser, and platform. I still need to add the source to pump out page hits based on post, ip, browser, platform, etc. Anyways, somebody's probably come up with a similar/better solution before, but as always, feel free to hack away :)
/** / ljLog \\> @author Michael Bommarito @version 20050209-2 @license GPL Keep track of who views your LJ, complete with analysis by post, browser, operating system, and referring URL. */
$db = new mysqli('localhost', 'root', 'password', 'ljlog'); if( mysqli_connect_errno() ) { die("Unable to contact database server. Try again later..."); }
if( isset($_REQUEST['post-id']) ) { $post_id = $db->real_escape_string($_REQUEST['post-id']); $user_ip = ip2long($_SERVER['REMOTE_ADDR']); $user_agent = $_SERVER['HTTP_USER_AGENT']; $user_referer = $_SERVER['HTTP_REFERER']; $query = "SELECT SQL_CACHE * FROM `ljview_post` WHERE `ljview_post_id` = $post_id"; $res = $db->query($query); if( $res->num_rows > 0 ) { $user_post_id = $res->fetch_object()->ljview_post_id; $query = "UPDATE `ljview_post` SET `ljview_post_hits` = `ljview_post_hits` + 1 WHERE `ljview_post_id` = $post_id"; $db->query($query); } else { $query = "INSERT INTO `ljview_post` VALUES($post_id, 1)"; $db->query($query); $user_post_id = $db->insert_id; } $res->free(); if( $user_ip > 0 ) { $query = "SELECT SQL_CACHE * FROM `ljview_ip` WHERE `ljview_ip_ip` = $user_ip"; $res = $db->query($query); if( $res->num_rows > 0 ) { $user_ip_id = $res->fetch_object()->ljview_ip_id; $query = "UPDATE `ljview_ip` SET `ljview_ip_hits` = `ljview_ip_hits` + 1 WHERE `ljview_ip_ip` = $user_ip"; $db->query($query); } else { $query = "INSERT INTO `ljview_ip` VALUES(NULL, $user_ip, 1)"; $db->query($query); $user_ip_id = $db->insert_id; } $res->free(); } if( $user_referer != '' ) { $query = "SELECT SQL_CACHE * FROM `ljview_referer` WHERE `ljview_referer_string` = '$user_referer'"; $res = $db->query($query); if( $res->num_rows > 0 ) { $user_referer_id = $res->fetch_object()->ljview_referer_id; $query = "UPDATE `ljview_referer` SET `ljview_referer_hits` = `ljview_referer_hits` + 1 WHERE `ljview_referer_string` = '$user_referer'"; $db->query($query); } else { $query = "INSERT INTO `ljview_referer` VALUES(NULL, '$user_referer', 1)"; $db->query($query); $user_referer_id = $db->insert_id; } $res->free(); } $query = "SELECT SQL_CACHE * FROM `ljview_user_agent` WHERE `ljview_user_agent_string` = '$user_agent'"; $res = $db->query($query); if( $res->num_rows > 0 ) { $user_user_agent_id = $res->fetch_object()->ljview_user_agent_id; $query = "UPDATE `ljview_user_agent` SET `ljview_user_agent_hits` = `ljview_user_agent_hits` + 1 WHERE `ljview_user_agent_string` = '$user_agent'"; $db->query($query); } else { $query = "INSERT INTO `ljview_user_agent` VALUES(NULL, '$user_agent', 1)"; $db->query($query); $user_user_agent_id = $db->insert_id; } $res->free(); $ts = time(); $query = "INSERT INTO `ljview` VALUES (NULL, $user_ip_id, $post_id, $user_user_agent_id, $user_referer_id, $ts)"; $db->query($query); } else { ?> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
ljLog - ALPHA-000-UNSTABLE-RC-FLAMING-AARDVARK $count = array_pop($db->query("SELECT ljview_id FROM ljview ORDER BY ljview_id DESC LIMIT 1")->fetch_row()); $limit = isset($_REQUEST['limit']) ? abs($_REQUEST['limit']) : 20; $offset = isset($_REQUEST['offset']) ? abs($_REQUEST['offset']) : 0; $place = $count - $offset; $query = "SELECT SQL_CACHE * FROM ljview WHERE ljview_id <= $place ORDER BY ljview_ts DESC LIMIT $limit"; $res = $db->query($query); $end = $offset + $limit; $back = $offset - $limit; print(""); print(""); } ?> |
while( $view = $res->fetch_object() ) { print (""); $ip_id = $view->ljview_ip_id; $res_ip = $db->query("SELECT SQL_CACHE * FROM ljview_ip WHERE ljview_ip_id = $ip_id"); if( $res_ip ) { $ip = $res_ip->fetch_object(); $res_ip->free(); print ( "" . long2ip($ip->ljview_ip_ip) . " | " ); } else { print(" | "); } $post_id = $view->ljview_post_id; print("$post_id | "); if( $view->ljview_ts ) { print ( "" . date("m/d/Y, g:i:s a", $view->ljview_ts) . " | " ); } else { print(" | "); } $user_agent_id = $view->ljview_user_agent_id; $res_user_agent = $db->query("SELECT SQL_CACHE * FROM ljview_user_agent WHERE ljview_user_agent_id = $user_agent_id"); if( $res_user_agent ) { $user_agent = $res_user_agent->fetch_object(); $res_user_agent->free(); $browser = get_browser($user_agent->ljview_user_agent_string); print ( "" . $browser->parent . " | " ); print ( "" . $browser->platform . " | " ); } else { print(" | "); } $referer_id = $view->ljview_referer; $res_referer = $db->query("SELECT SQL_CACHE * FROM ljview_referer WHERE ljview_referer_id = $referer_id"); if( $res_referer ) { $referer = $res_referer->fetch_object(); $res_referer->free(); $referer_stack = split('[-./]', $referer->ljview_referer_string); if( strcasecmp($referer_stack[2], 'livejournal') == 0 ) { $lj_user = $referer_stack[5]; } else if( strcasecmp($referer_stack[3], 'livejournal') == 0 ) { $lj_user = $referer_stack[6]; } print("$lj_user | "); } else { print(" | "); } print (" "); } ?>
} $db->close(); ?>
`ljview_post_id` int(10) unsigned NOT NULL default '0', `ljview_user_agent_id` int(10) unsigned NOT NULL default '0', `ljview_referer` int(10) unsigned NOT NULL default '0', `ljview_ts` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`ljview_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- --------------------------------------------------------
-- -- Table structure for table `ljview_ip` --
DROP TABLE IF EXISTS `ljview_ip`; CREATE TABLE IF NOT EXISTS `ljview_ip` ( `ljview_ip_id` int(10) unsigned NOT NULL auto_increment, `ljview_ip_ip` int(32) NOT NULL default '0', `ljview_ip_hits` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`ljview_ip_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- --------------------------------------------------------
-- -- Table structure for table `ljview_post` --
DROP TABLE IF EXISTS `ljview_post`; CREATE TABLE IF NOT EXISTS `ljview_post` ( `ljview_post_id` int(10) unsigned NOT NULL auto_increment, `ljview_post_hits` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`ljview_post_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- --------------------------------------------------------
-- -- Table structure for table `ljview_referer` --
DROP TABLE IF EXISTS `ljview_referer`; CREATE TABLE IF NOT EXISTS `ljview_referer` ( `ljview_referer_id` int(10) unsigned NOT NULL auto_increment, `ljview_referer_string` text collate utf8_bin, `ljview_referer_hits` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`ljview_referer_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- --------------------------------------------------------
-- -- Table structure for table `ljview_user_agent` --
DROP TABLE IF EXISTS `ljview_user_agent`; CREATE TABLE IF NOT EXISTS `ljview_user_agent` ( `ljview_user_agent_id` int(10) unsigned NOT NULL auto_increment, `ljview_user_agent_string` text collate utf8_bin, `ljview_user_agent_hits` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`ljview_user_agent_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Source: http://www.livejournal.com/community/mysql/47917.html
-
Enhance Query Fu
Date: 02/07/05
Keywords: mysql, database, sql, web
For a website, I have to write a suite of scripts interacting with a database of bibliographic references. Some pages will have an embedded request for a set of the latest titles by an author, and some pages will have an embedded list of titles from which to generate full references. Additionally, I have to build a simple search engine that will query by author, title, publication or abstract.
The search engine is a later problem, and I've already got searching for an embedded list of titles working. So right now I have to solve the author problem. I'm a MySQL newbie and am having problems with the following dilemma:
The problem with citations is that there's no fixed number of authors for a book. As a consequence, the database has a table for the papers, a table for the authors, and a link table with columns for a paper's foreign key and author's foreign key. A paper with three authors populates three rows of the link table.
My challenge is that to pull a complete citation when I search by author, I currently have to make two database queries: SELECT name, id_people, papers_fk FROM papers pa, link li, people pe WHERE name LIKE 'Smith' AND pa.id_paper=li.papers_fk AND pe.id_people=li.people_fk pulls the IDs of papers that Smith has contributed to.
I take that list of IDs and loop through SELECT name, id_people, title, id_paper FROM papers pa, link li, people pe WHERE li.papers_fk='$titleid' AND pa.id_paper='$titleid' AND pe.id_people=li.people_fk entering a different paper ID into $titleid on each loop to generate arrays of citations. A script will distill the arrays into comma-separated lists of authors and format the results.
My question is: Is there a way to distill this into a single query? Which is to say: Can I use a single query to get the complete list of authors for each title that Smith has contributed to?
The approach I have now will work, even if it ends up sending a lot of queries for each web page hit, but being able to streamline this query will both make this page more efficient and give me a leg up on writing the full text (citation + abstract) search section.
Source: http://www.livejournal.com/community/mysql/47657.html
-
can't change max_connections variable
Date: 01/21/05
Keywords: mysql, sql
Hi all,
I can't seem to change my max_connections variable in mysql. If I try to do it through the client I get this. mysql> set global max_connections=500; ERROR 1064: You have an error in your SQL syntax near 'max_connections=500' at line 1
Then put set-variable = max_connections=300 in the my.cnf and restarted mysql.
I did show variables; and I get | max_connections | 100
Why aren't these attempts working?
Source: http://www.livejournal.com/community/mysql/46397.html
-
exporting databases?
Date: 01/20/05
Keywords: mysql, database, sql
hey, i'm having trouble finding anything about this in the manual. how do you export a database to a file, so that you can import it into mysql on another server?
thanks
Source: http://www.livejournal.com/community/mysql/45945.html
-
Database Design Question - Users
Date: 01/17/05
Keywords: no keywords
If you have basic information used throughout the site, (such as name, password, etc,) and then some parts of the site need more information, (such as address, real names, country, phone number, tons of other info,) is is better to have one table with a small profile of necessary information and then a separate table of a more full profile, or is it better to have one big table and just call the data that you need out of it?
Would it be useful to split it up even further, like, a separate profile table for the online shop and one for the forums, both linked by uid to the necessary basic user table?
Which produces better performance on the server? Which is easier to administrate? Pro and con?
Thanks.
Source: http://www.livejournal.com/community/mysql/45679.html
-
insert + enum
Date: 01/09/05
Keywords: php, mysql, sql
hey everyone, my name is Phil. i started learning PHP in september and about a month ago started learning mysql and how the two work together.
i have a question regarding the enum data type. any time i try to create a table which has a field which i'd like to specify as enum, i get a syntax error. i've checked the manual and am not sure what i'm doing wrong. here's an example:
create table records(record_id int auto_increment, user varchar(50) not null, status enum("open","closed"), primary key(record_id));
what i've ended up having to do is create the first half of the table, then put in each of the enum fields one at a time, then do the rest. could someone tell me what i'm doing wrong?
thanks
Source: http://www.livejournal.com/community/mysql/45246.html
-
Database Woes, x-posted in php...
Date: 12/29/04
Keywords: php, mysql, html, database, sql
Ok, I figured something out.
When I run the mysql query at a mysql or phpmyadmin prompt I get the proper results...
SELECT * FROM fff_news WHERE month(date)=month(now()) and year(date)=year(now()) ORDER BY date DESC
But when I throw this into an array with php's mysql_fetch_array or mysql_fetch_row everything will display except for the top entry which would be the last date entered into the database. If I add a new entry for yesterday, it won't show until I add a second entry for yesterday(and then that one won't show) or a new entry for today(which won't show).
Why is php mangling my query like this, it's rather simple...
snip...(and I cut out my html which semagic won't let me display as code anyway...)
} else {
$sql = 'SELECT * FROM fff_news WHERE month(date)=month(now()) and year(date)=year(now()) ORDER BY date DESC'; $result = mysql_query($sql); $row = mysql_fetch_array($result);
if (!$result) { echo(" Error performing query: " . mysql_error() . "\n"); exit(); }
while ($row = mysql_fetch_array($result)) { echo $row[date]; echo $row[pagetitle]; echo $row[content];
}
...snip...
Source: http://www.livejournal.com/community/mysql/45003.html
-
Upgrade question...
Date: 12/23/04
Keywords: mysql, sql, web
I upgraded my server today to 4.1.8, both MySQL and MySQL Max. I had to force it as there was a conflict with the client tools. My question is, where are all the client tools? I looked for upgrades on the MyS!QL site and couldn't find them.
Also, any reason why WebMin would still report the old version number?
Source: http://www.livejournal.com/community/mysql/44609.html
-
result of a stored procedure
Date: 12/15/04
Keywords: no keywords
Is it possible to get multirows result from a stored procedure? If yes, could you give me an example?
Source: http://www.livejournal.com/community/mysql/43998.html
-
orphans
Date: 12/07/04
Keywords: no keywords
i recently wrote a gift wish list for a friend, but i'm stuck on a odd situation.
i have 3 tables.
"users"
uid user
"lists"
lid uid title
"things"
tid lid title.
things belong to lists, lists belong to users. simple. users are free to create and destroy their own lists. (duh) and then add/delete things onto said lists.
in the begining, when a user deleted a list, i just deleted the list from the list table. (i wrote the list add/delete before i wrote the things stuff)
this left "things" tied to lists that didnt exist anymore. i fixed this later by also deleting any "things" that had that listid. but i still have a table full of orphans.
basicly what i'm looking for: SELECT things.tid FROM things, lists WHERE things.lid doesnt exist in lists.lid
Source: http://www.livejournal.com/community/mysql/43652.html
-
selecting only 1 match
Date: 12/07/04
Keywords: no keywords
It's easier to show what I'm trying to do than explain it in english so here goes.
create table table1( id int, name varchar(15) );
create table table2( id int, table1Id int, variation varchar(15) );
insert into table1 values(1, 'productA'); insert into table1 values(2, 'productB');
insert into table2 values(1, 1, 'gold'); insert into table2 values(2, 1, 'silver'); insert into table2 values(3, 1, 'platinum'); insert into table2 values(4, 2, 'gold');
Now, what I want is a query that will return all products but only one of the matching variations. The following query returns all matching variations.
select name, variation from table1 inner join table2 on table2.table1Id = table1.id
RETURNS productA, gold productA, silver productA, platinum productB, gold
What I want is: productA, gold --this can be any of the variations productB, gold
Is there an easy way to do this?
Source: http://www.livejournal.com/community/mysql/43317.html
-
inserted row ID
Date: 12/02/04
Keywords: php, mysql, database, sql
Hi guys, i'm a newbie to MYSQL, so excuse me for stupid questions :) I have the following question: For instance my database looks like -
+====+============+
| ID | SOME_VALUE |
+====+============+
| 10 | MY_VALUE |
+----+------------+
| | |
+----+------------+ ID - is auto_increment I use php and make MYSQL query to insert a new row (INSERT query) Is there any way to know inserted row ID?
----------------------- PS. If i use
(1) INSERT INTO ... ... ... VALUES ... ... ...; (2) SELECT LAST_INSERT_ID();
can i be 100% sure that another INSERT query (from another user) won't be inserted between (1) and (2) and the returned ID will be wrong?
Source: http://www.livejournal.com/community/mysql/43111.html
-
MySQL support for AMD64
Date: 11/30/04
Keywords: mysql, database, sql
I just received a box with the following specs:
Dual AMD64
8G ram
Two 3ware 2.4 terabyte RAID 5 arrays.
My company has been using Redhat for most of its production machines.
1. Does anyone have any success/horror stories running MySQL 4.0.x on RHES 3/ AMD64?
2. Does anyone have alternate recommendations for running MySQL databases in the terabyte range on AMD64?
Source: http://www.livejournal.com/community/mysql/42916.html
|