Web Analytics Made Easy -
StatCounter Help me please! - CodingForum

Announcement

Collapse
No announcement yet.

Help me please!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help me please!

    function get_pixels(){

    $pixels_query = mysql_query("SELECT * FROM users ");
    while ($row = mysql_fetch_array($pixels_query, MYSQL_ASSOC)) {
    $pixels = $row['pixels'];
    }

    This does not work, and I don't know why.

  • #2
    We need more information. Are you getting any errors?

    You don't need to use the MYSQL_ASSOC constant if you only have 1 connection instance (you also haven't supplied this for your query). You also haven't done any error reporting on your query at all - add an or die(mysql_error()) statement to the end of it. Also, your function doesn't return anything, and doesn't accept any parameters. Functions are defined as algorithms that return a result based on input.

    Also, you're reassigning $pixels every single row, instead of having a running total or a collection of results. Which are you wanting the function to return?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      I want too make it so it shows the number of pixels a user has corrosponding too there ID.

      Comment


      • #4
        Originally posted by DanielWatts View Post
        I want too make it so it shows the number of pixels a user has corrosponding too there ID.
        Read what I said above. Are you getting any errors? Add the 'or die()' statement like I said. You'll also need to be more specific - are the pixels (pixels of what :S) stored in the users table? You'll need to add a WHERE clause to your query as well, to single out an ID. There are several issues with your code, did you write it? It looks like you don't really know what each element does.
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          Parse error: syntax error, unexpected '}' in C:\Users\tracy watts\Desktop\Notepad++\xampp\htdocs\projects\wowscene\func\user.func.php on line 12

          Line 12:

          }

          I wrote it, but using my mates source. if you can add my msn; [email protected] and assist me through that, that'll be awesome thanks.

          Comment


          • #6
            Even though there are errors in that function, and the misuse of function logic, your error is not coming from there. Your error is coming from line 12 in the script. Paste the whole script on here please, including user.func.php.

            I wrote it, but using my mates source. if you can add my msn; [email protected] and assist me through that, that'll be awesome thanks.
            No. It's better for everyone if you receive your help from here, for multiple reasons.
            Useful function to retrieve difference in times
            The best PHP resource
            A good PHP FAQ
            PLEASE remember to wrap your code in [PHP] tags.
            PHP Code:
            // Replace this
            if(isset($_POST['submitButton']))
            // With this
            if(!empty($_POST))
            // Then check for values/forms. Some IE versions don't send the submit button 
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

            Comment


            • #7
              This function is missing its closing brace.
              If your intent is to store many results, $pixels must be an array. Otherwise, you needn't fetch in a loop. You will also need to do something with the results.
              As for using MYSQL_ASSOC, this has nothing to do with a connection. It has to do with whether the mysql_fetch_array will return a numeric, associative, or both.
              PHP Code:
              function get_pixels()
              {
                  
              $pixels = array();
                  
              $pixels_query mysql_query("SELECT * FROM users ");
                  while (
              $row mysql_fetch_array($pixels_queryMYSQL_ASSOC))
                  {
                      
              $pixels[] = $row['pixels'];
                  }
                  return 
              $pixels

              That is likely what you want to use.

              Edit:
              I should add. Functions themselves do not require any parameters, nor do they require any results. There is nothing wrong with a function signature of void myfunc();. The purpose of functions are not to return results, they are designed to create reuse of code. An example function in PHP that matches the above signature is session_unset.
              Last edited by Fou-Lu; Sep 10, 2011, 04:55 PM.
              PHP Code:
              header('HTTP/1.1 420 Enhance Your Calm'); 
              Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

              Comment


              • #8
                Originally posted by Fou-Lu View Post
                This function is missing its closing brace.
                If your intent is to store many results, $pixels must be an array. Otherwise, you needn't fetch in a loop. You will also need to do something with the results.
                As for using MYSQL_ASSOC, this has nothing to do with a connection. It has to do with whether the mysql_fetch_array will return a numeric, associative, or both.
                PHP Code:
                function get_pixels()
                {
                    
                $pixels = array();
                    
                $pixels_query mysql_query("SELECT * FROM users ");
                    while (
                $row mysql_fetch_array($pixels_queryMYSQL_ASSOC))
                    {
                        
                $pixels[] = $row['pixels'];
                    }
                    return 
                $pixels

                That is likely what you want to use.

                Edit:
                I should add. Functions themselves do not require any parameters, nor do they require any results. There is nothing wrong with a function signature of void myfunc();. The purpose of functions are not to return results, they are designed to create reuse of code. An example function in PHP that matches the above signature is session_unset.
                What I mainly meant was, Functions should return something, usually based on a parameter of input. They should have some form of return statement to produce a result.

                MYSQL_ASSOC, lol, I spewed out completely the wrong information.

                It was noted that he wanted to produce a result of pixels based on an ID, so he'll need to include the WHERE id=$id clause, meaning he probably will have to pass an argument into the function (I think).

                I didn't jump to the missing } conclusion because his error was a missing }, on line 12 so thought it could be a reasonable assumption line 12 wasn't the last line of his function definition. Based on the information I took from the replies so far, I think this is more appropriate:

                PHP Code:
                function get_pixels($id)
                {
                    
                $pixels_query mysql_query("SELECT * FROM users WHERE id=$id") or die(mysql_error());
                    
                $row mysql_fetch_array($pixels_queryMYSQL_ASSOC);
                    return 
                $row['pixels'];

                Last edited by BluePanther; Sep 10, 2011, 05:06 PM.
                Useful function to retrieve difference in times
                The best PHP resource
                A good PHP FAQ
                PLEASE remember to wrap your code in [PHP] tags.
                PHP Code:
                // Replace this
                if(isset($_POST['submitButton']))
                // With this
                if(!empty($_POST))
                // Then check for values/forms. Some IE versions don't send the submit button 
                Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

                Comment


                • #9
                  Originally posted by DanielWatts View Post
                  Parse error: syntax error, unexpected '}'
                  "Tango says double quotes with a single ( ' ) quote in the middle"
                  '$Name says single quotes with a double ( " ) quote in the middle'
                  "Tango says double quotes ( \" ) must escape a double quote"
                  '$Name single quotes ( \' ) must escape a single quote'

                  Comment


                  • #10
                    Originally posted by tangoforce View Post
                    unexpected. I think there's something wrong with my eyes tonight...

                    Definitely need to see more of this code.
                    Useful function to retrieve difference in times
                    The best PHP resource
                    A good PHP FAQ
                    PLEASE remember to wrap your code in [PHP] tags.
                    PHP Code:
                    // Replace this
                    if(isset($_POST['submitButton']))
                    // With this
                    if(!empty($_POST))
                    // Then check for values/forms. Some IE versions don't send the submit button 
                    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

                    Comment


                    • #11
                      Yup lol. Been there done that
                      "Tango says double quotes with a single ( ' ) quote in the middle"
                      '$Name says single quotes with a double ( " ) quote in the middle'
                      "Tango says double quotes ( \" ) must escape a double quote"
                      '$Name single quotes ( \' ) must escape a single quote'

                      Comment

                      Working...
                      X
                      😀
                      🥰
                      🤢
                      😎
                      😡
                      👍
                      👎