php-general Digest 2 May 2008 09:13:31 -0000 Issue 5436
Topics (messages 273792 through 273800):
Re: mysql query and maximum characters in sql statement
273792 by: Waynn Lue
273795 by: Jim Lucas
273796 by: Chris
Re: check if any element of an array is not "empty"
273793 by: tedd
Re: Categories like wordpress
273794 by: Chris
problem with for loop
273797 by: Richard Kurth
273798 by: Nathan Nobbe
273799 by: Richard Kurth
Any Running Simple Ajax Sample for Php
273800 by: Heysem KAYA
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
Wouldn't using LOAD DATA INFILE be better than writing your own script?
On 5/1/08, Jim Lucas <[EMAIL PROTECTED]> wrote:
> Jim Lucas wrote:
> > Sanjeev N wrote:
> >> Hi,
> >> I have written a program which imports the tab delimited file and
> >> insert all
> >> the line from file to the mysql line by line.
> >> I am succeding in the above case. but problem with the above method is
> >> its
> >> taking to too much time while inserting into the database.
> >> The file's size will be more than 5000 lines.
> >>
> >> Then i tried to build a string of ; seperated queries. as follows
> >>
> >> for($i=1; $i<sizeof($array); $i++){
> >> $insert_string .= "insert into tablename (v1, v2..... v6)
> >> values('$array[$i][1]', '$array[$i][2]'..... '$array[$i][6]');";
> >> }
> >> if(!empty($insert_string)){
> >> mysql_query($insert_string, $conn) or die("query failed :
> >> ".mysql_errror());
> >> }
> >>
> >> Its throwing error saying check the manual for right syntax.....
> >>
> >> After investigating in some sites i come to know that its problem of
> >> limitations in query size.
> >>
> >> I also tried with "SET GLOBAL max_allowed_packet=30000000;"
> >> Then also its throwing the same error.
> >>
> >> Can anybody tell me how to fix this error and reduce the inserting
> >> time with
> >> a single statement instead of writing more insert statements
> >>
> >
> > You are probably looking for something like this.
> >
> > <?php
> >
> > if ( count($array) ) {
> > $insert_string = "INSERT INTO tablename (v1, v2..... v6) VALUES ";
> > $data = array();
> > foreach ( $array AS $row ){
> > $row_clean = array_map('mysql_real_escape_string', $row);
> > $data[] = "('{$row_clean[1]}',
> > '{$row_clean[2]}',.....'{$row_clean[6]}')";
> > }
> > $insert_string = join(', ', $data);
> > mysql_query($insert_string, $conn) or die("query failed :
> > ".mysql_errror());
> > } else {
> > echo "Nothing to insert";
> > }
> >
> > ?>
> >
>
> That would work, but will probably result in a query string that is too
> long.
>
> I'll redo the above to fix that.
>
>
> <?php
>
> # How often do you want to insert??
> $break_at = 100;
>
> # Initialize the counter
> $cnt = 0;
>
> # Initial insert string
> $insert_string = "INSERT INTO tablename (v1, v2..... v6) VALUES ";
>
> # if there is data, then process, otherwise skip it.
> if ( count($array) ) {
>
> $data = array();
>
> # Loop through data
> foreach ( $array AS $row ) {
>
> $cnt++;
>
> # Clean the result data
> $row_clean = array_map('mysql_real_escape_string', $row);
>
> # Build data string and push it onto the data array.
> $data[] = "('{$row_clean[1]}',
> '{$row_clean[2]}',.....'{$row_clean[6]}')";
>
> # Break and insert if we are at the break point
> if ( $cnt === $break_at ) {
>
> # Reset Counter
> $cnt = 0;
>
> # Run insert
> mysql_query($insert_string . join(', ', $data), $conn) or
> die("query failed : ".mysql_error());
>
> # Reset data array
> $data = array();
>
> } //if
>
> } //foreach
>
> # This should take care of any extra that didn't get processed in the
> foreach
> if ( count($data) ) {
>
> # Insert remaining data
> mysql_query($insert_string . join(', ', $data), $conn) or
> die("query failed : ".mysql_error());
>
> } //if
>
> } else {
>
> echo "Nothing to insert";
>
> } //if
>
> ?>
>
>
> --
> Jim Lucas
>
> "Some men are born to greatness, some achieve greatness,
> and some have greatness thrust upon them."
>
> Twelfth Night, Act II, Scene V
> by William Shakespeare
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
Waynn Lue wrote:
Wouldn't using LOAD DATA INFILE be better than writing your own script?
depends, does the data file match the table column for column?
--- End Message ---
--- Begin Message ---
Jim Lucas wrote:
> Waynn Lue wrote:
>> Wouldn't using LOAD DATA INFILE be better than writing your own script?
>>
>
> depends, does the data file match the table column for column?
Doesn't have to.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
By default, when no column list is provided at the end of the LOAD DATA
INFILE statement, input lines are expected to contain a field for each
table column. If you want to load only some of a table's columns,
specify a column list:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);
But load data infile requires extra mysql privileges.
--
Postgresql & php tutorials
http://www.designmagick.com/
--- End Message ---
--- Begin Message ---
At 3:36 PM -0500 4/30/08, afan pasalic wrote:
hi,
as a result of one calculation I'm receiving an array where elements
could be 0 or date (as string yyyy-mm-dd hh:ii:ss).
I have to check if any of elements of the array is "date" or if all
elements of the array is 0?
If I try array_sum($result) I'll get 0 no matter what (0 + $string = 0).
I know I can do something like:
foreach($result as $value)
{
if ($value !=0)
{
$alert = true;
}
}
or
if (in_array($result, '-'))
{
$alert = true;
}
but I was thinking if there is the function does that.
thanks for any help.
-afan
You can create an empty array and check to see if the difference via
array_diff.
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
Ryan S wrote:
> Hey,
> Am not really used to using the JOIN in SQL so am a bit confused as to what
> kind of data I need to enter into this table:
>
> image_category_mapping table:
> - image_id
> - category_id
It comes down to database normalization
(http://en.wikipedia.org/wiki/Database_normalization).
The idea behind database normalization is you only ever have one copy of
the data, you don't have to update multiple tables to reflect a change.
So you only store the image in the 'images' table:
create table images
(
image_id int auto_increment primary key,
image_name text,
......
);
For multiple categories to share a particular image, there needs to be a
link between a category and an image.
If you only ever want one image per category, you could put the image_id
in the categories table:
create table categories
(
category_id int auto_increment primary key,
category_name text,
image_id int,
.....
);
However the problem with that is if you want more than one image,
instead of adding another field to the categories table:
create table categories
(
category_id int auto_increment primary key,
category_name text,
image_id int,
image_id_two int,
.....
);
which means if you want a category to have 3 images, you need another
field and so on, so instead you create a separate table:
create table category_images
(
category_id int,
image_id int
);
which means you could have 10 images for that category and no db changes
are necessary.
So you create the image as normal (insert into images ...).
Then when you link it to a particular category, you insert into the
separate table:
insert into category_images (category_id, image_id) values (.....);
This also means if you update the image (eg change the name or alt-value
or whatever), it's automatically reflected for all categories.
How do you get an image for the particular category?
SELECT i.* from images i INNER JOIN category_images ci ON (image_id)
WHERE category_id='X';
That will give you (multiple if necessary) images for that category.
--
Postgresql & php tutorials
http://www.designmagick.com/
--- End Message ---
--- Begin Message ---
Way does my for loop not complete the task if there are 4 emails it only
process 3 emails through the foreach loop if there is 3 it only process 2
# Connect up
$host ="domain.com";
$port ="110";
$mailtype = "pop3";
$mailbox ="INBOX";
$username ="[EMAIL PROTECTED]";
$password ="boat1234";
$conn = @imap_open("{" . $host . ":" . $port . "/" . $mailtype .
"/notls}" . $mailbox, $username, $password);
$number=imap_num_msg($conn);
for($i = 1; $i <= $number; $i++) {
$file="C:\web\bouncehandler\eml\em$i";
imap_savebody($conn,$file,$i);
$file=file_get_contents("C:\web\bouncehandler\eml\em$i");
$multiArray = Bouncehandler::get_the_facts($file);
$EMAIL = $the['recipient'];
foreach($multiArray as $the){
switch($the['action']){
case 'failed':
$sql="UPDATE contacts SET emailstatus = 'Fatal-Bounced' WHERE
emailaddress = '$EMAIL'";
mysql_query($sql) or die("Invalid query: " . mysql_error());
break;
case 'transient':
$sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE
emailaddress = '$EMAIL'";
mysql_query($sql) or die("Invalid query: " . mysql_error());
break;
case 'autoreply':
$sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE
emailaddress = '$EMAIL'";
mysql_query($sql) or die("Invalid query: " . mysql_error());
break;
default:
//don't do anything
break;
}
}
}
--- End Message ---
--- Begin Message ---
On Fri, May 2, 2008 at 1:20 AM, Richard Kurth <[EMAIL PROTECTED]>
wrote:
> Way does my for loop not complete the task if there are 4 emails it only
> process 3 emails through the foreach loop if there is 3 it only process 2
>
> $file=file_get_contents("C:\web\bouncehandler\eml\em$i");
> $multiArray = Bouncehandler::get_the_facts($file);
have you checked $multiArray here to verify it has the number of emails you
expect there are to be processed ?
> $EMAIL = $the['recipient'];
> foreach($multiArray as $the){
> switch($the['action']){
> case 'failed':
> $sql="UPDATE contacts SET emailstatus = 'Fatal-Bounced' WHERE
> emailaddress = '$EMAIL'";
> mysql_query($sql) or die("Invalid query: " . mysql_error());
> break;
> case 'transient':
> $sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE emailaddress
> = '$EMAIL'";
> mysql_query($sql) or die("Invalid query: " . mysql_error());
> break;
> case 'autoreply':
> $sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE emailaddress
> = '$EMAIL'";
> mysql_query($sql) or die("Invalid query: " . mysql_error());
> break;
> default:
> //don't do anything
> break;
> }
have you done any logging, or error checking to see if one (or more) of the
emails isnt falling into the default case of the switch statement ?
-nathan
--- End Message ---
--- Begin Message ---
Nathan Nobbe wrote:
On Fri, May 2, 2008 at 1:20 AM, Richard Kurth
<[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
Way does my for loop not complete the task if there are 4 emails
it only process 3 emails through the foreach loop if there is 3 it
only process 2
$file=file_get_contents("C:\web\bouncehandler\eml\em$i");
$multiArray = Bouncehandler::get_the_facts($file);
have you checked $multiArray here to verify it has the number of
emails you expect there are to be processed ?
$EMAIL = $the['recipient'];
foreach($multiArray as $the){
switch($the['action']){
case 'failed':
$sql="UPDATE contacts SET emailstatus = 'Fatal-Bounced'
WHERE emailaddress = '$EMAIL'";
mysql_query($sql) or die("Invalid query: " . mysql_error());
break;
case 'transient':
$sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE
emailaddress = '$EMAIL'";
mysql_query($sql) or die("Invalid query: " . mysql_error());
break;
case 'autoreply':
$sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE
emailaddress = '$EMAIL'";
mysql_query($sql) or die("Invalid query: " . mysql_error());
break;
default:
//don't do anything
break;
}
have you done any logging, or error checking to see if one (or more)
of the emails isnt falling into the default case of the switch
statement ?
-nathan
Yes I have it is just no processing the last email. I have even printed
them out before the foreach and it always comes out one less that there
are emails.
It is downloading the correct amount and creating them in the directory
it is just not passing the last one on
--- End Message ---
--- Begin Message ---
Hi,
I would like to check each minute whether the user credit is finished and
update the relevant place on page. So I have downloaded however was not able
to run some sample ajax code.
Is there anyone who has such a simple code to implement?
Thanks,
Heysem Kaya
--- End Message ---