Cybercampbell
Cybercampbell

Reputation: 2606

wrap a mysql query in a php function and print results

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

Upvotes: 0

Views: 204

Answers (4)

ilyes kooli
ilyes kooli

Reputation: 12043

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;");
?>

Upvotes: 1

Thomas Wright
Thomas Wright

Reputation: 1309

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.

Upvotes: 1

user1086498
user1086498

Reputation:

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;

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

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.

Upvotes: 2

Related Questions