Need Help escaping an apostrophe in CSV -->SQL using PHP - php

OK I know I probably need to use a str_replace and I have tried $thequery = str_replace("'", "''", $thequery); but it didn't work. Please don't ding me on the code...I didn't write it. Each week we get a txt file done like a CSV with all our course offerings and we copy and paste it into a web page with the following code and it gets posted to an MSSQL table...however sometimes we have a course name with an ' and then it doesn't load and we have to manually remove the '. I need to escape the ' and can use some help.
if ($_POST) {
if(isset($_POST['semester'])) {
# Table name to upload to
$tablename = $_POST['semester'];
} else {
die("No table to use");
}
# Parsing of the text
$thequery = $_POST['sql'];
$thequery = str_replace("\"", "'", $thequery);
$thequery = str_replace(".00", "", $thequery);
$thequery = str_replace(".50", "", $thequery);
$thequery = str_replace("'Brien", " Brien", $thequery);
$thequery = str_replace("'Shaughnessy", " Shaughnessy", $thequery);
$thequery = str_replace("--", "10-01-01", $thequery);
$thequery = str_replace("WEB", "Online", $thequery);
$squery = explode("\n", $thequery);
$names = array();
$equery = array();
$i = 0;
foreach($squery as $newquery) {
$a = split('[*]', $newquery);
$final = substr($a[0], 0, -2);
$newid = $a[1];
$names[$i] = $newid;
$equery[$i] = $final;
$i++;
}
$scount = 0;
# Easiest way to redo the course list was to drop the table and re-insert it with the new values
mssql_query("DROP TABLE [dbo].[$tablename]") or die(mysql_error());
mssql_query("CREATE TABLE [dbo].[$tablename] (division CHAR(4) NULL, cid CHAR(11) NULL, cname CHAR(45) NULL, credits TINYINT NULL, days CHAR(7) NULL, day_M TINYINT NULL, day_T TINYINT NULL, day_W TINYINT NULL, day_R TINYINT NULL, day_F TINYINT NULL, day_S TINYINT NULL, sdate DATETIME NULL, edate DATETIME NULL, stime DATETIME NULL, etime DATETIME NULL, duration CHAR(5) NULL, building CHAR(3) NULL, room CHAR(4) NULL, method CHAR(12) NULL, instructor CHAR(40) NULL, secsyn INT NULL)") or die(mysql_error());
# Inserts the courses
while ($scount < count($equery)) {
mssql_query("INSERT INTO [dbo].[$tablename] (division, cid, cname, credits, days, day_M, day_T, day_W, day_R, day_F, day_S, sdate, edate, stime, etime, duration, building, room, method, instructor, secsyn) VALUES ($equery[$scount])") or die(mysql_error());
print($equery[$scount] . " has been entered.<br>");
$scount++;
}
# Quick hack to update the "Last Updated" with the current date
$date = date("M j Y g:iA");
mssql_query("UPDATE courses SET updated='$date' WHERE id='ABED140'") or die(mysql_error());
$nq = mssql_query("SELECT * FROM [dbo].[$tablename] WHERE method='Online'") or die(mysql_error());
$tnum = 0;
print("<br /><br />");
# Sets courses to Hybrids if they have a 'H' in the course ID
while($tnum < mssql_num_rows($nq)) {
$tcourse = mssql_result($nq,$tnum,"cid");
$ccode = explode(" ",$tcourse);
if(isset($ccode[1])) {
if (strpos($ccode[1], 'H') !== false) {
mssql_query("UPDATE [dbo].[$tablename] SET method='Hybrid' WHERE cid='$tcourse'") or die(mysql_error());
print("Updated " . $tcourse . " to be a Hybrid course.<br />");
}
}
$tnum++;
}
# Special cases for certain classes that need to be set as a Hybrid
# Simply add $sq[NEXT NUMBER] = "COURSE ID"; and it will set it to a Hybrid
$sq = array();
$sq[0] = "BIOL101 WH";
$sq[1] = "BIOL140 WH";
foreach ($sq as $nq) {
if(mssql_num_rows(mssql_query("SELECT * FROM [dbo].[$tablename] WHERE cid='$nq'")) > 0) {
mssql_query("UPDATE [dbo].[$tablename] SET method='Hybrid' WHERE cid='$nq'") or die(mysql_error());
print("Updated " . $nq . " to be a Hybrid course.<br />");
}
}
# Checking to make sure the online classes have a special_online table link
$cq = mssql_query("SELECT * FROM [dbo].[$tablename] WHERE method='Online' OR method='Hybrid'");
$cn = 0;
while($cn < mssql_num_rows($cq)) {
$ccid = mssql_result($cq,$cn,"cid");
$ncq = mssql_query("SELECT * FROM [dbo].[special_online] WHERE cid='$ccid'");
if(mssql_num_rows($ncq) == 0) {
mssql_query("INSERT INTO [dbo].[special_online] (cid,url) VALUES ('$ccid','http://dacc.blackboard.com')") or die(mysql_error());
print("Updated " . $ccid . " to have a online course link.<br />");
}
$cn++;
}
} else {
# Prints our form that shows when page is first loaded
print("<form action='' method='post'><select name='semester'>
<option value='2011SP'>2011SP</optoin>
<option value='2011SU'>2011SU</option>
<option value='2011FA'>2011FA</option>
<option value='2011FA'>2011WI</option>
<option value='2012SP'>2012SP</option>
<option value='2012SU'>2012SU</option>
<option value='2012FA'>2012FA</option>
<option value='2013SP'>2013SP</option>
<option value='2013SU'>2013SU</option>
<option value='2013FA'>2013FA</option>
<option value='2013WI'>2013WI</option>
<option value='2014SP'>2014SP</option>
<option value='2014SU'>2014SU</option>
<option value='2014FA'>2014FA</option>
</select><br><br>Insert the statement:<br><textarea name='sql' rows='10' cols='100'></textarea><br><input type='submit' value='Submit'></form>");
}

Try to replace ' with \ before you put that in your database:
Change this line:
$thequery = str_replace("\"", "'", $thequery);
to:
$thequery = str_replace("'", "\\", $thequery);
DEMO

Related

PHP MySQL search with multiple criteria

I have a search form in a website and would like to have several search terms which is input by the user to perform db search, terms as below:
Keywords
Property For (Sale, Rent...)
Property Type (Apartment, Terrace House...)
State
Min Price
Max Price
Here is script to perform search with above term's input
public function get_property_list_by_search($start, $per_page, $keyword, $prop_for, $min, $state, $ptype, $max, $mysqli)
{
if(empty($start) && empty($per_page))
{
return 0;
}
$start = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($start));
$per_page = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($per_page));
$keyword = $mysqli->real_escape_string(stripslashes($keyword));
$prop_for = $mysqli->real_escape_string(stripslashes($prop_for));
$state = $mysqli->real_escape_string(stripslashes($state));
$ptype = $mysqli->real_escape_string(stripslashes($ptype));
$min_price = self::num_clean($mysqli->real_escape_string($min));
$max_price = self::num_clean($mysqli->real_escape_string($max));
$t1 = '';
$t2 = '';
$t3 = '';
$t4 = '';
$t5 = '';
if(isset($keyword) && !empty($keyword)){
$t1 = " AND `proj_title` LIKE '%".$keyword."%' OR `proj_addr` LIKE '%".$keyword."%' OR `proj_area` LIKE '%".$keyword."%'";
}
if(isset($prop_for) && !empty($prop_for)){
$t2 = " AND `proj_for`='".$prop_for."'";
}
if(isset($state) && !empty($state)){
$t3 = " AND `state`='".$state."'";
}
if(isset($ptype) && !empty($ptype)){
$t4 = " AND `proj_cat`='".$ptype."'";
}
//min & max
if((isset($min_price) && !empty($min_price)) && (isset($max_price) && !empty($max_price))){
$t5 = " AND `price` BETWEEN '".$min_price."' AND '".$max_price."'";
}
//min only
if(!empty($min_price) && empty($max_price)){
$t5 = " AND `price` >= '".$min_price."'";
}
//max only
if(empty($min_price) && !empty($max_price)){
$t5 = " AND `price` <= '".$max_price."'";
}
$sql = $mysqli->query("SELECT * FROM `project` WHERE `status`='1' ".
$t1." ".$t2." ".$t3." ".$t4." ".$t5." ".
"ORDER BY `posted_date` DESC LIMIT ".$start.", ".$per_page);
if($sql->num_rows > 0){
return $sql;
}else{
return false;
}
}
The query output will something like:
SELECT * FROM `project`
WHERE `proj_title` LIKE '%keywords%'
OR `proj_addr` LIKE '%keywords%'
OR `proj_area` LIKE '%keywords%'
AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000'
(Datatype for price is DECIMAL(10,2), it stored value like 250000.00)
However, the returned results is not like expected (not accurate), its also will come out a result with price more than 600000 and project category which is out of '8' which is not fancy for the end user to searching in the website.
is there any way to refine on the query to perform more specific?
Instead of taking these variables you should use ".=" operator.
/* $t1 = '';
$t2 = '';
$t3 = '';
$t4 = '';
$t5 = '';
*/
$q = "SELECT * FROM `property` WHERE `status`='1' ";
// You need to enclose all **OR** logical tests in parenthesis.
// Moreover most of the usages of isset function are useless,
// as your are initializing many variables
if($keyword && !empty($keyword)){
$q .= " AND (`p_title` LIKE '%".$keyword."%' OR `address` LIKE '%".$keyword."%' OR `area` LIKE '%".$keyword."%')";
}
if($prop_for && !empty($prop_for)){
// If you are using double quotes you really don't need handle to concatenation.
$q .= " AND `p_for`='$prop_for'";
}
if($state && !empty($state)){
$q .= " AND `state`='$state'";
}
if($ptype && !empty($ptype)){
$q .= " AND `p_category`='$ptype'";
}
//min only
if($min_price && !empty($min_price)){
$q .= " AND `price` >= '".$min_price."'";
}
//max only
if($max_price && !empty($max_price)){
$q .= " AND `price` <= '$max_price'";
}
// When you are not using OFFSET keyword,
//the first number after LIMIT keyword should be the number of records
$q .= " ORDER BY `posted_date` DESC LIMIT $per_page , $start;";
$sql = $mysqli->query($q);
You're going to need parentheses.
SELECT * FROM `project` WHERE (`proj_title` LIKE '%keywords%' OR `proj_addr` LIKE '%keywords%' OR `proj_area` LIKE '%keywords%') AND `proj_for`='Sale' AND `state`='Somewhere' AND `proj_cat`='8' AND `price` BETWEEN '250000' AND '600000'
Without the parentheses it just has to match one of the criteria before the last OR.
if(isset($_SESSION['login']))
{
echo "<div align=\"right\"><strong> Home |
Signout|
Profile</strong></div>";
}
else
{
echo " ";
}
$con= mysql_connect("localhost","root","");
$d=mysql_select_db("matrimonial",$con);
$gender=$_POST['gender'];
$age1=$_POST['age1'];
$age2=$_POST['age2'];
$city=$_POST['city'];
$subcast=$_POST['subcast'];
$result=mysql_query("select * from matri where gender='$gender' and age between '$age1' and '$age2' and city='$city' and subcast='$subcast'");
if($gender && !empty($gender))
{
$result .= " AND `gender`='$gender'";
}
if($age1 && !empty($age1)){
$result .= " AND `age`='$age1'";
}
if($age2 && !empty($age2)){
$result .= " AND `age`='$age2'";
}
if($city && !empty($city)){
$result .= " AND `city`='$city'";
}
if($subcast && !empty($subcast)){
$result .= " AND `subcast`='$subcast'";
}
$result .= " select * from ";
$sql = $mysql->query($result);
how to run this code
On the price difference you should do a if the price if between the 2 values else only 1 value.

How to make one variable out of this?

Best,
I'm bussy making a website, but one thing holds me up..
I have to make a real long search query, and i have made this PHP code:
if($_GET['genre']) {
echo 'SELECT * FROM movies WHERE `genre1` = ';
foreach($_GET['genre'] as $genre)
{
$genres = array( "Actie", "Animatie", "Avontuur", "Documentaire", "Drama", "Erotiek", "Familie", "Fantasy", "Film", "Horror", "Komedie", "Misdaad", "Muziek", "Mystery", "Oorlog", "Roadmovie", "Romantiek", "Sciencefiction", "Thriller", "Western" );
if (!in_array($genre, $genres))
{
header('location: ?error=1');
}
echo " '".$genre."' OR `genre2` = '".$genre."'"; if(end($_GET['genre']) !== $genre)
{
echo ' OR `genre1` = ';
}
}
echo " AND `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';";
}
else
{
echo "SELECT * FROM movies WHERE `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';";
}
On a URL like this:
127.0.0.1/querygenerator.php?genre%5B3%5D=Avontuur&genre%5B4%5D=Documentaire&genre%5B6%5D=Erotiek&year1=1900&year2=2014
And it outputs something like this:
SELECT * FROM movies WHERE `genre1` = 'Avontuur' OR `genre2` = 'Avontuur' OR `genre1` = 'Documentaire' OR `genre2` = 'Documentaire' OR `genre1` = 'Erotiek' OR `genre2` = 'Erotiek' AND `year` > '1900' AND `year` < '2014';
So, my question is, how can i make one PHP variable out this whole, so I can run the query?
I can do this with file_get_contents but that's not so safe, I guess..
Thanks!
- Karim
Just concatenate to a string rater than echoing:
if($_GET['genre']) {
$sql = 'SELECT * FROM movies WHERE `genre1` = ';
foreach($_GET['genre'] as $genre) {
$genres = array( "Actie", "Animatie", "Avontuur", "Documentaire", "Drama", "Erotiek", "Familie", "Fantasy", "Film", "Horror", "Komedie", "Misdaad", "Muziek", "Mystery", "Oorlog", "Roadmovie", "Romantiek", "Sciencefiction", "Thriller", "Western" );
if (!in_array($genre, $genres)) {
header('location: ?error=1');
die();
}
$sql = $sql . " '".$genre."' OR `genre2` = '".$genre."'";
if(end($_GET['genre']) !== $genre) {
$sql = $sql . ' OR `genre1` = ';
}
}
$sql = $sql . " AND `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';";
} else {
$sql = "SELECT * FROM movies WHERE `year` > '".$_GET['year1']."' AND `year` < '".$_GET['year2']."';";
}
echo $sql;

Saving multiple filename (img_path) in one row separating with commas or |

The code I have worked except, it's only saving the first filename in the database. I want to be able to save them under img_path in one row separated by commas or | --->
img1.jpg, img2.jpg, img3.jpg
or
img123.jpg|img456.jpg|img789.jpg
Form:
<input type="file" name="img[]" multiple="" />
Here's the full code
<?php
$con = mysql_connect( 'localhost', '', '') or die('Could not connect to mysql server.' );
mysql_select_db('shop', $con) or die('Could not select database.');
$product=mysql_real_escape_string($_POST['product']);
$description=mysql_real_escape_string($_POST['description']);
$category=mysql_real_escape_string($_POST['category']);
$color=mysql_real_escape_string($_POST['color']);
$sizes=mysql_real_escape_string($_POST['sizes']);
$style=mysql_real_escape_string($_POST['style']);
$material=mysql_real_escape_string($_POST['material']);
$stock=mysql_real_escape_string($_POST['stock']);
$ws_price=mysql_real_escape_string($_POST['ws_price']);
$rt_price=mysql_real_escape_string($_POST['rt_price']);
$sp_code=mysql_real_escape_string($_POST['sp_code']);
foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name =$_FILES["img"]["tmp_name"][$key];
//$tmp_name = implode(",", $_FILES["img"]["tmp_name"][$key]);
$name = $_FILES["img"]["name"][$key];
$rand = rand(0000,9999);
$px = $rand . $name;
$px = preg_replace('/\s+/','_', $px);
$px = str_replace('&', 'and', $px);
$target = 'img/'.$category.'/';
if (is_dir($target) == false) {
mkdir($target, 0755);
echo "Directory Created</br>";
}
$u = $target . $px;
move_uploaded_file($tmp_name, $u);
echo "". $px.", ";
$a = array($px);
$x = implode("|", $a);
}
}
$sql = "INSERT INTO items (
product, description, category, color, sizes, style, material, stock, ws_price, rt_price, sp_code, img_path
)VALUES(
'$product', '$description', '$category','$color','$sizes','$style','$material','$stock','$ws_price','$rt_price','$sp_code','$x'
)";
$result = mysql_query($sql);
if($result){ echo("<br>Product Added!"); }
else{ echo("<br>Failed! Please try again.");}
mysql_close($con);
?>
I've used this
$x = implode(',', $px)
$sql = "INSERT INTO items (img_path)VALUES('$x');";
and this
$sql = "INSERT INTO items (img_path)VALUES('". implode(',', $px)."')";
it would always give me an error.
Warning: implode(): Invalid arguments passed in [...]
Here's the solution I've come up with taking some suggestions here.
Instead of using implode to save them in img_path in the same table, I've created a new one which is img to save the image path and link them using pid (product_id) from the products (id) table.
Database
CREATE TABLE IF NOT EXISTS `img` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`img_path` varchar(255) NOT NULL,
`pid` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Here's the edited code (currently working... until I break it again XD):
<?php
$con=mysqli_connect("localhost","user","password","db");
if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$product=mysql_real_escape_string($_POST['product']);
$description=mysql_real_escape_string($_POST['description']);
$category=mysql_real_escape_string($_POST['category']);
$color=mysql_real_escape_string($_POST['color']);
$sizes=mysql_real_escape_string($_POST['sizes']);
$style=mysql_real_escape_string($_POST['style']);
$material=mysql_real_escape_string($_POST['material']);
$stock=mysql_real_escape_string($_POST['stock']);
$ws_price=mysql_real_escape_string($_POST['ws_price']);
$rt_price=mysql_real_escape_string($_POST['rt_price']);
$sp_code=mysql_real_escape_string($_POST['sp_code']);
$sql = "INSERT INTO products (product, description, category, color, sizes, style, material, stock, ws_price, rt_price, sp_code)
VALUES('$product', '$description', '$category','$color','$sizes','$style','$material','$stock','$ws_price','$rt_price','$sp_code')";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
} echo "<p>Product Added! </p>";
foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name =$_FILES["img"]["tmp_name"][$key];
$name = $_FILES["img"]["name"][$key];
$rand = rand(0000,9999);
$px = $rand . $name;
$px = preg_replace('/\s+/','_', $px);
$px = str_replace('&', 'and', $px);
$px = str_replace("'", '-', $px);
$target = 'img/'.$category.'/';
if (is_dir($target) == false) {
mkdir($target, 0755);
echo "Directory Created</br>";
}
$u = $target . $px;
move_uploaded_file($tmp_name, $u);
echo "". $px."</br>";
mysqli_query($con,"INSERT INTO img (img_path, pid) VALUES ('$px', (SELECT MAX(id) FROM products))");
}
}
mysqli_close($con);
?>

xml to php and parse encoding error

I am parsing xml file from url(in code below), using file_get_contents() function, and simpleXML, to insert data into the table, i did well, but i have problem with encoding(russian words) i get this ->Черногория; file and database encoding is set to utf-8;
require_once 'mysql_connect.php';
/**
*
*
*/
error_reporting(E_ALL);
$sql = "CREATE TABLE IF NOT EXISTS `db_countries` (
`id` int(11) unsigned NOT NULL auto_increment,
`countrykey` varchar(255) NOT NULL default '',
`countryname` varchar(255) NOT NULL default '',
`countrynamelat` varchar(500) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
mysql_query($sql);
$data = file_get_contents("http://www2.turtess-online.com.ua/export/dictionary/countries/");
$xml = new SimpleXMLElement($data);
echo $xml->body->dictionary->element["countryName"];
foreach ($xml->body->dictionary->element as $element) {
$countryname = mysql_real_escape_string($element["countryName"]);
$countrynamelat = mysql_real_escape_string($element["countryNameLat"]);
$countrykey = $element["countryKey"];
if ($countrykey) {
$q = $insert = 'INSERT INTO db_countries (countrykey, countryname, countrynamelat) VALUES ("' . $countrykey . '", "' . $countryname . '", "' . $countrynamelat . '")';
mysql_query($q);
} else {
echo "not valid key of country";
}
}
Make sure you insert Unicode content as well, database charset is not doing any "automagic" conversion.
As an alternative, I would suggest utf8_encode($countryname) as in :
if ($countrykey) {
$q = $insert = 'INSERT INTO db_countries (countrykey, countryname, countrynamelat) VALUES ("' . $countrykey . '", "' . cp1251_to_utf8($countryname) . '", "' . $countrynamelat . '")';
mysql_query($q);
} else {
echo "not valid key of country";
}
update : indeed, the XML source file shows a Windows 1251 charset
UPDATE(2) : i tested the code against this nifty little function and it works at last :)
function cp1251_to_utf8($s)
{
if ((mb_detect_encoding($s,'UTF-8,CP1251')) == "WINDOWS-1251")
{
$c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
for($i=0; $i<strlen($s); $i++)
{
$c=ord($s[$i]);
if ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
elseif ($c>239) $t.=$c209.chr($c-112);
elseif ($c==184) $t.=$c209.$c209;
elseif ($c==168) $t.=$c208.$c129;
else $t.=$s[$i];
}
return $t;
}
else
{
return $s;
}
}
credit goes to Martin Petrov

Varchar values to numerical classification

I have a database containing three tables:
practices - 8 fields
patients - 47 fields
exacerbations - 11 fields
The majority of the fields in these tables are recorded in varchar format, other fields include integers, doubles and dates.
I have to transform this data into numerically classified data so that it can be used by a statistician to extrapolate any patterns in the data. To acheive this I will have to convert varchar fields into integers that represent the classification that string belongs to, an example being 'Severity' which has the following possible string values:
Mild
Moderate
Severe
Very Severe
This field in the patients table has a finite list of string values that can appear, other fields have an endless possibility of string values that cannot be classified until they are encountered by my database (unless I implement some form of intelligent approach).
For the time being I am just trying to construct the best approach to converting each field for all entries in each of the 3 tables to numeric values. The pseudo code I have in my head so far is as follows (it's not complete):
function profileDatabase
for each table in database
for each field that is of type varchar
select all distinct values and insert into classfication table for that field
end for
end for
function classifyDatabase
for each table in database
for each field that is of type varchar
// do something efficient to build an insert string to place into new table
end for
end for
Can someone suggest the best way of performing this process so that it is efficient giving that there are currently in excess of 100 practices, 15,000 patients and 55,000 exacerbations in the system. I have no need to implement this in PHP, build I would prefer to do so. Any pointers as to how to structure this would be great as I am not sure my approach the best approach.
This process will have to run every month for the next two years as the database grows to have a total of 100,000 patients.
Maybe http://dev.mysql.com/doc/refman/5.1/en/procedure-analyse.html will help to get an overview to find out how fields are used.
I have managed to build my own solution to this problem which runs in reasonable time. For anyone interested, or anyone who may encounter a similar issue here is the approach I have used:
A PHP script that is run as a cron job by calling php scriptName.php [database-name]. The script builds a classified table for each table name that is within the database (that is not a lookup table for this process). The setting up of each classification creates a new table which mimics the format of the base table but sets all fields to allow NULL values. It then creates blank rows for each of the rows found in the base table. The process then proceeds by analysing each table field by field and updating each row with the correct class for this field.
I am sure I can optimise this function to improve on the current complexity, but for now I shall use this approach until the run-time of the scripts goes outside of an acceptable range.
Script code:
include ("../application.php");
profileDatabase("coco");
classifyDatabase("coco");
function profileDatabase($database) {
mysql_select_db($database);
$query = "SHOW TABLES";
$result = db_query($query);
$dbProfile = array();
while ($obj = mysql_fetch_array($result)) {
if (!preg_match("/_/", $obj[0])) {
$dbProfile[$obj[0]] = profileTable($obj[0]);
}
}
return $dbProfile;
}
function profileTable($table) {
$tblProfile = array();
$query = "DESCRIBE $table";
$result = db_query($query);
while ($obj = mysql_fetch_array($result)) {
$type = substr($obj[1], 0, 7);
//echo $type;
if (preg_match("/varchar/", $obj[1]) && (!preg_match("/Id/", $obj[0]) && !preg_match("/date/", $obj[0]) && !preg_match("/dob/", $obj[0]))) {
$x = createLookup($obj[0], $table);
$arr = array($obj[0], $x);
$tblProfile[] = $arr;
}
}
return $tblProfile;
}
function getDistinctValues($field, $table) {
$distinct = array();
$query = "SELECT DISTINCT $field as 'value', COUNT($field) as 'no' FROM $table GROUP BY $field ORDER BY no DESC";
$result = db_query($query);
while ($obj = mysql_fetch_array($result)) {
$distinct[] = $obj;
}
return $distinct;
}
function createLookup($field, $table) {
$query = "CREATE TABLE IF NOT EXISTS `" . $table . "_" . $field . "`
(
`id` int(5) NOT NULL auto_increment,
`value` varchar(255) NOT NULL,
`no` int(5) NOT NULL,
`map1` int(3) NOT NULL,
`map2` int(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1";
db_query($query);
$distinct = getDistinctValues($field, $table);
$count = count($distinct);
foreach ($distinct as $val) {
$val['value'] = addslashes($val['value']);
$rs = db_query("SELECT id FROM " . $table . "_" . $field . " WHERE value = '" . $val['value'] . "' LIMIT 1");
if (mysql_num_rows($rs) == 0) {
$sql = "INSERT INTO " . $table . "_" . $field . " (value,no) VALUES ('" . $val['value'] . "', " . $val['no'] . ")";
} else {
$sql = "UPDATE " . $table . "_" . $field . " (value,no) VALUES ('" . $val['value'] . "', " . $val['no'] . ")";
}
db_query($sql);
}
return $count;
}
function classifyDatabase($database) {
mysql_select_db($database);
$query = "SHOW TABLES";
$result = db_query($query);
$dbProfile = array();
while ($obj = mysql_fetch_array($result)) {
if (!preg_match("/_/", $obj[0])) {
classifyTable($obj[0]);
//echo "Classfied $obj[0]\n";
}
}
}
function classifyTable($table) {
$query = "SHOW TABLES";
$result = db_query($query);
$dbProfile = array();
$setup = true;
while ($obj = mysql_fetch_array($result)) {
if ($obj[0] == "classify_" . $table)
$setup = false;
}
if ($setup) {
setupClassifyTable($table);
//echo "Setup $table\n";
}
$query = "DESCRIBE $table";
$result = db_query($query);
while ($obj = mysql_fetch_array($result)) {
if (preg_match("/varchar/", $obj[1]) && (!preg_match("/Id/", $obj[0]) && !preg_match("/date/", $obj[0]) && !preg_match("/dob/", $obj[0]))) {
$rs = db_query("
SELECT t.entryId, t.$obj[0], COALESCE(tc.map1,99) as 'group' FROM $table t
LEFT JOIN " . $table . "_$obj[0] tc ON t.$obj[0] = tc.value
ORDER BY tc.map1 ASC");
while ($obj2 = mysql_fetch_object($rs)) {
$sql = "UPDATE classify_$table SET $obj[0] = $obj2->group WHERE entryId = $obj2->entryId";
db_query($sql);
}
} else {
if ($obj[0] != "entryId") {
$rs = db_query("
SELECT t.entryId, t.$obj[0] as 'value' FROM $table t");
while ($obj2 = mysql_fetch_object($rs)) {
$sql = "UPDATE classify_$table SET $obj[0] = '" . addslashes($obj2->value) . "' WHERE entryId = $obj2->entryId";
db_query($sql);
}
}
}
}
}
function setupClassifyTable($table) {
$tblProfile = array();
$query = "DESCRIBE $table";
$result = db_query($query);
$create = "CREATE TABLE IF NOT EXISTS `classify_$table` (";
while ($obj = mysql_fetch_array($result)) {
if (preg_match("/varchar/", $obj[1]) && (!preg_match("/Id/", $obj[0]) && !preg_match("/date/", $obj[0]) && !preg_match("/dob/", $obj[0]))) {
//echo $obj[1]. " matches<br/>";
$create .= "$obj[0] int(3) NULL,";
} else {
$create .= "$obj[0] $obj[1] NULL,";
}
}
$create .= "PRIMARY KEY(`entryId`)) ENGINE=MyISAM DEFAULT CHARSET=latin1";
db_query($create);
$result = mysql_query("SELECT entryId FROM $table");
while ($obj = mysql_fetch_object($result)) {
db_query("INSERT IGNORE INTO classify_$table (entryId) VALUES ($obj->entryId)");
}
}
?>

Categories