Unix Technical Forum

INSERT works in phpMyAdmin but not in PHP

This is a discussion on INSERT works in phpMyAdmin but not in PHP within the MySQL forums, part of the Database Server Software category; --> I've got several SQL statements that run in succession. If I paste them into a phpMyAdmin SQL window, they ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:28 AM
billbois at gmail dot com
 
Posts: n/a
Default INSERT works in phpMyAdmin but not in PHP

I've got several SQL statements that run in succession. If I paste
them into a phpMyAdmin SQL window, they run quickly and correctly.
However, when I use them in a .PHP script, they run without error but
one fails to do it's job.

My PHP code is:

$sql = "TRUNCATE TABLE TransactionRptTrans;";
$dummy = $this->db->r_query($sql);

$sql = "TRUNCATE TABLE TransactionRptDetail;";
$dummy = $this->db->r_query($sql);

$sql = "INSERT INTO TransactionRptTrans(TransactionID, DomainID,
DomainName, Amount, ShipAmount, ShipSurcharge) SELECT t.TransactionID,
t.DomainID, d.DomainName, t.Amount, t.ShipAmount, d.ShipSurcharge FROM
Transaction t LEFT OUTER JOIN Domain d ON t.DomainID=d.DomainID WHERE
DATE_FORMAT(TransactionDate, '%Y-%m-%d') BETWEEN '".$from_date."' AND
'".$to_date."';";
$dummy = $this->db->r_query($sql);

$ssql = "INSERT INTO TransactionRptDetail(TransactionID, DomainID,
TransactionDetailID, ProductCode, Price, Qty, charge_fee,
merch_amount, fc_amount) SELECT t.TransactionID, t.DomainID,
td.TransactionID, td.ProductCode, td.Price, td.Qty, if(p.ReqShipping=1
AND p.FreeShipping=0, 1, 0) AS charge_fee, 0 AS merch_amount, 0 AS
fc_amount FROM TransactionRptTrans t INNER JOIN TransactionDetail td
ON t.TransactionID = td.TransactionID LEFT JOIN Product p ON
td.ProductID=p.ProductID;";
$dummy = $this->db->r_query($sql);

There's more after this but the last INSERT statement doesn't insert
anything, and no records in that table, it doesn't matter what happens
afterward.

I tried putting BEGIN TRANSACTION's and COMMIT's around each statement
and that made no difference. I also checked for errors and there
weren't any.

Can anyone see why the last INSERT doesn't insert?

Thanks,
Bill

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:28 AM
Jerry Stuckle
 
Posts: n/a
Default Re: INSERT works in phpMyAdmin but not in PHP

billbois at gmail dot com wrote:
> I've got several SQL statements that run in succession. If I paste
> them into a phpMyAdmin SQL window, they run quickly and correctly.
> However, when I use them in a .PHP script, they run without error but
> one fails to do it's job.
>
> My PHP code is:
>
> $sql = "TRUNCATE TABLE TransactionRptTrans;";
> $dummy = $this->db->r_query($sql);
>
> $sql = "TRUNCATE TABLE TransactionRptDetail;";
> $dummy = $this->db->r_query($sql);
>
> $sql = "INSERT INTO TransactionRptTrans(TransactionID, DomainID,
> DomainName, Amount, ShipAmount, ShipSurcharge) SELECT t.TransactionID,
> t.DomainID, d.DomainName, t.Amount, t.ShipAmount, d.ShipSurcharge FROM
> Transaction t LEFT OUTER JOIN Domain d ON t.DomainID=d.DomainID WHERE
> DATE_FORMAT(TransactionDate, '%Y-%m-%d') BETWEEN '".$from_date."' AND
> '".$to_date."';";
> $dummy = $this->db->r_query($sql);
>
> $ssql = "INSERT INTO TransactionRptDetail(TransactionID, DomainID,
> TransactionDetailID, ProductCode, Price, Qty, charge_fee,
> merch_amount, fc_amount) SELECT t.TransactionID, t.DomainID,
> td.TransactionID, td.ProductCode, td.Price, td.Qty, if(p.ReqShipping=1
> AND p.FreeShipping=0, 1, 0) AS charge_fee, 0 AS merch_amount, 0 AS
> fc_amount FROM TransactionRptTrans t INNER JOIN TransactionDetail td
> ON t.TransactionID = td.TransactionID LEFT JOIN Product p ON
> td.ProductID=p.ProductID;";
> $dummy = $this->db->r_query($sql);
>
> There's more after this but the last INSERT statement doesn't insert
> anything, and no records in that table, it doesn't matter what happens
> afterward.
>
> I tried putting BEGIN TRANSACTION's and COMMIT's around each statement
> and that made no difference. I also checked for errors and there
> weren't any.
>
> Can anyone see why the last INSERT doesn't insert?
>
> Thanks,
> Bill
>


Check the result of your query for errors.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:28 AM
billbois at gmail dot com
 
Posts: n/a
Default Re: INSERT works in phpMyAdmin but not in PHP

>
> Check the result of your query for errors.
>


Have done, and there aren't any. It's almost like the last query runs
before the previous one.

Thanks,
Bill

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:28 AM
Rik Wasmus
 
Posts: n/a
Default Re: INSERT works in phpMyAdmin but not in PHP

On Wed, 26 Sep 2007 21:31:12 +0200, billbois at gmail dot com
<bbois@hotmail.com> wrote:

> I've got several SQL statements that run in succession. If I paste
> them into a phpMyAdmin SQL window, they run quickly and correctly.
> However, when I use them in a .PHP script, they run without error but
> one fails to do it's job.
>
> My PHP code is:
>
> $sql = "TRUNCATE TABLE TransactionRptTrans;";
> $dummy = $this->db->r_query($sql);
>
> $sql = "TRUNCATE TABLE TransactionRptDetail;";
> $dummy = $this->db->r_query($sql);
>
> $sql = "INSERT INTO TransactionRptTrans(TransactionID, DomainID,
> DomainName, Amount, ShipAmount, ShipSurcharge) SELECT t.TransactionID,
> t.DomainID, d.DomainName, t.Amount, t.ShipAmount, d.ShipSurcharge FROM
> Transaction t LEFT OUTER JOIN Domain d ON t.DomainID=d.DomainID WHERE
> DATE_FORMAT(TransactionDate, '%Y-%m-%d') BETWEEN '".$from_date."' AND
> '".$to_date."';";
> $dummy = $this->db->r_query($sql);
>
> $ssql = "INSERT INTO TransactionRptDetail(TransactionID, DomainID,
> TransactionDetailID, ProductCode, Price, Qty, charge_fee,
> merch_amount, fc_amount) SELECT t.TransactionID, t.DomainID,
> td.TransactionID, td.ProductCode, td.Price, td.Qty, if(p.ReqShipping=1
> AND p.FreeShipping=0, 1, 0) AS charge_fee, 0 AS merch_amount, 0 AS
> fc_amount FROM TransactionRptTrans t INNER JOIN TransactionDetail td
> ON t.TransactionID = td.TransactionID LEFT JOIN Product p ON
> td.ProductID=p.ProductID;";
> $dummy = $this->db->r_query($sql);


Hmmm, shouldn't that be:
$dummy = $this->db->r_query($ssql);

$ssql != $sql
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:28 AM
billbois at gmail dot com
 
Posts: n/a
Default Re: INSERT works in phpMyAdmin but not in PHP

On Sep 26, 6:37 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Wed, 26 Sep 2007 21:31:12 +0200, billbois at gmail dot com
>
> > $dummy = $this->db->r_query($sql);

>
> Hmmm, shouldn't that be:
> $dummy = $this->db->r_query($ssql);
>


D'oh! OK, where's my dunce cap?

Thanks very much!
Bill

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:47 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com