www.webdeveloper.com
Results 1 to 15 of 29

Thread: Can't use function return value in write context

Hybrid View

  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
    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...

  7. #7
    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;


  8. #8
    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

  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
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by lightnb
    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);
    I don't understand what is going on here. Anyway I was right about a boolean. It is an UPDATE query and mysql_query is returning true. Then for some reason you are running that boolean as if it were another query. Also what is the point having that function if you never use the return value for anything.

  11. #11
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by bokeh
    I don't understand what is going on here. Anyway I was right about a boolean. It is an UPDATE ...
    Where did you find this snippet?

    EDIT: Found it... Didn't see it before... Should be:
    PHP Code:
    if(empty($error)) {
            
    $query "UPDATE Users SET title='$title',last_name='$l_name',first_name='$f_name',zip='$zip' WHERE handle='$user_name'";
            
    $result safe_query($query); 
    EDIT: Based on Bokeh's observation...
    Last edited by NightShift58; 02-26-2007 at 03:12 PM.

  12. #12
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736
    Quote Originally Posted by NightShift58
    PHP Code:
    safe_query($query); 
    Yeah, but you also need to pick up the return value and do something with it.

  13. #13
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,736

  14. #14
    Join Date
    Jul 2006
    Posts
    249
    I've switched to session variables to handle all of the passing of information from one page to another, and I have *most* everything working.

    I'm having trouble with my email confirmation page now, and I can't figure out why. It changes the database corectly, but it isn't printing the error or confirmation method.

    I've been working on it for a couple hours now and I can get the message to print under the table, but not inside it!


    This is the confirmation page:

    PHP Code:
    <?PHP
    session_start
    ( );
    $location_ID ="RVM Email Confirmation"// Set the location variable 

    // Include the registration forms functions page
    Require('../registration/registration_forms.php');

    // Include Database connection information 
    Require('../include/SQL_connect.php');

    // Include the shared registration functions
    Require('../include/functions.php');


    // get the confirmation code
    $code $HTTP_GET_VARS["confirmation_code"];

    check_confirmation_code($code);

    //$_SESSION["return_message"] = check_confirmation_code($code);



    ?>
    and this is the function that goes with it:

    PHP Code:
    function check_confirmation_code($code)
        {
        
    $query "SELECT * FROM Users WHERE confirmation_code = '$code'";
        
    $result safe_query($query);
        if (
    mysql_num_rows($result) > 1)
            {
            
    $return_message "AAAAAAAAHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!<br/> Something has gone teribly wrong. Please email technical support,
            or <a href='../registration/dc_request.php' class='inline_link'>request a new confirmation code here</a>."
    ;
            
            
    // draw the page
            
    registration_email_confirmation();    
            
            }
            elseif (
    mysql_num_rows($result) < 1)
            {
            
    $return_message "I'm sorry, but the confirmation code you have entered isn't valid.<br/>Please be sure you are using the
            code provided in your confirmation email, and that you are doing so within 24 hours of receiving it.
            If you need to, you may <a href='../registration/dc_request.php' class='inline_link'>request a new confirmation code here</a>."
    ;
        
            
    // draw the page
            
    registration_email_confirmation();    
            
            }
            elseif (
    mysql_num_rows($result) == 1)
            {
            
            
    $row mysql_fetch_array$result );
                    
            
    session_register("l_name");
            
    $_SESSION["l_name"] = $row['last_name'];
            
            
    session_register("title");
            
    $_SESSION["title"] = $row['title'];
            
            
    session_register("user_name");
            
    $_SESSION["user_name"] = $row['handle'];
            
                    
    session_register("email");
            
    $_SESSION["email"] = $row['email'];
            
            
    session_register("confirmation_code");
            
    $_SESSION["confirmation_code"] = $row['confirmation_code'];
            
            
    $return_message "Thank you, ".$_SESSION["title"]."&nbsp;".$_SESSION["l_name"].".<br/><br/><br/>
            You are now registered under the username: "
    .$_SESSION["user_name"]." Using the email address: ".$_SESSION["email"]."
            <br/><br/> From here, you can <a href='../interface/user_home.php' class='inline_link'>go to your homepage</a>.... more options when I acctually code somewhere to go."
    ;
            
            
    $query "UPDATE Users SET confirmation_code='0',confirmed='1' WHERE confirmation_code='".$_SESSION["confirmation_code"]."'";
            
    $result safe_query($query);
            
            
    // draw the page
            
    registration_email_confirmation();
            
            }
        
        } 

    and this is the function that prints the HTML, and *should* be printing out the $return_message variable inside of it:



    PHP Code:
    function registration_email_confirmation()
        {
        
        
    // Include the header information

        
    global $return_message;

        Require(
    '../include/header.php');
        
        echo 
    "
        
    <table align='center' background='http://www.rahlentertainment.com/RVM/Images/registration/register_generic.jpg' border='0' width='778px' height='379px'>

    <! ROW ONE >

        <tr height='75px'>
            <td>
            
            </td>
        </tr>
        
    <! ROW TWO >

        <tr height='35px'>
            <td>
            <Table border='0'>
                <tr>
                    <td width='240px'>
                    
                    </td>
                    <td width='450px' align='left'>
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
            </td>
        </tr>

    <!ROW THREE>
        <tr height='120px' valign='top'>
            <td>
                <table width='522px' align='right'>
                    <tr>
                        <td>
                        <div class='form_instructions'>
                        "
    .$return_message."
                        </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

    <! ROW FOUR >

        <tr height='89'>
            <td>
            
            </td>
        </tr>
        
    <! ROW FIVE >

        <tr>
            <td>
            <table border='0'>
                <tr>
                    <td width='131'>
                    
                    </td>
                    <td>
                    <a href='#' class='small_text_links'>Terms of Use</a>
                    </td>
                    <td width='346px'>
                    </td>
                    <td>
                    <a href='#' class='small_text_links'>Privacy Policy</a>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
    </table>
        "
    ;
        
        } 
    Last edited by lightnb; 02-27-2007 at 08:58 AM.

Thread Information

Users Browsing this Thread

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

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