Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

PHP QUERY TO REPLACE RULES
 
chiaraweb
Guru
 
Avatar
Total Posts:  721
Joined:  2008-03-21
Aviano ~ Italy
 

Hi All

I need to run in cron a php script that run a replace query to salesrule into my magento db.
Then now I have a rules.php file in root directory that contain this code:

<?php


$link 
mysql_connect('localhost''magento_shop''password')
or die(
"Could not connect");
mysql_select_db('magento_shop'$link) or die("Could not select magento");

//then perform your query
$result mysql_query('REPLACE INTO `salesrule` (`rule_id`, `name`, `description`, `from_date`, `to_date`, `coupon_code`, `uses_per_coupon`, `uses_per_customer`, `customer_group_ids`, `is_active`, `conditions_serialized`, `actions_serialized`, `stop_rules_processing`, `is_advanced`, `product_ids`, `sort_order`, `simple_action`, `discount_amount`, `discount_qty`, `discount_step`, `simple_free_shipping`, `times_used`, `is_rss`, `website_ids`) VALUES
.................. 1, '
by_percent', '0.0000', NULL, 0, 2, 0, 0, '1');'$link) or die("Query failed");


mysql_close($link);
?>

but if I run this script in browser… query is not applied.

Does anybody know where is mistake? or why this query is not applied?

the string for replace is created from phpmyadmin, maybe I have a mistake in php code
the file rules.php is chmod 777

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

What prints out in your browser? Anything? Just a blank page?

Not sure why that wouldn’t work.

You probably need to show your errors:

<?php


$link 
mysql_connect('localhost''magento_shop''password')
or die(
"Could not connect");
//mysql_select_db('magento_shop', $link) or die("Could not select magento");  this is redundant

//then perform your query
$result mysql_query('REPLACE INTO `salesrule` (`rule_id`, `name`, `description`, `from_date`, `to_date`, `coupon_code`, `uses_per_coupon`, `uses_per_customer`, `customer_group_ids`, `is_active`, `conditions_serialized`, `actions_serialized`, `stop_rules_processing`, `is_advanced`, `product_ids`, `sort_order`, `simple_action`, `discount_amount`, `discount_qty`, `discount_step`, `simple_free_shipping`, `times_used`, `is_rss`, `website_ids`) VALUES
.................. 1, '
by_percent', '0.0000', NULL, 0, 2, 0, 0, '1');'$link) or die("Query failed: "+mysql_error($link));


mysql_close($link);
?>

My 2nd theory is that the problem arises from the straight copy and paste from phpmyadmin.

Replace your ` marks with a plain text ‘ (just do a find and replace to find ` and replace with ‘ - a non-copied-and-pasted commas)

Also, wrap your query in double quotes “ because when you replace your copied and pasted single quotes PHP is going to interpret them as string enders/beginners.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Actually, the back ticks that he has are correct for MySQL.  Using a single quote in those places would be an error.  Now, replacing the start/end single quotes with double quotes might help him.

 
Magento Community Magento Community
Magento Community
Magento Community
 
chiaraweb
Guru
 
Avatar
Total Posts:  721
Joined:  2008-03-21
Aviano ~ Italy
 

Thanks both for helps

ok…

now I have this script:

<?php


$link 
mysql_connect('localhost''magento_shop''password')
or die(
"Could not connect");
mysql_select_db('magento_shop'$link) or die("Could not select magento");

$result mysql_query("my-query"$link) or die("Query failed: "+mysql_error($link));

mysql_close($link);
?>

than I changed ‘ with “ (initial and final query string)

but it doesn’t work and I have a blank page, no text with errors or other

also I tried to change ` with ‘

but the result is the same

I did not thought that create a query string could be so difficult grrr

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 
Lee Saferite - 20 September 2008 07:30 AM

Actually, the back ticks that he has are correct for MySQL.  Using a single quote in those places would be an error.  Now, replacing the start/end single quotes with double quotes might help him.

I usually don’t even use single quotes, except for values being entered (when appropriate). Perhaps that could be an issue.

For instance, mine would look more like:

$matchValue 'some string';

$query "SELECT something, something_else FROM aTable WHERE someString=".$matchValue;

$result  mysql_query($query);

It looks like I concatenated with a + in my example (previous post) instead of a period(.) by accident also rasberry

 
Magento Community Magento Community
Magento Community
Magento Community
 
chiaraweb
Guru
 
Avatar
Total Posts:  721
Joined:  2008-03-21
Aviano ~ Italy
 

Hi nikefido

sorry ... I’m lost

instead of ‘some string’ what I must put?

Do you mean that instead + I must put . (point?)

I’m not a php programmer… and obviously I have the problem of language (I’m italian)…

can you help me to finish my script?

Thank You so much
Chiara

 
Magento Community Magento Community
Magento Community
Magento Community
 
matponta
Member
 
Total Posts:  39
Joined:  2008-05-20
Italy
 

Ciao chiara.

Prova a sostituire
Try changing

$result mysql_query("my-query"$link) or die("Query failed: "+mysql_error($link));

con
with

if(!($result=mysql_query("SELECT ......",$link)))
       die(
mysql_error($link).'errore nella query');

Mattia

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top