Web Hosting Talk







View Full Version : Having a bit of trouble with this code:


Liguidsoul
02-21-2004, 03:46 PM
Two things, one, there is an error. Secondly, I am trying to make it dispatch an email to every row in the database who's birthday is today. Could someone help with this? I know how to use the mail(); function, and I have already set up the query to find rows where today is their brithday. Just not sure what to do next.

<?

$todaymonth = date(n);
$todayday = date(j);

$dbcnx = mysql_connect("localhost", "username", "passwrd");
mysql_select_db("database");

$result = "SELECT FirstName LastName Email BirthMonth BirthDay FROM eclub WHERE BirthMonth=$todaymonth AND BirthDay=$todayday";

while ($row = mysql_fetch_array($result)) {
echo("<p>" . $row["Email"] . "</p>");
}

?>

The error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/livoniag/public_html/eclubcron.php on line 11

mpdaddy
02-21-2004, 04:40 PM
change this part

$result = "SELECT FirstName LastName Email BirthMonth BirthDay FROM eclub WHERE BirthMonth=$todaymonth AND BirthDay=$todayday";

to

$result = mysql_query ("SELECT FirstName LastName Email BirthMonth BirthDay FROM eclub WHERE BirthMonth=$todaymonth AND BirthDay=$todayday");

See if that gets you anywhere

Regards
Wayne

Liguidsoul
02-21-2004, 04:54 PM
No, still error :( Same one.

mpdaddy
02-21-2004, 05:20 PM
<?

$todaymonth = date(n);
$todayday = date(j);

$dbcnx = mysql_connect("localhost", "username", "passwrd");
mysql_select_db("database");

$result = mysql_query ("SELECT FirstName LastName Email BirthMonth BirthDay FROM eclub WHERE BirthMonth=$todaymonth AND BirthDay=$todayday");

var_dump (mysql_fetch_array ($result));
echo("<p>" . $result["Email"] . "</p>");
?>

I am pretty new to php, but hopefully this will get you somewhere. Looks logical to me.

W-S-Nexus
02-21-2004, 11:00 PM
Try adding(or die("[ Operation type ] : " . mysql_error());) to each mysql command... Put in your own Operation type for each die portion.... Should tell you where the error is occuring....

The error could be in the connection line, the select database line, or with the query itself..

cheers
Richard

Liguidsoul
02-22-2004, 02:07 AM
Ok, I did that, and the error is "Query Empty" for Line 11.

However, it should not be. I added a row in which the birthday is today's date (2/22). The query doesn't seem to find this row as a match. Any idea why?

MGCJerry
02-22-2004, 02:29 AM
Try this:


$result = "SELECT FirstName, LastName, Email, BirthMonth, BirthDay FROM eclub WHERE BirthMonth='$todaymonth' AND BirthDay='$todayday'";


Edit:
Note the single quotes. I notice many times this is the only way I can execute queries like this, so this is usually how I do it. I also heard it is a good start to help prevent SQL inection (not sure if true though).

I also just noticed there are no commas.

Edit2:
Looks like digitok posted when I was doing my first edit :stickout:

digitok
02-22-2004, 02:30 AM
You do not have any commas seperating the fields you wish to extract from the table.

You cannot just go
SELECT blah blah2 FROM table

It must be in the format
SELECT blah,blah2 FROM table

Liguidsoul
02-22-2004, 02:34 AM
Added comas and apostraphies as you noted. Same error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/livoniag/public_html/eclubcron.php on line 11

mg-
02-22-2004, 03:57 AM
did you check to make sure the tables you're requesting.. exist?
put quotes around the date("j"); stuff.

Burhan
02-22-2004, 06:08 AM
You need to do mysql_query() before you can fetch anything.

mysql_fetch_array() expects a MySQL result resource type, which is only returned by mysql_query().

In other words, in your script, you are asking for results when you haven't exectued the query.

You also need to use mysql_error() after you execute the query, don't assume it worked. Also, echo out your query before you execute it.

Re-paste your edited code again if you still have problems.

mg-
02-22-2004, 06:12 AM
I can't believe I didn't notice that o.O

Liguidsoul
02-22-2004, 07:45 PM
Ok, this is very strange. I fixed the problem in the original script. Now I'm having the SAME problem in a new script:

<?

$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("database");

$result = @mysql_query("SELECT WorkPhone FROM 2003");
if (!result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

while ($row = mysql_fetch_array($result)) {
echo("<p>" . $row["WorkPhone"] . "</p>");
}
?>

Error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/swiftglo/public_html/fixdb.php on line 12

If you need me to put that "debugging" code in there, could you specify what/where? Thanks!

Liguidsoul
02-22-2004, 08:10 PM
Update: Added extra debugging to the query, got this:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '2003' at line 1

Liguidsoul
02-22-2004, 08:25 PM
Ok, this is bullcrap. I figured it out, but listen to this... you will be amused.

SINCE the table name is all numbers (2003), the query does not work without writing `2003`. You would think they are APOSTRAPHE's... but they're not. It's the key next to the 1 key. Took me a good hour or so to figure that out. Only way I noticed it is because phpMyAdmin has a default query typed in when you go to the SQL tab, and I saw thsoe.

Thanks anyway for those who tried to look at this code for me.