You are here: Re: Name of a variable in MySQL field « PHP Programming Language « IT news, forums, messages
Re: Name of a variable in MySQL field

Posted by Rik on 09/28/73 11:47

Heike Behrens wrote:
> Thanks a lot, Erwin, for your time. But I think you didn't understand
> my question. My problem is not the querying of MySQL.
>
> The MySQL field contains the string "$jahr", as I wrote above.
>
>> This just sets some variable named $jahr.
> Right. And I want the content of the variable $jahr inserted at that
> place in the string from MySQL where $jahr is written. This is no
> problem with normal strings (see the example of "Hello World"), but a
> string from a MySQL statement does not behave the same. This is what I
> do not understand.


If you assign a string to a variable using double quotes, $world is
recognized.
This action is taken on creation of the string though. Keep in mind that the
new string isn't "variable".

$world = 'world';
$string = "Hello $world";
$world = 'earth';
echo $string;

$string would still be "Hello world".

While fetching fields from a database, your not assinging variables to it
(your database doesn't know anything about the vaiables of PHP script, so it
just outputs the string '$world'. (dollarworld).

Take this into consideration:
$field = '$jahr'; //field now contains '$jahr' literally.
$string = $field;
echo $string;//$string offcourse just contains the literal '$jahr' string.

If your database-field ONLY contains the variable name, you could store the
variable name without the $-sign, and use in your code $$field. What this
does is taking the stringvalue from $field, and looking for the variable of
that name.

You could almost equally use $GLOBAL[$field];

If you have text in the database, containing several places where you want
the value of a certain field, with a limited amount of possible fields, you
could use the (s)printf() function:
http://nl3.php.net/manual/en/function.sprintf.php

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация