Forum Moderators: coopster

Message Too Old, No Replies

Insert information in database

insert information in database

         

madristaa

9:31 am on Nov 6, 2007 (gmt 0)

10+ Year Member



Hello friends i have created a indexer. It will accept .htm and .html or actual folders. It will display all the information of the .htm and .html files i.e. metatags and it will count the occurence of the words repeating in that file.

I want you to help me those information to put those information in database.

The database should contain four tables
1)files containing id_file,name,url -->3 columns
2)file_word containing id_file,id_word-->2 columns
3)words containing id_word,word --->2 columns
4)meta_info containing id_file,type,content-->3 columns

I am attaching my code for further queries.

<?PHP
function recursive_directory($dir)
{
$arr = array();
if ($hld = opendir($dir))
{
while (false!== ($file = readdir($hld)))
{
if ($file!= "." && $file!= "..")
{
if (is_dir($dir. "/" . $file))
{
$arr = array_merge($arr, recursive_directory($dir. "/" . $file));
}
else
{
if((ereg(".html" , $file)) ¦¦ (ereg(".htm",$file)))
{
$path = $dir . "/" . $file;
$arr[] = realpath($path);
}
}
}
}
closedir($hld);
}
return $arr;
}
function superExplode($string,$st)
{
$i=0;
$arr[$i++]= strtok($string,$st);
while($tok = strtok($st))
$arr[$i++] = $tok;
return $arr;
}
function count_word($url)
{
if($file=file($url))
{
$tags = get_meta_tags($url);
echo "<h1>Meta Information</h1> ";
if(count($tags)!=0)
{
echo "Description: ".$tags['description']."<br>";
echo "Author: ".$tags['author']."<br>";
echo "Keywords: ".$tags['keywords']."<br>";
echo "Generator: ".$tags['generator']."<br>";
}
else
{
echo "No Meta Data<br>";
}

$fetch = implode('',$file);
$fetch = strtolower(strip_tags($fetch));
$arr_tok = superExplode($fetch,"\" ' : , ( ) { } [ ] ; #? .! \n -");
sort($arr_tok);

$temp = (array_count_values($arr_tok));
echo"<br>";
echo "<table border='0'><tr><th>Word</th><th>Count</th></tr>";
foreach($temp as $s => $r)
{
if($s!="")
echo "<tr><td>$s</td><td>$r</td></tr>";
}
echo "</table>";
}
else
{
echo "Try Again";
}

}
if(isset($_GET['path']))
{
$path=$_GET['path'];
$array = recursive_directory($path);
for($i=0;$i<count($array);$i++)
{
echo "</br>";
echo"Path&nbsp;&nbsp;:";
echo $array[$i]."<br>";
count_word($array[$i]);
}

}

echo "<html>";
echo "<body>";
echo "<form action=\"test.php\" method=\"get\">";
echo "</br>";
echo "Path of: <input type=\"text\" name=\"path\"/>";

echo "<input type=\"submit\" value=\"Enter\"/>";
echo " </form>";
echo "</body>";
echo "</html>";

?>

PHP_Chimp

10:07 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What type of database are you using?
Assuming you are using MySQl (as everyone seems to be) then have a look at the database functions as there are examples of how to get your data into and out of that database.
[uk2.php.net...]

madristaa

10:14 am on Nov 6, 2007 (gmt 0)

10+ Year Member



I did it..but i am not able to insert properly...i m doing this since last 2 days....but i am not able to .....Can you please run the program so that u can have better idea what i want to insert in respective fields....i am not that much good in query....please..can you help me...

PHP_Chimp

10:27 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the database code you are using looking like?
Also what is that query producing in the database?
And am I correct in assuming it is a MySQL database?

madristaa

10:40 am on Nov 6, 2007 (gmt 0)

10+ Year Member



Yes it is in MYSQL Database.

Well i have design 4 tables. Well actually i have just displayed the file name ..dont know what to do know...

files table should contain 3 fileds
1)id_file
2)name
3)url

file_word table should contain 2 fileds
1)file_word
2)id_word

words table should contain 2 fields
1)id_word
2)word

meta_info should contain 3 fields
1)id_file
2)type
3)content

thanks for your help

PHP_Chimp

10:47 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC));

Thats from the PHP manual. What does your version of the same piece of code look like?
Are the tables already created, or do you just have a blank file with the correct name?

The MySQL SELECT [dev.mysql.com] statement should help you. There is a list on the right hand side of that page that gives a lot of the other commands you will need to use to populate the table with data.

madristaa

10:59 am on Nov 6, 2007 (gmt 0)

10+ Year Member



i have created the tables according to my requirements ..do suggest me whether should i make other design for tables or not?did you run the file?did you understand what the file is performing?

PHP_Chimp

11:23 am on Nov 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I havent run the file, but have looked through the code. So I think I understand what you want.

In brief summary (in case I have it wrong) -
You want to populate the table you have with data. mysql_query [php.net] and the INSERT statement will do that.
You may need to create the tables. Another mysql_query will do that.
You may need to retrieve data from the tables. mysql_query along with a few other functions will do that.

So it seems that you need assistance with the SQL statements i.e. you want the script written for you.
There is something about homework posts in the charter [webmasterworld.com] so I dont mind helping, but im not writing the complete script for you.
So if you can post what you have then im sure that people will be willing to help you with the errors that there may be.

madristaa

10:40 pm on Nov 6, 2007 (gmt 0)

10+ Year Member



i dont know how to write the insert query for it...i mean how can i write a insert query which will affect the 4 tables....some fields are foreign and primary key in some tables....thats why i told you to help me....any way thanks for your help .....

d40sithui

10:51 pm on Nov 6, 2007 (gmt 0)

10+ Year Member



u probably will need more than one query to affect four tables.
suggest you write your loops around your insert queries and run them when you've attained enough data for a table.
your code is long and the way its presented is not structured so i am hesitant to try and decipher all your variables, otherwise i can give u a better response.

sample insert into table "users", which has 3 fields - id, name, password:

<?
$query = "insert into users (id, name, password) values ($id, '$name', '$password')";
$result = mysql_query($query);
//success
if(mysql_affected_rows()){
echo "inserted!";
}
//failure
else{
echo "failed to insert";
}

?>

madristaa

10:19 am on Nov 7, 2007 (gmt 0)

10+ Year Member



i dont know what you mean by "code not structured properly". you putting me in dilemma..putting more than one query in loop....kindly if your free than please write me query for my code which will affect 4 tables..thanks

madristaa

10:20 am on Nov 7, 2007 (gmt 0)

10+ Year Member



i think id_file and id_word will be auto incremented...

d40sithui

5:14 pm on Nov 7, 2007 (gmt 0)

10+ Year Member



1)looks like u have a firm grasp on php programming. i think if you're capable of writing this complex coding with usage of recursive functions and multilevel loops, inserting into the db should be cake. i dotn see how this would present a problem.
2)due to my good-hearted nature =), i actually tried running your script and creating some junk hmtl files to see what it does. interesting application.
3) inserting for each table. id_file is unique for files table it looks like, so somewhere in your script you will have to save this id in order to reuse it again into other tables where it is not unique.
you can use the "SELECT LAST_INSERT_ID()" to get the last inserted id(autoincremented).

i)files (id_file, name, url)
-you can have an insert query for this in the last else if statement in the function recursive_directory():
if((ereg(".html" , $file)) ¦¦ (ereg(".htm",$file))) {
/* insert statement for table files here */

-or u can have the insert query in the last if statement (if isset($_GET['path'])...

ii) file_word (id_file, id_word), and words (id_word, word)
-in your foreach statement in the function count_word() is where this insert query goes:
foreach($temp as $s => $r) {
if($s!=""){
/* insert statement for table file_word and words here */

iii)meta_info (id_file, type, content)
- this would also go in the count_word function inside the first nested if statement : if(count($tags)!=0) {
/* insert statement for table meta_info here */

madristaa

8:08 pm on Nov 7, 2007 (gmt 0)

10+ Year Member



vow...thanks for your help....i am currently looking at it...but how it will insert in table...i mean we have to give condition..that pressing the submit button than only it will insert in database...sorry i m bugging you...because of not all good in sql....how will it insert in 4 tables at a time...using simple phpMyAdmin i have created 4 tables in which id_file and id_word i put as auto incremented and i have named the database as indexer...so i dont have to write the query for creating the tables in my file...i just have to put the query for insertion in the file...

i dont know what to do know? can you please be more precise in your insert query....i mean can you write the whole 4 query for inserting in 4 tables....

do i have to write all this also?

$username = "";
$password = ";
$hostname = "";

#connect to database
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

#Select database
$db = mysql_select_db('indexer', $dbh) or die(mysql_error());

do i have to write all this for all 4 tables? please help me..

d40sithui

5:36 pm on Nov 9, 2007 (gmt 0)

10+ Year Member



you only have to connect once (before executing queries) in your script. to insert to eachof your table you need a query for each.
Remember to surround your strings with single quote or escape with double quotes when doing db queries. integers do not need quotes.

1)files containing id_file,name,url
$sql = "insert into files (id_file, name, url) values ($id, '$name', '$url')";
mysql_query($sql);

//this will insert into your files table.

2)file_word containing id_file,id_word
$sql = "insert into file_word (id_file, id_word) values($id_file, $id_word)";
mysql_query($sql);

//this will insert into file_word

3)words containing id_word,word
$sql = "insert into words (id_word, word) values ($id_word, '$word')";
mysql_query($sql);

4)meta_info containing id_file,type,content
$sql = "insert into meta_info (id_file, type, content) values ($id_file, \"$type\", \"$content\")";
mysql_query($sql);