This topic is locked
[SOLVED]

 Mysql query works in PHPadmin but not in program

4/27/2017 3:39:53 PM
PHPRunner General questions
M
Mark Kramer author

Hello..
I am stumped.....

I am trying to insert records that are not duplicates (why I'm using replace) into another table in another attached db. Everything works perfectly as it should in phpMyAdmin running the sql query.

My problem is when I try to run it in the "After record added" event, nothing happens.

Here is the code that I exported as php from phpMyAdmin :
$sql = "REPLACE INTO camp.Room_Ins_Global (Cabin,Date,Total_Score)\n"

. "\n"

. "SELECT\n"

. "CabinInfo.Cabincode,\n"

. "Room Inspection.Week Ending,\n"

. "Room Inspection.Total Score\n"

. "\n"

. "FROM Room Inspection\n"

. ", CabinInfo\n"

. "\n"

. "WHERE (Room Inspection.Week Ending >= now() - INTERVAL 15 DAY) AND (Room Inspection.Pass/Fail =\'Fail\')\n"

. "GROUP BY Room Inspection.Week Ending, Room Inspection.Total Score, Room Inspection.Pass/Fail\n"

. "ORDER BY Room Inspection.Week Ending DESC\n"

. "";
I have tried every combination I could think of but I am having a mental block on this one.. Any help, Ideas would be appreciated <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=24670&image=1&table=forumtopics' class='bbc_emoticon' alt=':)' /> I'm not sure what I'm missing. The code seems to check out
Thank you

Admin 4/27/2017

You are shooting yourself in the foot. PHP allows multiline strings so you can just use your query as is without extra quotes, dots or line breaks.

M
Mark Kramer author 4/27/2017



You are shooting yourself in the foot. PHP allows multiline strings so you can just use your query as is without extra quotes, dots or line breaks.


So.... This is the actual query:
REPLACE INTO inntel5_camp.Room_Ins_Global (Cabin,Date,Total_Score)

SELECT

CabinInfo.Cabincode,

Room Inspection.Week Ending,

Room Inspection.Total Score
FROM Room Inspection

, CabinInfo
WHERE (Room Inspection.Week Ending >= now() - INTERVAL 500 DAY) AND (Room Inspection.Pass/Fail ='Fail')

GROUP BY Room Inspection.Week Ending, Room Inspection.Total Score, Room Inspection.Pass/Fail

ORDER BY Room Inspection.Week Ending DESC
How do I integrate it like this?
@sql= REPLACE INTO inntel5_camp.Room_Ins_Global (Cabin,Date,Total_Score)
SELECT

CabinInfo.Cabincode,

Room Inspection.Week Ending,

Room Inspection.Total Score
FROM Room Inspection

, CabinInfo
WHERE (Room Inspection.Week Ending >= now() - INTERVAL 500 DAY) AND (Room Inspection.Pass/Fail ='Fail')

GROUP BY Room Inspection.Week Ending, Room Inspection.Total Score, Room Inspection.Pass/Fail

ORDER BY Room Inspection.Week Ending DESC
CustomQuery($sql);

M
Mark Kramer author 4/28/2017



So.... This is the actual query:
REPLACE INTO inntel5_camp.Room_Ins_Global (Cabin,Date,Total_Score)

SELECT

CabinInfo.Cabincode,

Room Inspection.Week Ending,

Room Inspection.Total Score
FROM Room Inspection

, CabinInfo
WHERE (Room Inspection.Week Ending >= now() - INTERVAL 500 DAY) AND (Room Inspection.Pass/Fail ='Fail')

GROUP BY Room Inspection.Week Ending, Room Inspection.Total Score, Room Inspection.Pass/Fail

ORDER BY Room Inspection.Week Ending DESC
How do I integrate it like this?
@sql= REPLACE INTO inntel5_camp.Room_Ins_Global (Cabin,Date,Total_Score)
SELECT

CabinInfo.Cabincode,

Room Inspection.Week Ending,

Room Inspection.Total Score
FROM Room Inspection

, CabinInfo
WHERE (Room Inspection.Week Ending >= now() - INTERVAL 500 DAY) AND (Room Inspection.Pass/Fail ='Fail')

GROUP BY Room Inspection.Week Ending, Room Inspection.Total Score, Room Inspection.Pass/Fail

ORDER BY Room Inspection.Week Ending DESC
CustomQuery($sql);

H
headingwest 4/28/2017

Couple of suggestions:

  • get rid of your single quotes inside your query unless wrapping a string
  • Remove spaces in column names (just never do this if it's too late some DB's allow square brackets [col name])
  • Remove the / from Pass/Fail (use a consistent table and column naming convention with letters only)
    For myself getting SQl to be readable...

    Use $sql =

    Use Double quotes around the entire thing

    Use single quotes inside the double quotes for strings

    Use . to concatenate
    EG:
    $sql = " select col1 from table1 where col2 = 'Pass' ";

    CustomQuery($sql);
    Or using a string variable $mystring
    $sql = " select col1 from table1 where col2 = '" . $mystring . "'";

    CustomQuery($sql);
    Not sure if this is the best way, but it works for me. For yours (unchecked) something like:
    $sql = "REPLACE INTO inntel5_camp.Room_Ins_Global (Cabin,Date,Total_Score)

    SELECT CabinInfo.Cabincode, RoomInspection.Week Ending, RoomInspection.Total Score

    FROM RoomInspection, CabinInfo

    WHERE (RoomInspection.WeekEnding >= now() - INTERVAL 500 DAY) AND (RoomInspection.PassFail =Fail)

    GROUP BY RoomInspection.WeekEnding, RoomInspection.Total Score, RoomInspection.PassFail

    ORDER BY RoomInspection.WeekEnding DESC";

    CustomQuery ($sql);

M
Mark Kramer author 4/28/2017



Couple of suggestions:

  • get rid of your single quotes inside your query unless wrapping a string
  • Remove spaces in column names (just never do this if it's too late some DB's allow square brackets [col name])
  • Remove the / from Pass/Fail (use a consistent table and column naming convention with letters only)
    For myself getting SQl to be readable...

    Use $sql =

    Use Double quotes around the entire thing

    Use single quotes inside the double quotes for strings

    Use . to concatenate
    EG:
    $sql = " select col1 from table1 where col2 = 'Pass' ";

    CustomQuery($sql);
    Or using a string variable $mystring
    $sql = " select col1 from table1 where col2 = '" . $mystring . "'";

    CustomQuery($sql);
    Not sure if this is the best way, but it works for me. For yours (unchecked) something like:
    $sql = "REPLACE INTO inntel5_camp.Room_Ins_Global (Cabin,Date,Total_Score)

    SELECT CabinInfo.Cabincode, RoomInspection.Week Ending, RoomInspection.Total Score

    FROM RoomInspection, CabinInfo

    WHERE (RoomInspection.WeekEnding >= now() - INTERVAL 500 DAY) AND (RoomInspection.PassFail =Fail)

    GROUP BY RoomInspection.WeekEnding, RoomInspection.Total Score, RoomInspection.PassFail

    ORDER BY RoomInspection.WeekEnding DESC";

    CustomQuery ($sql);


Awesome! and Thank you. I was able to figure out how to make it work last night then tweaked it this morning. I have been a long time hardware guy and decided to jump into the programming side about 2 years ago. I have learned a lot but have a long way to go. Sergey makes me work for an answer (Rightfully so) and I have more than once figured it out after a gentle kick in the pants from him, but it's so much easier when someone says "this is a better way" and tells you why. I know from the being an IT tech for so many years, we can't assume that it makes since to everyone just because it does to us. I've had no formal programming training and have learned it as I went along.
So Thanks to you and Sergey for the expertise . As I progress I will certainly pay it back to the Newbies that are greener than myself!