...

View Full Version : Help me please!



DanielWatts
09-10-2011, 09:11 PM
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.

BluePanther
09-10-2011, 09:21 PM
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?

DanielWatts
09-10-2011, 09:21 PM
I want too make it so it shows the number of pixels a user has corrosponding too there ID.

BluePanther
09-10-2011, 09:24 PM
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.

DanielWatts
09-10-2011, 09:25 PM
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.

BluePanther
09-10-2011, 09:49 PM
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.

Fou-Lu
09-10-2011, 09:52 PM
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.


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

That is likely what you want to use.


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.

BluePanther
09-10-2011, 10:04 PM
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.


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

That is likely what you want to use.


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:



function get_pixels($id)
{
$pixels_query = mysql_query("SELECT * FROM users WHERE id=$id") or die(mysql_error());
$row = mysql_fetch_array($pixels_query, MYSQL_ASSOC);
return $row['pixels'];
}

tangoforce
09-10-2011, 10:39 PM
Parse error: syntax error, unexpected '}'

:confused:

BluePanther
09-10-2011, 10:41 PM
:confused:

unexpected. I think there's something wrong with my eyes tonight...

Definitely need to see more of this code.

tangoforce
09-10-2011, 10:51 PM
Yup lol. Been there done that ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum