TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   method madness (http://www.talkphp.com/advanced-php-programming/2812-method-madness.html)

Evulness 05-16-2008 04:03 PM

method madness
 
ok, i think i've run into a problem but, well here...

Code:

function getone($sql){
$result = $this->query($sql);

if(mysql_num_rows($result) == 0){
    $value = FALSE;
}
 else
    {
      $value = mysql_result($result, 0);
}
    return $value;
}

if i use this as
Code:

$EvSql = " SELECT username FROM users WHERE username ='".$_SESSION['usr_nme']."' ";
$result = $dbCon->getone($EvSql);
print $result;

SHOULD return that username, as $result, right? or did i not write that method correct?
i get no mysql errors, no log errors, no notices with E_all on.... it returns an empty var... or i think its empty...
in that function is $value an array, after returning it at the end?
i dont think it should be, not the way i understand things, that should return that 1 cells data out of the db table....

Village Idiot 05-16-2008 04:06 PM

mysql_result requires three parameters, the resource, row, and field name.

Izym 05-17-2008 07:48 PM

Quote:

Originally Posted by Village Idiot (Post 14680)
mysql_result requires three parameters, the resource, row, and field name.

It doesn't, have a look at PHP: mysql_result - Manual. It should return as an array yes, but try write "print_r($result);" instead of "print $result;", since print can't handle arrays (which print_r was made for).

skweegeasy 05-17-2008 08:59 PM

Quote:

It doesn't, have a look at PHP: mysql_result - Manual
No, no, no, that's just YOUR opinion. Don't tell him his method of using three parameters is wrong, because his method works just fine for him. Also he doesn't believe in typecasting of any kind, so $result might not be an array, it could actually be a chocolate chip cookie for all we know.
:-)

Salathe 05-17-2008 09:17 PM

Is the result from $this->query returning the expected result object? Is the return value really "empty", or FALSE (which would be returned when either no usernames match or when mysql_result fails). Use var_dump or some other debugging functions to spot problems rather than relying on error messages being raised to tell you what's wrong.

It's difficult to help you with debugging when we just get "help, it doesn't work" style requests. Debug everything, anything possibly related to the problem to make sure things are behaving as you expect (or not behaving!).

Village Idiot 05-18-2008 12:26 AM

Quote:

Originally Posted by Izym (Post 14705)
It doesn't, have a look at PHP: mysql_result - Manual. It should return as an array yes, but try write "print_r($result);" instead of "print $result;", since print can't handle arrays (which print_r was made for).

Whoops, my mistake. I've never used it with two rows.

And skwee, please don't slander me.

Salathe 05-18-2008 12:54 AM

Quote:

Originally Posted by Izym (Post 14705)
It doesn't, have a look at PHP: mysql_result - Manual. It should return as an array yes, but try write "print_r($result);" instead of "print $result;", since print can't handle arrays (which print_r was made for).

A quick response to the quoted post (whilst waiting for the OP to get back to us), here are some comments to chew on.

The initial statement is correct, VI said that mysql_result requires three arguments; it doesn't, only requires two arguments with the third field name/offset being optional. I think there may be some confusion over what this function does. The manual should clear that up for anyone not quite clear.

However from that point onwards the post strays a little. I'm going to assume that "it" (in "It should return as an array") references the return value of the getone method as that seems most appropriate. It should not be returning an array at all since the method is get one not get array. Since the method will only return FALSE or the row-column value there's no need in this case to use print_r over print as they will behave similarly. That said, in general, functions like print_r and var_dump are useful tools for taking a peek at things. If "it" refers to the local variable within getone there's again no reason to assume that it should be an array, it's likely not to be one if the class architecture is anything resembling useful.

If you're talking about mysql_result returning an array, I've no idea where you got that notion.

Finally, print can "handle" arrays (in that it doesn't return an error) by doing its job of printing a string representation of the argument (Array).

OP, please chime in with some responses. ^^

Evulness 05-20-2008 04:29 PM

sorry guys i've been down with a migraine for the last few days...
i switched it to print_r in my method, and its working, so far.... though heres something odd i found...

the result of a var_dump, on $result breaks the item selected into an array, making $result[1-9] = a letter of that item. aka pulling the username, breaks ths result down, if i print($result) it prints "Evulness" but if i print($result[1]); it returns E, 2 returns v, 3 returns u, etc...
if i print $result;, i get the full username....
odd... about how it breaks the username down into an array like that...

when i var_dump $result, it gives me
Quote:

string(7) "Evulness"
i was curious why it said 7, not knowing it counts the letters in the result. which is how i found it breaks each letter into its own entry in the array... var_dump ($result[3]); returns string (3) "l".

it looks like its working how i wanted, with it printing the result like that....
see i thought print would work, but print for some odd reason wasn't returning anything, it was in an array, and print($result) was returning result[0], which is empty... but print_r ($result), and print_r($result[0-7]) returned the username, and each letter as it was called...

Messing around like that just gave me a good idea for a feature for the cms.

thanks for pointing out print_r..... and i never thought of using var_dump.... i have a query output for debugging my mysql, but maybe i should put the var_dump and print_r into it too.


All times are GMT. The time now is 04:02 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0