Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
mysql db function not showing what expected?
Old 09-19-2007, 01:47 PM mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hi all,

i though i would make a function to make getting info from my dbs a bit easier but its not working as i expected

im trying to use this function:
PHP Code:
function db_query($db_tbl$field$where$where_val)
{
include_once(
'connect.php');
$res mysql_query("SELECT $field FROM $db_tbl WHERE $where = $where_val") or die("Error getting $field from $db_tbl");
$return mysql_fetch_array($res);
return 
$return;

and calling it like so:
PHP Code:
echo db_query('users','email','id','1'); 
instead of showing admin@dansgalaxy.co.uk which is in users table on the users with a id of 1

it shows Array... why?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 09-19-2007, 01:51 PM Re: mysql db function not showing what expected?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
mysql_fetch_array returns an array, even if you only select one field. Try this instead:
PHP Code:
function db_query($db_tbl$field$where$where_val)
{
include_once(
'connect.php');
$res mysql_query("SELECT $field FROM $db_tbl WHERE $where = $where_val LIMIT 1;") or die("Error getting $field from $db_tbl");
$return mysql_fetch_array($resMYSQL_ASSOC);
return 
$return[$field];

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-19-2007, 01:53 PM Re: mysql db function not showing what expected?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
PHP Code:
$return mysql_fetch_array($res);
return 
$return
Because you return an array, simply.

If you want only the field:
PHP Code:
$rows=mysql_fetch_array($res);
return 
$rows[$field]; 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-19-2007, 03:10 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
thanks i knew i was missing somethin simple why dont PHP work tho so if liek this situation its just one field (in my case) $res just is that field? or oculd this cause problems im not thinking about?

i tried to give TP but i need to give some more beofore i can

edi:
Yay i just added this in: $return = $return[$field]; and all is good
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE

Last edited by dansgalaxy; 09-19-2007 at 03:11 PM..
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 03:13 PM Re: mysql db function not showing what expected?
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
Your fetching an array, even if it contains only one member. Then you have to get the individual member.
joder is offline
Reply With Quote
View Public Profile
 
Old 09-19-2007, 03:20 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Ok this would probably sound stupid but please i havent botherd with custom functions much

ok how could i have a flexible function
so if for instance i had a function like...

PHP Code:
function settings($field)
{
$res mysql_query("SELECT $field FROM settings WHERE id=1") or die("ERROR getting $field value");
$return mysql_fetch_array($res);
$return $return[$field];
return 
$return;

but i would want it so if i used it like settings('somefield','a new value');

it would execute this:
PHP Code:
function settings($field,$newval)
{
if(
$res mysql_query("UPDATE settings SET $field=$newval WHERE id=1"))
return 
TRUE;

or something like that?

so if it has two params it does one thing if it has one it does another?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 03:21 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Quote:
Originally Posted by joder View Post
Your fetching an array, even if it contains only one member. Then you have to get the individual member.

Sorry i missed this i was posting my post before this

what do you mean? i do in the whole thing i do like where=1 ??
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 03:34 PM Re: mysql db function not showing what expected?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
Ok this would probably sound stupid but please i havent botherd with custom functions much

ok how could i have a flexible function
so if for instance i had a function like...

PHP Code:
function settings($field)
{
$res mysql_query("SELECT $field FROM settings WHERE id=1") or die("ERROR getting $field value");
$return mysql_fetch_array($res);
$return $return[$field];
return 
$return;

but i would want it so if i used it like settings('somefield','a new value');

it would execute this:
PHP Code:
function settings($field,$newval)
{
if(
$res mysql_query("UPDATE settings SET $field=$newval WHERE id=1"))
return 
TRUE;

or something like that?

so if it has two params it does one thing if it has one it does another?
No you can't.
Some languages, like JAVA have a principle of signature, who specify that several functions can ave the same name, but being different and based upon the number and the type of the parameters they accept.

For exemple, in Oracle, I can declare those:
Code:
function isNum(nbr number) return boolean is
 reslt number;
begin
  select nbr+1 into reslt from dual;
  return true;
exception
  when others then return false;
end isNum;

function isNum(nbr varchar2) return boolean is
begin
  return false;
end;
Those 2 functions are valid, but one is used when the paramter is a number, and the other when the parameter is a string.

PHP being loose on it's datatype, it's not possible. Or at least not now.
I've heard that some dev are working on it, and that it's already existing for array and classes, but not the other primitives.
http://www.php.net/language.oop5.typehinting
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-19-2007, 03:37 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
ok so if i had a two para function

but only enterd one would that make the second nothing? so could it be done like

PHP Code:
function settings($field,$newval)
{
if(
$newval == "")
{
$res mysql_query("SELECT $field FROM settings WHERE id=1") or die("ERROR getting $field value");
$return mysql_fetch_array($res);
$return $return[$field];
return 
$return;
}
else {
if(
$res mysql_query("UPDATE settings SET $field=$newval WHERE id=1"))
return 
TRUE;
}

know what i mean?

or would it just return a error if i just had settings(field)
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 04:01 PM Re: mysql db function not showing what expected?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I'm not sure if you are talking about polymorphism or optional parameters but if you want a parameter to be optional all you have to do is specify a default value. In php you do this by setting a parameter equal to a value in the declaration:

function foo($par1, $par2=false)
{
//body
}

when you call foo, if you only give it one parameter par2 will be false.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-19-2007, 04:12 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
so i could then just have a if/else condition if the second par == what ever default i give it run it as select if it has it update the first par with the value of the second ?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 04:16 PM Re: mysql db function not showing what expected?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
so i could then just have a if/else condition if the second par == what ever default i give it run it as select if it has it update the first par with the value of the second ?
Correct. But I just read that PHP does support polymorphism (PHP5+). So if you prefer you can define two functions with the same name:

function foo($par1)
{
//body
}

function foo($par1, $par2)
{
//body
}

documentation: http://www.devshed.com/c/a/PHP/PHP-5...olymorphism/2/

In PHP the signature of a function is the name of the function and the number of parameters it takes. In other languages like Java and C++ the signature of a function is its name, number of parameters, and datatypes.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-19-2007 at 04:23 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-19-2007, 04:16 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
yay!!
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 04:24 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
erm it not working?

i have this for the function:
PHP Code:
function style($field$newval=FALSE)
{
if(
$newval == FALSE)
{
$res mysql_query("SELECT $field FROM style WHERE id=1") or die("ERROR getting $field value");
$return mysql_fetch_array($res);
$return $return[$field];
return 
$return;
}
else {
if(
$res mysql_query("UPDATE style SET $field=$newval WHERE id=1"))
return 
'ITS CHANGED';
}

and am trying to change it like this
PHP Code:
echo style('title''DANS TITLE');
echo 
style('title'); 
but this only echos the orginal value once, and the one which should be changing the title saved in my DB its seems to be doing nothing
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-19-2007, 06:09 PM Re: mysql db function not showing what expected?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
But I just read that PHP does support polymorphism (PHP5+)
Sadly not..
Only classes overloading is possible, but you have to extend a base class for this.
Outside of the classes, nothing like polymorphism exists...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-19-2007, 07:20 PM Re: mysql db function not showing what expected?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
should i still be pretending i understand?...

o why isnt the update working tho??
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to mysql db function not showing what expected?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53062 seconds with 12 queries