www.webdeveloper.com
+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 29
  1. #1
    Join Date
    Jul 2006
    Posts
    249

    Unhappy Can't use function return value in write context

    I have a function that's supposed to query mysql with a zip code, and generate an html <select> box with possible cities. Instead, it crashes the site and gives this error:

    Code:
    Fatal error: Can't use function return value in write context in /home/rahl/public_html/RVM/include/functions.php on line 298
    The content of the function is:

    PHP Code:
    function zip_code_city_handler($zip)
        {

        global 
    $zip;
        
        
    $query "SELECT * FROM zip WHERE zip = '$zip'";
        
        
    $result safe_query($query);
            
        if (
    mysql_num_rows($result) = 1)
            {
            
    $city_select "<select>";
            while(
    $row mysql_fetch_array($result))
                {
                
    $city_select .= "<option>".$row['city']."</option>";
                }
            
    $city_select .= "</select>";
            }
            else
            {
            
    $row mysql_fetch_array$result );
            
    $city_select $row['city'];
            }
            
            return 
    $city_select;
        } 
    I've never gotten this error before and I'm not really sure what it means...

    Any help is appreciated, as always.

    Thanks,

    Nick

  2. #2
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    It means that your function is returning an unspeakable value - make that unwritable value. True and false are examples of that.

    You have a coding error in the function:
    PHP Code:
    if (mysql_num_rows($result) = 1
    This resolves to a value assignment and not a condition test. Use "==" instead of "=".

  3. #3
    Join Date
    Jul 2006
    Posts
    249
    oops... That fixed the error. Thank you

    Now I'm getting:

    Code:
    # errorno=1064
    # error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
    # query=query

    I think this is causing the problem:

    PHP Code:
        if(empty($error))
            {
            
    $query mysql_query("UPDATE Users SET title='$title',last_name='$l_name',first_name='$f_name',zip='$zip' WHERE handle='$user_name'"
            or die(
    mysql_error());  
            
            
    safe_query($query); 
    Last edited by lightnb; 02-26-2007 at 10:44 AM.

  4. #4
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Change the function safe_query as follows:
    PHP Code:
    function safe_query ($query "")
        {
        if(empty(
    $query))
            {
            return 
    FALSE;
            }
        
    $result mysql_query($query)
            or die(
    "I've Failed You: <li>errorno=".mysql_errno()."<li>error=".mysql_error()."<li>query=".$query);
        return 
    $result;
        } 
    (Missing "$" in final "query".)

  5. #5
    Join Date
    Jul 2006
    Posts
    249

    Smile

    wow. how do you always find the needle in the haystack? (or the missing $ in 3000 lines of code)

    I now get:

    PHP Code:
    query=
    So I'm guessing the query got screwed up somewhere...

  6. #6
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    On second thought, I would change it to this:

    PHP Code:
    function safe_query ($query "") {
      
    $result FALSE;
      if (!empty(
    $query)) {
        
    $result = @mysql_query($query);
        if (!
    $result) {
          echo 
    "I've Failed You:<br>";
          echo 
    "<li>Script Name: " $_SERVER['PHP_SELF'];
          echo 
    "<li>Include File: " __FILE__;
          echo 
    "<li>errorno=" mysql_errno();
          echo 
    "<li>error="   mysql_error();
          echo 
    "<li>query= "  $query;
          exit;
        }
      }
      return 
    $result;


  7. #7
    Join Date
    Jul 2006
    Posts
    249
    Error now reads:


    Code:
    I've Failed You:
    # Script Name: /RVM/registration/register2.php
    # Include File: /home/rahl/public_html/RVM/include/functions.php
    # errorno=1064
    # error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
    # query= 1

  8. #8
    Join Date
    Jul 2006
    Posts
    249
    register2.php reads:
    PHP Code:
    <?PHP

    $location_ID 
    ="RVM Registration"// Set the location variable 

    // Include Database connection information 

    Require('../include/SQL_connect.php');

    // Include the shared registration functions

    Require('../include/functions.php');

    // Include the registration forms functions page

    Require('../registration/registration_forms.php');


    // Check to see if the form has been submited


    if (isset($HTTP_POST_VARS["submit"])) //if the form has been submited, do the following
        
    {
        
        
    $title $HTTP_POST_VARS["title"];
        
    $f_name $HTTP_POST_VARS["f_name"];
        
    $l_name $HTTP_POST_VARS["l_name"];
        
    $zip $HTTP_POST_VARS["zip"];    
        
        
    $ret_error form_validator_names($title$l_name$f_name$zip);
        
        
    registration_form_2();
            
        }else 
    //(if the form has not be submited)
        
    {
        
    $user_name $HTTP_GET_VARS["user_name"];
        
    registration_form_2(); //display the form
        
    }
        
    ?>

  9. #9
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Try this:
    PHP Code:
    function safe_query ($query "") {
      
    $result FALSE;
      if (!empty(
    $query)) {
        
    $result = @mysql_query($query);
        if (!
    $result) {
          echo 
    "I've Failed You:<br>";
          echo 
    "<li>Script Name: " $_SERVER['PHP_SELF'];
          echo 
    "<li>Include File: " __FILE__;
          echo 
    "<li>errorno=" mysql_errno();
          echo 
    "<li>error="   mysql_error();
          echo 
    "<li>query= "  $query;
          
    var_dump(debug_backtrace());
          exit;
        }
      }
      return 
    $result;


  10. #10
    Join Date
    Jul 2006
    Posts
    249
    Code:
    I've Failed You:
    # Script Name: /RVM/registration/register2.php
    # Include File: /home/rahl/public_html/RVM/include/functions.php
    # errorno=1064
    # error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1
    # query= 1array(2) { [0]=> array(4) { ["file"]=> string(48) "/home/rahl/public_html/RVM/include/functions.php" ["line"]=> int(217) ["function"]=> string(10) "safe_query" ["args"]=> array(1) { [0]=> &bool(true) } } [1]=> array(4) { ["file"]=> string(53) "/home/rahl/public_html/RVM/registration/register2.php" ["line"]=> int(29) ["function"]=> string(20) "form_validator_names" ["args"]=> array(4) { [0]=> &string(3) "Dr." [1]=> &string(4) "last" [2]=> &string(4) "nick" [3]=> &string(5) "23456" } } }

  11. #11
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Code:
    # array(2) {
    #   [0]=> array(4) {
    #     ["file"]=> string(48) "/home/rahl/public_html/RVM/include/functions.php"
    #     ["line"]=> int(217)
    #     ["function"]=> string(10) "safe_query"
    #     ["args"]=> array(1) {
    #       [0]=> &bool(true)
    #     }
    #   }
    #   [1]=> array(4) {
    #     ["file"]=> string(53) "/home/rahl/public_html/RVM/registration/register2.php"
    #     ["line"]=> int(29)
    #     ["function"]=> string(20) "form_validator_names"
    #     ["args"]=> array(4) {
    #       [0]=> &string(3) "Dr."
    #       [1]=> &string(4) "last"
    #       [2]=> &string(4) "nick"
    #       [3]=> &string(5) "23456"
    #     }
    #   }
    # }
    The calling line is Line 29 in register2.php with the values listed.

  12. #12
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by lightnb
    wow. how do you always find the needle in the haystack? (or the missing $ in 3000 lines of code)
    Probably because I make more mistakes than you do...

  13. #13
    Join Date
    Dec 2006
    Location
    Escazú (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    PHP Code:
    function safe_query ($query "") {
      
    $result FALSE;
      if (!empty(
    $query)) {
        
    $result = @mysql_query($query);
        if (!
    $result) {
          echo 
    "I've Failed You:<br>";
          echo 
    "<li>Script Name: " $_SERVER['PHP_SELF'];
          echo 
    "<li>Include File: " __FILE__;
          echo 
    "<li>errorno=" mysql_errno();
          echo 
    "<li>error="   mysql_error();
          echo 
    "<li>query= "  $query;
          echo 
    "<pre>";
          
    print_r(debug_backtrace());
          echo 
    "</pre>";
          exit;
        }
      }
      return 
    $result;

    It should make the display a bit more readable. Make sure that you only use this when testing...

  14. #14
    Join Date
    Jul 2006
    Posts
    249
    Line 29 reads:

    PHP Code:
    $ret_error form_validator_names($title$l_name$f_name$zip); 
    and the variables:

    Code:
    [0]=> &string(3) "Dr."
    #       [1]=> &string(4) "last"
    #       [2]=> &string(4) "nick"
    #       [3]=> &string(5) "23456"
    are correct as well.

    I don't see where it's getting "Query=1" though...

  15. #15
    Join Date
    Jul 2006
    Posts
    249
    PHP Code:
    I've Failed You:
    # Script Name: /RVM/registration/register2.php
    # Include File: /home/rahl/public_html/RVM/include/functions.php
    # errorno=1064
    # error=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '
    1' at line 1
    # query= 1

    Array
    (
        [0] => Array
            (
                [file] => /home/rahl/public_html/RVM/include/functions.php
                [line] => 220
                [function] => safe_query
                [args] => Array
                    (
                        [0] => 1
                    )

            )

        [1] => Array
            (
                [file] => /home/rahl/public_html/RVM/registration/register2.php
                [line] => 29
                [function] => form_validator_names
                [args] => Array
                    (
                        [0] => Dr.
                        [1] => last
                        [2] => nick
                        [3] => 23456
                    )

            )



    So does:

    Code:
    [args] => Array
                    (
                        [0] => 1
                    )
    mean that the safe_query function itself is setting the value to one?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles