The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Mysql query dont get var
Discuss Mysql query dont get var in the PHP Development forum on Dev Shed. Mysql query dont get var PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 12th, 2012, 05:10 PM
|
|
Contributing User
|
|
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383
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.
|

December 12th, 2012, 06:05 PM
|
 |
Contributing User
|
|
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712
 
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.
|

December 12th, 2012, 06:12 PM
|
 |
Lord of the Dance
|
|
|
|
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
|

December 12th, 2012, 06:26 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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?
|

December 13th, 2012, 01:04 AM
|
|
Contributing User
|
|
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383
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.
|

December 13th, 2012, 03:57 AM
|
|
Contributing User
|
|
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383
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.
|

December 13th, 2012, 04:02 AM
|
|
Contributing User
|
|
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383
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.
|

December 13th, 2012, 06:48 AM
|
 |
Lord of the Dance
|
|
|
|
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());
}
|

December 13th, 2012, 06:55 AM
|
|
Contributing User
|
|
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383
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
|

December 13th, 2012, 07:57 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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.
|

December 13th, 2012, 07:58 AM
|
|
Contributing User
|
|
Join Date: Sep 2004
Location: Marbella, Spain
Posts: 383
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);?>
|

December 13th, 2012, 08:09 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
If you'd checked the query, you would have found that out in a second. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|