Jump to content

Reading An SQL Database And Printing To Email


aquapan

Recommended Posts

Hi,

 

I got stuck recently attempting to read an sql database using php as I kept on getting the error:

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/***/public_html/***/includes/query_posts.php  on line 28

 

The idea was that I would create this php page that would read an vbulletin sql database and then pop off an email when a user is over certain post amounts of 500 and 1000. The idea being that the poster is rewarded for their efforts etc.

 

Here below is the code I am using with identifying site information trimmed of course using *** symbols - this version is for the 500 post limit being met or exceeded.

 

<?php

 

 

 

//Get variables from config.php to connect to mysql server

 

require 'config.php';

 

 

 

// connect to the mysql database server.

 

mysql_connect ('localhost', '***', '***');

 

 

 

//select the database

 

mysql_select_db('***_forums1') or die('Cannot select database');

 

 

 

//select statement for users

 

$sql = "SELECT username FROM user WHERE posts >='500'";

 

$result = mysql_query($sql);

 

$row = mysql_fetch_assoc($result);

$username = $row["username"];

 

 

//take the emails and add a comma to email and create an string

 

$list = implode(',' , $username);

echo $list;

 

$to = "***@***.com";

 

$subject = "These users have reached 500 posts "&$list;

 

 

 

//send the emails

 

//mail($to, $from, $subject);

 

 

 

?></p>

 

Now I can't see why this error would happen as I don't fully understand implode and its requirements.

 

Any help appreciated.

 

Thanks

Aqua

Link to comment
Share on other sites

<?php



//Get variables from config.php to connect to mysql server

require 'config.php';



// connect to the mysql database server.

mysql_connect ('localhost', '***', '***');



//select the database

mysql_select_db('***_forums1') or die('Cannot select database');



//select statement for users

$sql = "SELECT username FROM user WHERE posts >='500'";

$result = mysql_query($sql);

$username = array();
if(mysql_num_rows($result)>0) {
   while($row = mysql_fetch_assoc($result)) {
      $username[] = $row["username"];
   }
}

//take the emails and add a comma to email and create an string

$list = implode(',' , $username);
echo $list;

$to = "***@***.com";

$subject = "These users have reached 500 posts: " . $list;



//send the emails

//mail($to, $from, $subject);



?>

 

That should do what u want it to

Link to comment
Share on other sites

Wow  Aeroswat you are fast... thanks!

 

I will give this a try :)

 

Cheers

Aqua

 

Np. You are going to have to do a little to that mail function to get it to work but the code I have given you will create your list sufficiently. If you need help with the mail part let me know and I'll help ya with that too.

Link to comment
Share on other sites

You were right on that too... a little help is needed on the mail.

 

I thought $subject would throw detail into the subject but for whatever reason it throws it into the body of the email.

 

The from I had originally seemed to be ignored by the mail function and it just sent it direct from the server. Is there a way to define from as well?

 

I will also do some code adjustments as well as I am looking at this alerting when someone reaches 500 posts and then again when they reach 1000 posts. The idea being that this is triggered daily as a report via a cron job.

 

Cheers

Aqua

Link to comment
Share on other sites

You were right on that too... a little help is needed on the mail.

 

I thought $subject would throw detail into the subject but for whatever reason it throws it into the body of the email.

 

The from I had originally seemed to be ignored by the mail function and it just sent it direct from the server. Is there a way to define from as well?

 

I will also do some code adjustments as well as I am looking at this alerting when someone reaches 500 posts and then again when they reach 1000 posts. The idea being that this is triggered daily as a report via a cron job.

 

Cheers

Aqua

 

from should be included in your header (the fourth section of the mail function)

something like this

 

$header =  'From: SystemAdmin@yoursite.com' . "\r\n" .

  'Reply-To: no-reply@yoursite.com' . "\r\n" .

      'X-Mailer: PHP/' . phpversion();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.