943,187 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1528
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 26th, 2010
0

Drop MySQL table in a PHP program

Expand Post »
I've been going round and round trying to sort this.

I have this in one of my programs:

PHP Syntax (Toggle Plain Text)
  1. drop table if exists temp2;
  2. rename table something to temp2;
  3. rename table temp to something;
  4. create table temp like something

and it's not working, though it will work when I manually paste it into the sql box on phpMyadmin

The problem is probably the first line: "drop table..." requires a confirmation, but as the program concerned is a cron job run once a day at 3am (to update a site), this is not available. I haven't been able to find any workaround despite lengthy searching. So I'm reduced to manually running these commands every morning, making the update late (which is probably not all that serious, but is a pain in the ass).

Anybody?
Similar Threads
Reputation Points: 12
Solved Threads: 8
Junior Poster
tiggsy is offline Offline
108 posts
since Jul 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
Please share your PHP code also.
Reputation Points: 17
Solved Threads: 23
Junior Poster
nileshgr is offline Offline
162 posts
since Aug 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
The code is:

PHP Syntax (Toggle Plain Text)
  1. //rename the old database
  2. $query5="drop table if exists temp2;
  3. rename table something to temp2;
  4. rename table temp to something;
  5. create table temp like something";
  6. //drop old database but not till testing is complete
  7. /*"drop table temp2";*/
  8. $result5 = mysql_query($query5);
Reputation Points: 12
Solved Threads: 8
Junior Poster
tiggsy is offline Offline
108 posts
since Jul 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
http://dev.mysql.com/doc/refman/5.0/en/drop-table.html

There is no such thing written as for confirmation. May be your user doesnt have DROP privelages.
Reputation Points: 17
Solved Threads: 14
Junior Poster in Training
shubhamjain1 is offline Offline
56 posts
since Oct 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
http://dev.mysql.com/doc/refman/5.0/en/drop-table.html

There is no such thing written as for confirmation. May be your user doesnt have DROP privelages.
Yeah exactly, even I was wondering how he's saying that it requires confirmation. But the contradiction is that, it works from PMA.

@tiggsy, did you connect to the mysql server with proper parameters in mysql_connect() ?
Reputation Points: 17
Solved Threads: 23
Junior Poster
nileshgr is offline Offline
162 posts
since Aug 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
Yes the user (me) does have all privileges, including drop

You try dropping a table - you will get a message "Are you sure?" I need to say "Yes" without being there!
Reputation Points: 12
Solved Threads: 8
Junior Poster
tiggsy is offline Offline
108 posts
since Jul 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
Click to Expand / Collapse  Quote originally posted by tiggsy ...
Yes the user (me) does have all privileges, including drop

You try dropping a table - you will get a message "Are you sure?" I need to say "Yes" without being there!
Friend, that question which PMA asks is a configurable option.

The code you shared, doesn't contain any mysql_connect() or you've not shared it here ?

You might be missing something in mysql_connect or the complete statment itself. Try again, also once connected, specify the connection resource explicitly in mysql_query()

PHP Syntax (Toggle Plain Text)
  1. $con = mysql_connect($server, $user, $pass, $db)
  2. $res = mysql_query($query, $con)
Reputation Points: 17
Solved Threads: 23
Junior Poster
nileshgr is offline Offline
162 posts
since Aug 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
This is a small portion of a larger program. The mysql connection is at the beginning before 4 other queries, all of which work fine. I've been writing php programs for several years, and normally have no problems sorting out problems. This one has beaten me.

How do i configure the option so it doesn't ask for confirmation? I'm on a cpanel host.
Reputation Points: 12
Solved Threads: 8
Junior Poster
tiggsy is offline Offline
108 posts
since Jul 2009
Jun 26th, 2010
0
Re: Drop MySQL table in a PHP program
Oh wth, here's the whole thing:

PHP Syntax (Toggle Plain Text)
  1. #!/usr/bin/php
  2. <?php
  3. function getad($url) {
  4. if (ini_get('allow_url_fopen') == '1') {
  5. $content = file_get_contents($url);
  6. if ($content !== false) {
  7. // do something with the content
  8. //my host doesn't allow this, so can't be bothered
  9. }
  10. else {
  11. $content = getcurlad($url);
  12. }
  13. }
  14. else {
  15. $content = getcurlad($url);
  16. }
  17. echo "$url: $content<br />";
  18. return $content;
  19. }
  20.  
  21. function getcurlad($url) {
  22. $ch = curl_init();
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. curl_setopt($ch, CURLOPT_HEADER, 0);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
  27. $content = curl_exec($ch);
  28. curl_close($ch);
  29. return $content;
  30. }
  31.  
  32. function GetRemoteLastModified($url) {
  33. $headers = get_headers($url,1);
  34. return $headers['Last-Modified'];
  35. }
  36.  
  37. $monthnames = array(1 => "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  38. function LongDateToU($thisdate) {
  39. /* Takes a date in the format Wed, 08 Jan 2003 23:11:55 GMT (RFC2822 format)
  40. and gets seconds since Unix Epoch
  41. **NOTE**: ignores timezone as I only use it for files on same server */
  42. $pieces = explode(" ",$thisdate);
  43. global $monthnames;
  44. $d = $pieces[1]+0;
  45. $m = $pieces[2];
  46. for ($q=1;$q<=12;$q++) {
  47. if ($m == $monthnames[$q]) {
  48. $m = $q;
  49. }
  50. }
  51. $y = $pieces[3]+0;
  52. $fulltime = $pieces[4];
  53. unset($pieces);
  54. $pieces = explode(":",$fulltime);
  55. $hr = $pieces[0]+0;
  56. $min = $pieces[1]+0;
  57. $sec = $pieces[2]+0;
  58. return date("U",mktime($hr, $min, $sec, $m, $d, $y));
  59. }
  60.  
  61. function savead($adname, $content) {
  62. $wfile = "./ads/".$adname;
  63. echo "creating $wfile<br />\n";
  64. $handle = fopen($wfile,'w');
  65. if (fwrite($handle,$content) === FALSE) {
  66. //notify me
  67. mail($adminEmail,"Failure creating ad file on DIP","On DIP record $couponId, the ad file could not be created\n\nRobot35 with love");
  68. }
  69. fclose($handle);
  70. }
  71.  
  72. $script_root = "./";
  73. $adminEmail = "nowone@nowhere.com";
  74. $allcoupons = array();
  75. $dbServer='localhost';
  76. $dbUser='ausername';
  77. $dbPass='apassword';
  78. //pass 0 - us site
  79. mysql_connect("$dbServer", "$dbUser", "$dbPass") or die("Could not connect");
  80. echo "Connected successfully<br />";
  81. //pick up and store the data
  82. $srchstr = array("1234567","3456789","1425852","u=4044","qrABC11IRks","0gAFgh2vvLQ","gnid=72311");
  83. $replstr = array("7654321","9876543","1420158","u=16578","edip0q*M7qs","eLem0q*M7qs","gnid=78241");
  84. for ($p=0;$p<2;$p++) {
  85. if ($p == 0) {
  86. mysql_select_db("user_ussite") or die("Could not select database ARN");
  87. }
  88. else {
  89. mysql_select_db("user_uksite") or die("Could not select database SMM");
  90. }
  91. echo "<br /><br />Database $p selected successfully<br />";
  92. $query="SELECT * FROM `coupons` where sector='handheld' and ExpiryDate>=CURRENT_DATE";
  93. $result=mysql_query($query);
  94. if ($result != FALSE) {
  95. $numrows=mysql_num_rows($result);
  96. }
  97. else {
  98. $numrows=0;
  99. }
  100. echo "$numrows records in database $p<br />";
  101. for ($j1=0;$j1<$numrows;$j1++) {
  102. $allcoupons[$j1]=mysql_fetch_assoc($result);
  103. $allcoupons[$j1]['CouponId'] = $allcoupons[$j1]['CouponId']+9000;
  104. $allcoupons[$j1]['CouponName'] = mysql_real_escape_string($allcoupons[$j1]['CouponName']);
  105. $allcoupons[$j1]['ShortName'] = mysql_real_escape_string($allcoupons[$j1]['ShortName']);
  106. $allcoupons[$j1]['CouponCode'] = mysql_real_escape_string($allcoupons[$j1]['CouponCode']);
  107. echo "<br />original url: ";
  108. echo $allcoupons[$j1]['CouponUrl'];
  109. $allcoupons[$j1]['CouponUrl'] = mysql_real_escape_string(str_replace($srchstr,$replstr,$allcoupons[$j1]['CouponUrl']));
  110. echo "<br />now: ";
  111. echo $allcoupons[$j1]['CouponUrl'];
  112. $allcoupons[$j1]['ImageUrl'] = mysql_real_escape_string(str_replace($srchstr,$replstr,$allcoupons[$j1]['ImageUrl']));
  113. $allcoupons[$j1]['ImageAlt'] = mysql_real_escape_string($allcoupons[$j1]['ImageAlt']);
  114. $allcoupons[$j1]['metaDesc'] = mysql_real_escape_string($allcoupons[$j1]['metaDesc']);
  115. $allcoupons[$j1]['tags'] = mysql_real_escape_string($allcoupons[$j1]['tags']);
  116. $allcoupons[$j1]['Live'] = 'no';
  117. $allcoupons[$j1]['TrafficBugged'] = 'no';
  118. $allcoupons[$j1]['ImageOptim'] = '';
  119. $allcoupons[$j1]['HandPicked'] = '0000-00-00';
  120. $allcoupons[$j1]['LastTweet'] = '0000-00-00';
  121. echo "$j1 read, ";
  122. #print_r ($allcoupons[$j1]);
  123. }
  124. mysql_select_db("user_handheld") or die("Could not select database DIP (1st)");
  125. echo "<br />Database DIP selected<br />";
  126. //make a temp database
  127. if ($p == 0) {
  128. $query2="CREATE TABLE temp LIKE coupons";
  129. $result2=mysql_query($query2);
  130. echo "<br />TEMP DATABASE CREATED<br />";
  131. }
  132. //add all the stuff to it
  133. for ($j=0;$j<$numrows;$j++) {
  134. if (isset($allcoupons[$j]['CouponId'])) {
  135. //record may have been deleted because DIP is not approved for this campaign
  136. $query3 = "insert into temp (`CouponId`, `CouponName`, `ShortName`, `CouponCode`, `CouponUrl`, `ImageUrl`, `ImageAlt`, `CouponAd`, `metaDesc`, `tags`, `sector`, `business`, `country`, `StartDate`, `StartTime`, `ExpiryDate`, `ExpiryTime`, `SomeDaysOnly`, `isAdult`, `Live`, `lastTweet`, `HandPicked`, `real_expiry`, `TrafficBugged`, `ImageOptim`) VALUES ({$allcoupons[$j]['CouponId']}, '{$allcoupons[$j]['CouponName']}', '{$allcoupons[$j]['ShortName']}', '{$allcoupons[$j]['CouponCode']}', '{$allcoupons[$j]['CouponUrl']}', '{$allcoupons[$j]['ImageUrl']}', '{$allcoupons[$j]['ImageAlt']}', '{$allcoupons[$j]['CouponAd']}', '{$allcoupons[$j]['metaDesc']}', '{$allcoupons[$j]['tags']}', '{$allcoupons[$j]['sector']}', '{$allcoupons[$j]['business']}', '{$allcoupons[$j]['country']}', '{$allcoupons[$j]['StartDate']}', '{$allcoupons[$j]['StartTime']}', '{$allcoupons[$j]['ExpiryDate']}', '{$allcoupons[$j]['ExpiryTime']}', '{$allcoupons[$j]['SomeDaysOnly']}', '{$allcoupons[$j]['isAdult']}', '{$allcoupons[$j]['Live']}', '{$allcoupons[$j]['lastTweet']}', '{$allcoupons[$j]['HandPicked']}', '{$allcoupons[$j]['real_expiry']}', '{$allcoupons[$j]['TrafficBugged']}', '{$allcoupons[$j]['ImageOptim']}')";
  137. $result3=mysql_query($query3);
  138. #echo "$j written: $query3,<br />";
  139. }
  140. }
  141. //clear data from previous pass
  142. unset($allcoupons);
  143. }
  144. $query1 = "select * from temp order by CouponAd";
  145. $result1 = mysql_query($query1);
  146. $numrows1 = mysql_num_rows($result1);
  147. echo "<br />$numrows1 rows in combined table<br />";
  148. //grab ads and modify content AND main links for cj/trd etc (where sites have individual ids)
  149. //pickup stuff we just stored to sort out site ids
  150. for ($j0=0;$j0<$numrows1;$j0++) {
  151. $row = mysql_fetch_array($result1);
  152. $couponId = $row['CouponId'];
  153. $adname = $row['CouponAd'];
  154. $country = $row['country'];
  155. if ($adname == '') {
  156. //notify me
  157. echo "{$couponId} has no ad!<br />";
  158. mail($adminEmail,"Missing ad file on DIP","On DIP record $couponId, the ad file name is missing\n\nRobot35 with love");
  159. }
  160. else {
  161. echo "Looking at ad $adname: ";
  162. $adsource = ($country == "UK") ? "http://www.somesite.co.uk/ads/" : "http://www.asomesite.com/ads/";
  163. $remotemoddate = LongDateToU(GetRemoteLastModified($adsource.$adname));
  164. echo "remote: ".$remotemoddate.", ";
  165. if (file_exists("./ads/".$adname)) {
  166. $localmoddate = filemtime("./ads/".$adname);
  167. }
  168. else {
  169. //dummy date
  170. $localmoddate = date("U",mktime(0,0,0,1,1,2000));
  171. }
  172. echo "local: ".$localmoddate.", ";
  173. if ($remotemoddate>$localmoddate) {
  174. echo "New ad needed<br />";
  175. mail($adminEmail,"Adcheck required on DIP","On DIP, the ad $adname is required/needs an update\n\nRobot35 with love");
  176. }
  177. else {
  178. echo "Existing ad ok<br />";
  179. }
  180. }
  181. }
  182. //rename the old database
  183. $query5="drop table if exists temp2;
  184. rename table coupons to temp2;
  185. rename table temp to coupons;
  186. create table temp like coupons";
  187. //drop old database but not till testing is complete
  188. /*"drop table temp2";*/
  189. $result5 = mysql_query($query5);
  190. mail($adminEmail,"DIP Update Complete","I have completed the daily update on DIP\n\nRobot35 with love");
  191. include "./coupon-update.php";
  192. echo "<br />\nUpdate complete!";
  193. ?>
Reputation Points: 12
Solved Threads: 8
Junior Poster
tiggsy is offline Offline
108 posts
since Jul 2009
Jun 26th, 2010
-1
Re: Drop MySQL table in a PHP program
I didn't ask the whole code man !
Just the database part.
Anyway, the code seems absolutely fine, I don't see any problems in it.
At the point where you set $dbUser, etc. use single quotes to specify that and remove double quotes while specifying variables in mysql_connect; though may not make any difference unless you have an special characters in the password (like $).
Reputation Points: 17
Solved Threads: 23
Junior Poster
nileshgr is offline Offline
162 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: email the username when it submit
Next Thread in PHP Forum Timeline: Problem with array_sum()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC