Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
unserialize - help with this
Old 04-15-2009, 08:58 AM unserialize - help with this
Average Talker

Posts: 26
Trades: 0
What I am really trying to do is to search my database after a word is input into a form and bring back the links to the files but im not sure how to do this at this point.

At the moment I am just trying to test getting the data from the database. I had to serialize my data to get it into the database, but now when i call the data from the database i just get;

'0'] =dates.txt ['1'] =s:0:""; ['0'] =dates.txt ['1'] =s:6:"friday"; ['0'] =dates.txt ['1'] =s:6:"monday"; ['0'] =dates.txt ['1'] =s:7:"tuesday";


for example. I want to get rid of this s:7: at the front so I can do a query on the actual words as this isnt working. I know i need to use unserialize but I dont know how to use this within my code as I have tried various ways.

This is my code. Any help much appreciated

function echo_array(&$_some_array)
{ echo "<tr>";
foreach ($_some_array as $name => $value)
{ echo "<td><font face=arial size=2> ['$name'] =</td>";
echo "<td><font face=arial size=2> $value </td>"; }
echo "<td><font face=arial size=2> </td></tr>\n"; }

$query = "SELECT * FROM webdata";

echo "<table width = 1% border=0 cellspacing=0 cellpadding=0>";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "<p>'$query' returns $num rows:<p>\r\n";
while ($row = mysql_fetch_row($result))
echo_array($row,'$row');
echo "</table>\r\n";
applebiz89 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-15-2009, 10:56 AM Re: unserialize - help with this
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Your error have been to serialize the datas into the DB.
The serialization is a process that turns a data type into a string representation.

If you wanted to store a flexible data structure, you should have created a normalized DB schema, and stored each of the object values in the db individually.

The db cannot search (or with much difficulty, and very unoptimized) because the structure of the datas you saved is not usable as is, it needs to be processed again to be usable.

You should have 2 tables for the datas: 1 with the "header", another with the fields.
As PHP is not strongly typed, you can use a varchar type for each variables, like this
Code:
create table header(
  headerId integer not null auto_increment,
  primary key headerId
);

create table vals(
  headerFk integer not null,
  valName varchar(255) not null,
  val varchar(255) not null,
  primary key (headerFk, valName)
);
With this, you can create 1 header, and attach as many variable with their values as you need.

Looking for a particular value is a matter of a select in the vals table, which will return you an headerFk value, and allows you to get back every variables attached to that header.

Once you will have separated your structure that way, you won't need any data mangling or (un)serialization to lookup your datas.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-15-2009, 11:47 AM Re: unserialize - help with this
Average Talker

Posts: 26
Trades: 0
Thanks for your reply. I can see that working better, but however im not sure how to get my data into two tables like that. As before I was just putting it straight into one table by scanning through the directory and inserting every word and filename using this code.

PHP Code:
function opening($file)
{
$pattern "(\.txt$)| (\.php$)";
if(
eregi($pattern$file)){
 
$file_handle fopen($file"r");
 
$contents file($file);
 while (!
feof($file_handle)){
  
$data fgets($file_handle);
  
$words explode(' ' $data);
  
$duplicates array_unique($words);
  
sort($duplicates);
  foreach(
$duplicates as $word)
  {
   
$word serialize($word);
   
$sql "INSERT INTO webdata VALUES('$file','$word')";
   if (
mysql_query($sql))
   {
    print 
"successful <p>";
   }
  }
}
}
else {
echo 
"Cant open that file type <p>"; }
}
 
function 
getfiles($dirname=".")
{
$pattern "(\.txt$)| (\.php$)";
$file = array($file);
if(
$handle opendir($dirname)) {
 while(
false !== ($file readdir($handle))){
  if(
eregi($pattern$file) || is_dir($file))
  echo 
"$file <br />";
  
opening($file);
 
}
closedir($handle);
}
return(
$file);

applebiz89 is offline
Reply With Quote
View Public Profile
 
Old 04-15-2009, 04:56 PM Re: unserialize - help with this
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
In that case, I suggest that the db schema to be changed to
Code:
create table header(
  headerId integer not null auto_increment,
  fileName varchar(255) not null,
  primary key headerId
);

create table vals(
  headerFk integer not null,
  word varchar(255) not null,
  primary key (headerFk, word)
);
And as for your code:
PHP Code:
<?php
function opening($file){
  
$pattern "(\.txt$)| (\.php$)";
  if(
eregi($pattern$file)){
    
$file_handle fopen($file"r");
    
$contents file($file);
    
//we create the header if the file is opened ok and not empty
    
if(sizeof($contents)>0){
      
$query_header="insert into header (fileName) values ('$file')";
      
mysql_query($query_header);
      
//we get back the header id value
      
$headerId=mysql_insert_id();
    }
    while (!
feof($file_handle)){
      
$data fgets($file_handle);
      
$words explode(' ' $data);
      
$duplicates array_unique($words);
      
sort($duplicates);
      foreach(
$duplicates as $word){
        
$word serialize($word);
        
$sql "INSERT INTO vals (headerFk, word) VALUES($headerId, '$word')";
        if (
mysql_query($sql)){
          print 
"successful <p>";
        }
      }
    }
  }
  else{
    echo 
"Cant open that file type <p>"; }
  }
 
function 
getfiles($dirname="."){
  
$pattern "(\.txt$)| (\.php$)";
  
$file = array($file);
  if(
$handle opendir($dirname)) {
    while(
false !== ($file readdir($handle))){
      if(
eregi($pattern$file) || is_dir($file)){
        echo 
"$file <br />";
      }
    
opening($file);
    }
    
closedir($handle);
  }
  return(
$file);


?>
I haven't tested anything, it's all theoretical for now.
You may need to correct something here or there, but this should help you to go on the right track.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to unserialize - help with this
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.35330 seconds with 12 queries