#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Marbella, Spain
    Posts
    381
    Rep 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.
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    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.
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,635
    Rep Power
    1945
    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
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Marbella, Spain
    Posts
    381
    Rep Power
    0
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Marbella, Spain
    Posts
    381
    Rep Power
    0
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Marbella, Spain
    Posts
    381
    Rep Power
    0
    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.
  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,635
    Rep Power
    1945
    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());
    }
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Marbella, Spain
    Posts
    381
    Rep Power
    0
    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
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Marbella, Spain
    Posts
    381
    Rep 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);?>
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    If you'd checked the query, you would have found that out in a second.

IMN logo majestic logo threadwatch logo seochat tools logo