On Fri, 9 Aug 2002, Stefan Melcher wrote:
> Hello Matthew,
>
> thanks for you fast responding. So I solved my problem by your
> suggestion in this way now:
>
> query="SELECT * FROM exampleDB";
> $db1->query($query);
> $meta=$db1->metadata("", true);
> while ($db1->next_record()) {
> for ($i=0; $i<=$meta["num_fields"]; $i++) {
> $col=$meta[$i]["name"];
> $$col=$db1->f($col);
> }
> echo $loginID;
> echo $other_colnamevalues;
> }
>
> Do you see an easier solution
My favorite looper is foreach, because it eliminates the need for a
dummy variable:
foreach ($meta as $m) {
$col = $m['name'];
$$col = $db->f($col);
}
> or a chance to put this whole thing
> into a function inside a new class that extends DB_Sql?
> Tried this in my own class DB_Example in local.inc:
>
> class DB_Example extends DB_Sql {
[...]
> function get_values($table="") {
> global $query;
> $this->db = new $this->classname;
> $this->db->query($query);
> $meta=$this->db->metadata($table, true);
> for ($i=0; $i<=$meta["num_fields"]; $i++) {
> $col=$meta[$i]["name"];
> $$col=$this->db->Record[$col];
> }
> return $$col;
> }
> }
A couple of things are wrong with this. Query shouldn't be a global
variable, and you shouldn't even call a query because as in the context
below, you have already queried before you call this function. Finally,
the variables you create and instantiate in the function scope don't get
passed up. You could do
function get_values() {
$meta = $this->metadata("",true);
foreach($meta as $m) {
$col = $m['name'];
$GLOBALS[$col] = $this->Record[$col];
}
}
But that would only put them in the global scope, and you would have to
import them down after calling the function! I don't think PHP is really
equipped to do this as a function.
The extract method is fastest. It might be a little unkosher in that
Record is labelled an internal variable, but it seems to me unlikely to
change implementation. Then you have
$db->query($whatever);
while ($db->next_record()) {
extract($db->Record);
echo $loginID;
// etc.
}
The EXTR_OVERWRITE specification is the default. See
http://www.php.net/extract
HTH,
MPL
>
>
> Which should be used in this context:
>
> $query="SELECT * FROM exampleDB;
> $db->query($query);
> while ($db->next_record()) {
> $db->get_values();
> echo $loginID;
> echo $other_colnamevalues;
> }
>
> But this didn't worked. Any new ideas?
>
> Thanks and Greetings,
> Stefan
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Phplib-users mailing list
> Php...@li...
> https://lists.sourceforge.net/lists/listinfo/phplib-users
>
--
----------------------------------------------------------------
Matthew Leingang http://www.math.rutgers.edu/
Rutgers University lei...@ma...
Department of Mathematics "This signature needs no quote."
|