wrap a mysql query in a php function and print results

Go To StackoverFlow.com

0

I have the following query that I ran on my database to remove some data:

delete subscriber, subscription from subscriber,subscription where subscription.status = 0 and subscription.snid=subscriber.snid;

But I now need to make the a php function that runs when I press a button called clean

then print out all the subscriber data that was deleted.

Not quitesure where to start with this.

this is my html so far:

<form id="form1" name="form1" method="post" action="">
Clean subscribers: 
<input type="submit" name="clean" id="clean" value="Clean" />
</form>

Any help or advice with this is very much appreciated.

C

2012-04-05 15:56
by Cybercampbell


1

Is abvious you made no effort! but I will answer you anyway.

<?php
$con = mysql_connect("serverUrl","login","password");
mysql_select_db("dbName", $con);

$result = mysql_query("SELECT * FROM subscriber, subscription where subscription.status = 0 and subscription.snid=subscriber.snid;");

while($row = mysql_fetch_array($result))
  {
  echo $row['subscriber.name']; //assuming you have a field {name} in your table
  echo "<br />";
  }
mysql_query("delete subscriber, subscription from subscriber,subscription where subscription.status = 0 and subscription.snid=subscriber.snid;");
?>
2012-04-05 16:05
by ilyes kooli
Thanks for this, I did make some effort and read quite a lot. Tis is all starting to make more sence to me now - Cybercampbell 2012-04-05 21:15
how do I do a check if $result is empty or not? if it is empty I wish to show a message that there is nothing to delete - Cybercampbell 2012-04-05 23:25
if (!$result){ echo 'nothing'; - ilyes kooli 2012-04-06 08:24


2

You'll need the button to submit a form to a handler page, the handler page would then run the query, and collect+print the data.

If you don't want to refresh the page (or have your users diverted into another page), you'll want to use Ajax.

That's where you start.

2012-04-05 15:59
by Madara Uchiha


1

First you'll need to select the data you're about to delete.

Then you'll need to delete it and return the selected rows.

$rows = array();
mysql_connect(...);
$res = mysql_query(...select query here...);
while($row=mysql_fetch_assoc($res)) {
    $rows[] = $row;
}
$res = mysql_query(...delete query here...);
return $rows;
2012-04-05 16:00
by NoName


1

You might not want to totally delete the subscriber. If I were you I would include a field named "deleted" or something along those lines, indicating whether or not the subscriber has been deleted. Then query according to whether or not that field is true or false.

2012-04-05 16:01
by Thomas Wright
That way, you don't have to rely on a deletion giving back the info you're looking for. shrugs I find it a very safe method to not actually delete data, but only mark it as "deleted". You never know when you'll need that information again - Thomas Wright 2012-04-05 16:03
Thanks Thomas, I'll think some more about this - Cybercampbell 2012-04-05 21:14
Ads