Forum Moderators: coopster

Message Too Old, No Replies

result mysql as variable

         

helenp

10:13 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
I have an select on which the user choose property, arrival and departure dates for calculating the price,
works perfect.

But I need to put in some sort of error if the person choose dates higher than end date in the database,

What I want to do is this, but don´t work of course:
sep_fin is an datecolumn in the database

if ($salida>"sep_fin") {
echo "¡ There are no prices yet!";
}
elseif ($row = mysql_fetch_array($result)){

How to do this?

coopster

12:40 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If your column is of type
date
, the MAX() [dev.mysql.com] function should work just fine...
$sql = "SELECT MAX(mydate) AS sep_fin FROM table"; 
$row = mysql_fetch_assoc(mysql_query($sql));
if ($salida>$row['sep_fin']) {
echo "¡ There are no prices yet!";
} elseif ($row = mysql_fetch_array($result)) {
// process
}

helenp

1:37 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thanks,
but something is wrong, tried several things,
this don´t give any errors, but gives the message there are no prices yet, no matter which dates I choose:

$result = mysql_query ("SELECT etc,....

$row = mysql_fetch_assoc($result);
if ($salida>$row['sep_fin']) { echo "¡ There are no prices yet!"; }
elseif ($row = mysql_fetch_array($result)){

coopster

1:58 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, first off, the example I gave you places the query result (highest date found in the database) into $row. I just noticed that you already have a query that you must have run prior to this in your code that places its result set into a variable called $row. You will need to rename one of the two...

Other concerns...

Is the date column in your table indeed of column type

date
?
If so, the format of your variable $salida had better be 'yyyy-mm-dd' or your comparison is going to return unexpected results.

baertyp

3:08 pm on Nov 12, 2004 (gmt 0)

10+ Year Member



Typo in Coopster code sniplet?

Isn't it "mysql_fetch_array" instead of "mysql_fetch_assoc"? Or am I outdated? This should give an error though "... call to undefined function..."

Regards
Markus

coopster

3:55 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, mysql_fetch_assoc() [php.net] is a valid function, I use it once in awhile when I don't plan on using the numerical indexes. Otherwise I use mysql_fetch_array() [php.net].

helenp

6:08 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



oops, wrong post

helenp

6:10 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yeah, I thought it looked strange :)
actually I tried to put the max in the same query,
but now in an separate query works just fine.
thanks
$sql = "SELECT MAX(sep_fin) AS sep_fin FROM propiedad";
$rows = mysql_fetch_assoc(mysql_query($sql));
if ($salida>$rows['sep_fin']) { echo "¡ There are no prices yet!"; }
elseif ($row = mysql_fetch_array($result)){

helenp

1:39 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well I retake this, though there should be some better options, this way I need to change the code every time I change the dates in the database.
I have the dates for one year in the database.

This is the current code that works perfect:
$sql = "SELECT MAX(sep_fin) AS max, MIN(baja_inicio) AS min FROM propiedad";
$rows = mysql_fetch_assoc(mysql_query($sql));
if ($salida>$rows['max']) { echo "¡ There are no prices yet for choosen dates!"; }
if ($salida<$rows['min']) { echo "¡ You choosen an past date!"; }
elseif ($row = mysql_fetch_array($result)){

Below is what I want to do, even though the code is crazy :) lol
just to clarify...
$sql = "SELECT baja_inicio AND baja_fin AND media_inicio AND media_fin and enero_inicio AND enero_fin and alta_inicio AND alta_fin and
abril_inicio AND abril_fin and verano_inicio AND verano_fin and sep_inicio AND sep_fin FROM propiedad";
$rows = mysql_fetch_assoc(mysql_query($sql));
if ($salida>$rows) { echo "¡ There are no prices yet for choosen dates!"; }
if ($salida<$rows) { echo "¡ You choosen an past date!"; }
elseif ($row = mysql_fetch_array($result)){

i.e I want to say that if $salida is < or > than all of these columns in the database to display an error message,
could be done?