having a function inside a table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

having a function inside a table

Post by dsdsdsdsd »

hello;

I have the_table:
col_1 , col_2
---------------
cook , cook the cookies

I would like to place a function inside of col_2 that reads the value of col_1 and then inserts that value into the col_2:
col_1 , col_2
----------------
cook , col_1.value the cookies

such that:

Code: Select all

$result = mysql_query("select * from the_table");
while ($row=mysql_fetch_array($result , MYSQL_ASSOC) )
  { print $row[col_2];
  }
will output ==> cook the cookies;

is this feasible;

thanks
Shannon Burnett
Asheville NC USA
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

my question is why? you can put what ever you want inside the column but I believe mysql doesn't support subqueries which is what it sounds like what you are trying to do.
I believe postgressehttp://www.postgresql.org/ does, as well as oraclehttp://www.oracle.com if you want complete overkill.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

why don't you want to use the following table:

Code: Select all

col_1
----
cook
and use it as:

Code: Select all

$result = mysql_query("select concat(col_1, ' the cookies') from the_table");
while ( list($sentence) = mysql_fetch_row($result) )
    echo $sentence;
?
dsdsdsdsd
Forum Commoner
Posts: 60
Joined: Fri Dec 05, 2003 3:10 pm

Post by dsdsdsdsd »

hello phpscott and weirdan; thanks for your responses;

the example I posted is a simplified situation; ultimately I will need to go into a cell and replace some string with some other string; there are several php things I could do to do pattern matches within the query result in order to find an identified placeholder in a table;

however as with any pattern matching there is a large potential for problems; for instance if you are looking for 'cook' a simple pattern match would find 'cook' and also the c-o-o-k in 'cookies';

I cannot concat because I have no control over the table values nor any control over what a user may want to change a value to;

so it seems that the best solution is to embed a function that refers to another table cell;

thanks Shannon
Post Reply