mysql db function not showing what expected?
09-19-2007, 01:47 PM
|
mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
09-19-2007, 01:51 PM
|
Re: mysql db function not showing what expected?
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
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($res, MYSQL_ASSOC);
return $return[$field];
}
|
|
|
|
09-19-2007, 01:53 PM
|
Re: mysql db function not showing what expected?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
09-19-2007, 03:10 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
Last edited by dansgalaxy; 09-19-2007 at 03:11 PM..
|
|
|
|
09-19-2007, 03:13 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,442
Name: James
Location: In the ocean.
|
Your fetching an array, even if it contains only one member. Then you have to get the individual member.
|
|
|
|
09-19-2007, 03:20 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
09-19-2007, 03:21 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
Quote:
Originally Posted by joder
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 <<
|
|
|
|
09-19-2007, 03:34 PM
|
Re: mysql db function not showing what expected?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Quote:
Originally Posted by dansgalaxy
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.
|
|
|
|
09-19-2007, 03:37 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
09-19-2007, 04:01 PM
|
Re: mysql db function not showing what expected?
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
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.
|
|
|
|
09-19-2007, 04:12 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
09-19-2007, 04:16 PM
|
Re: mysql db function not showing what expected?
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by dansgalaxy
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.
Last edited by NullPointer; 09-19-2007 at 04:23 PM..
|
|
|
|
09-19-2007, 04:16 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
yay!!
__________________
Discounted Web Hosting With XDnet! >> Get 25% of hosting~ Promo: Webmaster-talk <<
|
|
|
|
09-19-2007, 04:24 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
09-19-2007, 06:09 PM
|
Re: mysql db function not showing what expected?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
09-19-2007, 07:20 PM
|
Re: mysql db function not showing what expected?
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
|
« Reply to mysql db function not showing what expected?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|