PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 12th, 2012, 05:10 PM
helenp helenp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383 helenp Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 Days 8 h 41 m 34 sec
Reputation Power: 0
Mysql query dont get var

Hi, think Im going nut.
I have a script that is working,
I am using nearly the same script on another page,
where I fill in a form and go to another page where the script is.
I have this:
PHP Code:
if (isset($_POST['enviar'])) {
$propiedad htmlspecialchars($_POST['propiedad']); 
echo 
"$propiedad";
$result mysql_query ("SELECT DATE_FORMAT(llegada, '%e %b %Y') as lleg,
 DATE_FORMAT(salida, '%e %b %Y') as sal,tiporeserva, propiedad, 
TO_DAYS('
$salida') - TO_DAYS('$llegada') as dias 
from bookings where (propiedad = '
$propiedad')
AND (('
$llegada' BETWEEN llegada AND date_sub(salida, interval +1 day)) 
or ('
$salida' BETWEEN date_sub(llegada, interval -1 day)
 AND salida) or (llegada <= '
$llegada
AND salida >= '
$salida') or (llegada >= '$llegada
AND salida <= '
$salida'))"$dbh);
if (
$row mysql_fetch_object($result)) {
    echo 
"no disponible";


echo 
"$propiedad";
}
?> 

In both echos the var $propiedad is printed correctly, however the result for the query echo $propiedad does not appear.
If I change this where (propiedad = '$propiedad') and write the value of the var the query works perfect.....I just dont get why the query donīt get the value in the var...
Thanks

Last edited by helenp : December 12th, 2012 at 05:15 PM.

Reply With Quote
  #2  
Old December 12th, 2012, 06:05 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
Try echoing out the sql and see if you can see something weird that way. Also try running the outputted sql and see if you get any hints.

Reply With Quote
  #3  
Old December 12th, 2012, 06:12 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 21 h 52 m 18 sec
Reputation Power: 1736
You should save the query in it own variable and validate that the query looks correct.

What is the value of $result? you have to test this value as it can be false.

For you information, it is recommended to use mysqli_query() or PDO::query(), as specified in the documentation

Reply With Quote
  #4  
Old December 12th, 2012, 06:26 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 43 m 31 sec
Reputation Power: 811
Hi,

why do you apply a HTML escaping function to $propiedad when you want to put it into an SQL query?? This makes absolutely no sense and will basically mangle your input -- maybe that's your problem? You need to get rid of the wrong escaping functions and apply the right one to each value: mysql_real_ecape_string() (if you didn't already do that). Or even better, forget the ancient mysql_ functions and choose one of the contemporary libraries.

What do you mean by "however the result for the query echo $propiedad does not appear"? Did you actually output the query like msteudel said, and did you actually see the variable being empty in the query? Or did you conclude that from something else?

Reply With Quote
  #5  
Old December 13th, 2012, 01:04 AM
helenp helenp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383 helenp Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 Days 8 h 41 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by msteudel
Try echoing out the sql and see if you can see something weird that way. Also try running the outputted sql and see if you get any hints.

Thanks, already done,
If I run the query in phpmyadminn changing the vars for a value I get a result.
The same if I change the var $propiedad to a value in the query in the php page I also get a result.
It just dont understand where propiedad = '$propiedad'
however it does understand where propiedad = 'value'


There are other queries in the same script using same var and those works perfect.
I have tried using $propiedad = @mysql_real_escape_string($_POST['propiedad']);
and it does not work with that either.

Last edited by helenp : December 13th, 2012 at 06:41 AM.

Reply With Quote
  #6  
Old December 13th, 2012, 03:57 AM
helenp helenp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383 helenp Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 Days 8 h 41 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by MrFujin
You should save the query in it own variable and validate that the query looks correct.

What is the value of $result? you have to test this value as it can be false.

For you information, it is recommended to use mysqli_query() or PDO::query(), as specified in the documentation


I just found out about the mysqli_query for some weeks ago, and as my website and intranet have hundreds of scripts, and I need to do an urgent update I dont have time to study it, I will do that when I finished.

The query is ok as if I change propiedad = '$propiedad' to propiedad = 'Casa_Test' the query works perfect.
If I echo the var $propiedad I get as a result Casa_Test.

I added a if die in the query and I get as an error:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/hosting/public_html/booking.php on line 57

However this is weird as I use this script on the web normally.
I do need fetch_object in this query wich is shortened to debug.

Last edited by helenp : December 13th, 2012 at 04:03 AM.

Reply With Quote
  #7  
Old December 13th, 2012, 04:02 AM
helenp helenp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383 helenp Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 Days 8 h 41 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by Jacques1
Hi,

why do you apply a HTML escaping function to $propiedad when you want to put it into an SQL query?? This makes absolutely no sense and will basically mangle your input -- maybe that's your problem? You need to get rid of the wrong escaping functions and apply the right one to each value: mysql_real_ecape_string() (if you didn't already do that). Or even better, forget the ancient mysql_ functions and choose one of the contemporary libraries.

What do you mean by "however the result for the query echo $propiedad does not appear"? Did you actually output the query like msteudel said, and did you actually see the variable being empty in the query? Or did you conclude that from something else?


I have changed to mysql_real_ecape_string() and just the same, as I said before, I dont have time to learn the new functions and the old still works, so I leave that for later if not necesarry at this moment.

What I mean with the result for the query echo $propiedad does not appear is that I dont get any records, ie I dont get the text in this if ($row = mysql_fetch_object($result)) {
echo "no disponible";

}

As I posted below the problem is for some very strange reason Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/hosting/public_html/booking.php on line 57
And the script is a copy from a script I use alreayd on web.

If I add var_dump($result);
I get NULL

Last edited by helenp : December 13th, 2012 at 04:33 AM.

Reply With Quote
  #8  
Old December 13th, 2012, 06:48 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 21 h 52 m 18 sec
Reputation Power: 1736
What do you get if you add the below code after you run the the query but before you call the fetch_obejct() function:
Code:
if (!$result) 
{
	die(mysql_error());
}

Reply With Quote
  #9  
Old December 13th, 2012, 06:55 AM
helenp helenp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383 helenp Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 Days 8 h 41 m 34 sec
Reputation Power: 0
Quote:
Originally Posted by MrFujin
What do you get if you add the below code after you run the the query but before you call the fetch_obejct() function:
Code:
if (!$result) 
{
	die(mysql_error());
}


I get no error, but not the expected value either

Reply With Quote
  #10  
Old December 13th, 2012, 07:57 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 43 m 31 sec
Reputation Power: 811
You need to output the query. I mean, we can speculate all day long on why inserting $propiedad doesn't yield the correct result. But when you look at the query (and post it here), you'll actually see what's happening.

I wouldn't be surprised if $propiedad contains unwanted characters (maybe spaces), which prevent an exact match.

Reply With Quote
  #11  
Old December 13th, 2012, 07:58 AM
helenp helenp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383 helenp Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 6 Days 8 h 41 m 34 sec
Reputation Power: 0
Thanks all, it works now.
What so stupid, on previos page I replaced the _ and forgot to add it after I printed it:

<?php $propiedad = str_replace("_", " ", $propiedad);?>
<?php print $propiedad;?><?php $propiedad = str_replace(" ", "_", $propiedad);?>

Reply With Quote
  #12  
Old December 13th, 2012, 08:09 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 43 m 31 sec
Reputation Power: 811
If you'd checked the query, you would have found that out in a second.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Mysql query dont get var

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap