Jump to content

Is this possible?


TEENFRONT

Recommended Posts

Hey

 

When building a new section on my site, i find it very laborious (sp?) going through and setting all the row names to useable vars, checking which row names i have, and if iv created them all in the results etc.

 

so on the results i do this..

 

$id = $row['id'];
$tname = $row['tname'];
$ttag = $row['ttag'];
$members = $row['members'];
$rating = $row['rating'];
$stars = $row['ratingstar'];
$rrating = $row['rrating'];

 

is there not a simpler way? Can i not just tell php to do that for me, make vars of all the row names that come back from the query. This would be Soooooo much easier. I know $row['rrating'] is usuable and i dont NEED to set it to a prettier $rrating = $row['rrating']; - but i find $rrating easier to work with than $row['rrating'].

 

So yes, is there a function that returns all the rows in the query, and sets a nice var name from the row name.

 

so something like this..

 

$sql = mysql_query('select * from this and that etc');
magic_function_here($sql);

echo $username;
echo $rrating;

Link to comment
https://forums.phpfreaks.com/topic/138702-is-this-possible/
Share on other sites

if $row is the mysql_fetch_array, try this:

foreach(mysql_fetch_array($result, MYSQL_ASSOC) as $row => $value)
{
${$row} = $value;
}

 

completely untested and probably wrong because I don't use foreach much and I actually just learned that ${$var} thing like half an hour ago :o

Link to comment
https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725178
Share on other sites

OMG!!! That is Genius!! I used the Extract with mysql_fetch_assoc, bang it does it all for you.

 

You have no idea how much easier it makes it lol.

 

When buidling something new the worst part for me was going through all the results and setting/checking var names etc etc.

 

Nice nice nice nice nice!!

 

 

Quick question, i normally use mysql_fetch_array... whats the difference with _assoc?

Link to comment
https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725180
Share on other sites

OMG!!! That is Genius!! I used the Extract with mysql_fetch_assoc, bang it does it all for you.

 

You have no idea how much easier it makes it lol.

 

When buidling something new the worst part for me was going through all the results and setting/checking var names etc etc.

 

Nice nice nice nice nice!!

 

 

Quick question, i normally use mysql_fetch_array... whats the difference with _assoc?

 

mysql_fetch_array() stores the array with both numeric indexes and string indexes.  _assoc only performs the latter, which is what you want for extract().

Link to comment
https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725181
Share on other sites

i like having it that way, if im making a user system and want to have its id, but have ids for other stuff like posts, it will get mixed... now if i want something for the current user, i just use $user['id'] and then i know what id it is

 

mysql_fetch_array() stores the array with both numeric indexes and string indexes.  _assoc only performs the latter, which is what you want for extract().

 

mine specifies MYSQL_ASSOC but if you don't wnat that in tehre you can jsut use _assoc

 

Link to comment
https://forums.phpfreaks.com/topic/138702-is-this-possible/#findComment-725568
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.