Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Help me please!

  1. #1
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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?

  • #3
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want too make it so it shows the number of pixels a user has corrosponding too there ID.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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.

  • #5
    New Coder
    Join Date
    Aug 2011
    Location
    London
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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; sweet0798@hotmail.co.uk and assist me through that, that'll be awesome thanks.

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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; sweet0798@hotmail.co.uk 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.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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; 09-10-2011 at 09:55 PM.

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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; 09-10-2011 at 10:06 PM.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote Originally Posted by DanielWatts View Post
    Parse error: syntax error, unexpected '}'
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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.

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,324
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Yup lol. Been there done that
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    LinkBacks (?)

    1. 06-17-2014, 01:07 PM

    Posting Permissions

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