MasterACE14 Posted March 15, 2008 Share Posted March 15, 2008 hey, I have a function which I basically was using to SELECT 1 column from a table. But I have been using it alot more lately, in more and more pages, and it becomes tedious, and reduces page loading times. Basically, it is used just to SELECT the 1 column, I want to SELECT all of them( * ) , but the catch is, I want to put each column( $row['column'] ) into a variable with the column name as the suffix of the variable name so it looks like this: $player_column1 = $row['column1']; and have it do this for every single column using $player_ as the prefix for the variable name. I would also like to put in a $playerid variable into the function variables, and if it isn't set, then just use $_SESSION['playerid'] here's my function: <?php // Select fields from the user table function player_table($select){ $where = $_SESSION['playerid']; $sql = "SELECT `" . $select . "` FROM `" . what_table($select) . "` WHERE `id`='" . $where . "' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); while($row = mysql_fetch_assoc($rs)){ if(isset($row[$select])){return $row[$select];} } }; ?> Any help is greatly appreciated Regards ACE Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 15, 2008 Author Share Posted March 15, 2008 any ideas? Quote Link to comment Share on other sites More sharing options...
lordfrikk Posted March 15, 2008 Share Posted March 15, 2008 Use "show columns from $table", then do for loop and assign $player_{$column_name} = $row[$column_name]; I hope I understood what you want Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 15, 2008 Author Share Posted March 15, 2008 I soughta understand lol.... here's what I got so far: <?php // Select fields from the user table function player_table($select,$id=null){ if(!isset($id)) { $id = $_SESSION['playerid']; } $sql = "SHOW COLUMNS FROM `" . what_table($select) . "` WHERE `id`='" . $id . "' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); for(1 < $rs) { $player_{$column_name} = $row[$column_name]; } }; but I'm getting this error: Parse error: syntax error, unexpected ')', expecting ';' in /home/ace/public_html/conflictingforces/functions.php on line 81 line 81 is the blank line after }; any ideas? Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 15, 2008 Share Posted March 15, 2008 You don't need a semicolon after a curly brace, if it's ending a control structure. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 15, 2008 Author Share Posted March 15, 2008 ok, thanks, that errors gone now. But I'm now getting this one... Query: SHOW COLUMNS FROM `rank` WHERE `id`='' LIMIT 1 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `id`='' LIMIT 1' at line 1 my current code: <?php // Select fields from the user table function player_table($table=null,$id=null){ if(!isset($id)) { $id = $_SESSION['playerid']; } if(!isset($table)) { $table = "cf_users"; } $sql = "SHOW COLUMNS FROM `$table` WHERE `id`='" . $id . "' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $i = 1; while($row = mysql_fetch_assoc($rs)){ for($i < $row; $i++; ) { $player_{$column_name} = $row[$column_name]; } } } how do I set $column_name though? Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 15, 2008 Author Share Posted March 15, 2008 couldn't the same thing be done with SELECT * FROM ??? or do I need to use SHOW COLUMNS? latest code.... <?php // Select fields from the user table function player_table($table=null,$id=null){ if(!isset($id)) { $id = $_SESSION['playerid']; } if(!isset($table)) { $table = "cf_users"; } $sql = "SELECT * FROM `$table` WHERE `id`='" . $id . "' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $i = 1; while($row = mysql_fetch_assoc($rs)){ for($i < $row; $i++; ) { $player_{$row['']} = $row['']; } } } latest error... Query: SELECT * FROM `rank` WHERE `id`='' LIMIT 1 Error: Table 'ace_cf.rank' doesn't exist I don't know how its selecting from rank ??? <?php // User Information player_table("cf_users"); player_table("cf_users2"); Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 19, 2008 Author Share Posted March 19, 2008 bump Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 19, 2008 Author Share Posted March 19, 2008 bump.. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 $qsql = "select * from some_table"; $qresult = mysql_query($qsql) or die(mysql_error()); $data = mysql_fetch_assoc($qresult); foreach ($data AS $colname=>$value) { ${$colname} = $value; } Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 20, 2008 Author Share Posted March 20, 2008 Excellent! thank you BlueSkyIs Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 I'm getting the same error again, and I have no idea why.... Query: SELECT * FROM 'rank' WHERE `id`='1' LIMIT 1 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''rank' WHERE `id`='1' LIMIT 1' at line 1 <?php // Select fields from the user table function player_table($table=null,$id=null){ if(!isset($id)) { $id = $_SESSION['playerid']; } if(!isset($table)) { $table = "cf_users"; } $sql = "SELECT * FROM '$table' WHERE `id`='$id' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $row = mysql_fetch_assoc($rs); foreach ($row as $colname=>$value) { $player_{$colname} = $value; } } ?> calling it like this... <?php // User Information player_table("cf_users"); player_table("cf_users2"); ?> Any ideas are greatly appreciated! Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 this is really weird. I'm not calling the function with rank as the $table variable on any page on my website :-\ , yet its trying to do it. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted March 21, 2008 Author Share Posted March 21, 2008 I'm trying all soughts of things now and am getting no where. I'm really begging for help now , please whoever can help, I really would appreciate it. current error: Warning: Missing argument 2 for player_table() in /home/ace/public_html/conflictingforces/functions.php on line 53 Query: SELECT * FROM `rank` WHERE `id`='' LIMIT 1 Error: Table 'ace_cf.rank' doesn't exist current code: <?php // Select fields from the user table function player_table($table,$id){ $sql = "SELECT * FROM `$table` WHERE `id`='$id' LIMIT 1"; $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $row = mysql_fetch_array($rs); foreach ($row as $colname=>$value) { $player_{$colname} = $value; return $player_{$colname}; } } ?> please help if you can :'( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.