Warning Message

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
merlinti
Forum Newbie
Posts: 13
Joined: Mon Aug 19, 2002 1:24 pm
Contact:

Warning Message

Post 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.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

which are lines 45, 53, and 69?
merlinti
Forum Newbie
Posts: 13
Joined: Mon Aug 19, 2002 1:24 pm
Contact:

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
Last edited by llimllib on Mon Aug 19, 2002 1:35 pm, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
merlinti
Forum Newbie
Posts: 13
Joined: Mon Aug 19, 2002 1:24 pm
Contact:

Warning messages

Post 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
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
merlinti
Forum Newbie
Posts: 13
Joined: Mon Aug 19, 2002 1:24 pm
Contact:

Post 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);
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post 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.
merlinti
Forum Newbie
Posts: 13
Joined: Mon Aug 19, 2002 1:24 pm
Contact:

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
merlinti
Forum Newbie
Posts: 13
Joined: Mon Aug 19, 2002 1:24 pm
Contact:

Post by merlinti »

I'll try that too.

thanks :wink:
Post Reply