Page 1 of 1

Warning Message

Posted: Mon Aug 19, 2002 1:24 pm
by merlinti
Hey All,

I'm a newbie and I'm getting the same Warning message:

Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site190/web/test01.php on line 45

Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site190/web/test01.php on line 53

Warning: Supplied argument is not a valid MySQL result resource in /home/sites/site190/web/test01.php on line 69



<?php


// Set the variables for the database access:
$Host = "localhost";
$User = "username";
$Password = "password";
$DBName = "databasename";
$TableName = "Stratosphere";

$CheckIn = "09/01/2002";
$CheckOut = "09/05/2002";

$Link = mysql_connect ($Host, $User, $Password);

$Query = "SELECT * from $TableName WHERE (Dates = '$CheckIn')";
$Query01 = "SELECT * from $TableName WHERE (Dates = '$CheckOut')";
$Query02 = "SELECT Price FROM $TableName WHERE (Dates BETWEEN '$CheckIn' AND '$CheckOut')";


$Result = mysql_db_query ($DBName, $Query, $Link);
$Result01 = mysql_db_query ($DBName, $Query01, $Link);
$Result02 = mysql_db_query ($DBName, $Query02, $Link);

// Create a table.
print ("<TABLE BORDER=1 WIDTH=\"20%\" CELLSPACING=1 CELLPADDING=1 ALIGN=LEFT>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP>Price</TD>\n");
print ("</TR>\n");

// Fetch the results from the database.
while ($Row = mysql_fetch_array ($Result)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP>$Row[Price]</TD>\n");
print ("</TR>\n");
print "<p>Check In Day: $$Row[Price].00<br></p>\n";
}

// Fetch the results from the database01.
while ($Row = mysql_fetch_array ($Result01)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP>$Row[Price]</TD>\n");
print ("</TR>\n");
print "<p>Check Out Day: $$Row[Price].00<br></p>\n";
}

print ("</TABLE>\n");


// Create a table.
print ("<TABLE BORDER=1 WIDTH=\"20%\" CELLSPACING=1 CELLPADDING=1 ALIGN=LEFT>\n");
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP>Price</TD>\n");
print ("</TR>\n");

while ($Row = mysql_fetch_array ($Result02)) {
print ("<TR ALIGN=CENTER VALIGN=TOP>\n");
print ("<TD ALIGN=CENTER VALIGN=TOP>$Row[Price]</TD>\n");
print ("</TR>\n");
}

print ("</TABLE>\n");

$a = array($Query02);
echo "sum(a) = ".array_sum($a)."\n";


mysql_close ($Link);

?>


I have this working fine on my computer testing environment, IIS 5.0 , PHP 4.21.

But when I upload it to my web host I get the Warnings.
I can see that they are using PHP 4.12, I'm not sure if that makes a difference.

thanks for any help.

Posted: Mon Aug 19, 2002 1:25 pm
by llimllib
which are lines 45, 53, and 69?

Posted: Mon Aug 19, 2002 1:29 pm
by merlinti
Hi

Here they are:

45: while ($Row = mysql_fetch_array ($Result)) {
53: while ($Row = mysql_fetch_array ($Result01)) {
69: while ($Row = mysql_fetch_array ($Result02)) {


thanks

Posted: Mon Aug 19, 2002 1:34 pm
by llimllib
quoting from the PHP manual about mysql_db_query:
Note: This function has been deprecated since PHP 4.0.6. Do not use this function. Use mysql_select_db() and mysql_query() instead.
Try fixing that and see if your error goes away.

Posted: Mon Aug 19, 2002 1:35 pm
by twigletmac
Instead of:

Code: Select all

$Result = mysql_db_query ($DBName, $Query, $Link); 
$Result01 = mysql_db_query ($DBName, $Query01, $Link); 
$Result02 = mysql_db_query ($DBName, $Query02, $Link);
try

Code: Select all

mysql_select_db($DBName) or die(mysql_error());
$Result = mysql_query ($Query) or die(mysql_error().'<br />'.$Query); 
$Result01 = mysql_query ($Query01) or die(mysql_error().'<br />'.$Query01); 
$Result02 = mysql_query ($Query02) or die(mysql_error().'<br />'.$Query02);
which should give you better error messages than the PHP generated one (because mysql_error() gives you the MySQL error message). You shouldn't use mysql_db_query() instead use mysql_select_db() and mysql_query().

Edit: slightly too slow on the mysql_db_query() thing (really should preview posts before submitting them) but the mysql_error() thingie still stands. Never hurts to have a lot of error trapping in your code.

Mac

Warning messages

Posted: Mon Aug 19, 2002 1:46 pm
by merlinti
Thanks, twigletmac.

That worked!!

I have another Newbie question.
I need to add up the values that are in that array.
The results I get are 34, 34, 34, 34, 34, 75.
I tried looking around for the SUM function, but I don't understand how to use it with an array.


thanks

Posted: Mon Aug 19, 2002 1:52 pm
by llimllib
at the beginning of the script, put

Code: Select all

$price = 0;
Then, After every mysql_fetch_array(), do:

Code: Select all

$price = $price + $Row&#1111;'Price'];
At the end, $price will contain the sum of the prices.

Also, you should use $Row['price'] in place of $Row[price], and conventionally you use lowercase names for variables, reserving uppercase for function and class names.

Posted: Mon Aug 19, 2002 3:01 pm
by merlinti
Hello,

Sorry for the newbie questions, but where exactly do I put the
$price = $price + $Row['Price']; ?

Is it directly after the closing "}" of the mysql_fetch_array?

Then do I put this at the end?: print ("$price");
or echo ($price);

Posted: Mon Aug 19, 2002 3:04 pm
by llimllib
a) Anywhere between mysql_fetch_array statements will work - anywhere before you overwrite the value of $Row

b) Both of the above would work. Try things before you ask about them! only by trying will you learn. We come here to help you when you're stuck, not to write your script for you.

Posted: Mon Aug 19, 2002 3:16 pm
by merlinti
Hi llimllib,

Thank you very much for the help.
I forgot to mention that I tried it and didn't work.
But I did as you said and got it working now!! YAY!! :D :lol:


thanks for all the help.

Posted: Tue Aug 20, 2002 5:39 am
by mikeq
$Query02 = "SELECT sum(Price) TOTAL_PRICE FROM $TableName WHERE (Dates BETWEEN '$CheckIn' AND '$CheckOut')";

This will get you the sum of prices in an aliased column named TOTAL_PRICE, just use $Row[TOTAL_PRICE] to get the value. It is probably better to have mysql calculate as it will be quicker.

Posted: Tue Aug 20, 2002 10:50 am
by merlinti
I'll try that too.

thanks :wink: