MasterACE14 Posted December 4, 2007 Share Posted December 4, 2007 evening everyone, I had a script a couple days ago to work out if a MySQL column was in 1 table or another. And it never ended up working, I have done some more research and tried some more things, and this is what I got: <?php // Check if the column in the database is in 1 table or another function what_table($column) { $fields = mysql_list_fields("database", "cf_users"); $columns = mysql_num_fields($fields); for ($i = 0; $i < $columns; $i++) { $field_array[] = mysql_field_name($fields, $i); } if (!in_array($column, $field_array)) { $table = "cf_users2"; } return $table; }; //and my function for selecting a column from the database for the logged in user // Select fields from the user table function player_table($select){ $where = $_SESSION['playerid']; /* $rs = mysql_connect("localhost", "ace_ACE", "shadow69"); mysql_select_db("ace_cf", $rs); */ $sql = "SELECT `" . what_table($select) . "`.`" . $select . "` FROM `cf_users` INNER JOIN `cf_users2` ON (`cf_users2`.`id` = `cf_users`.`id`) 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];} } }; ?> But for some reason I am getting an error, and the SELECT field is empty ??? Query: SELECT ``.`id` FROM `cf_users` INNER JOIN `cf_users2` ON (`cf_users2`.`id` = `cf_users`.`id`) LIMIT 1 Error: Column 'id' in field list is ambiguous my what_table() function does seem to be putting the correct table in after the FROM. any ideas on why the SELECT field is empty, would be greatly appreciated. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/80074-solved-mysql-is-the-column-in-table-1-or-table-2-revised-and-revisited/ Share on other sites More sharing options...
btherl Posted December 4, 2007 Share Posted December 4, 2007 You set table to cf_users2 if the column is not found .. but what if it is found? Quote Link to comment https://forums.phpfreaks.com/topic/80074-solved-mysql-is-the-column-in-table-1-or-table-2-revised-and-revisited/#findComment-405815 Share on other sites More sharing options...
MasterACE14 Posted December 4, 2007 Author Share Posted December 4, 2007 haha, you got a point their, as soon as I added a ELSEIF, it is now working perfectly <?php // Check if the column in the database is in 1 table or another function what_table($column) { $fields = mysql_list_fields("database", "cf_users"); $columns = mysql_num_fields($fields); for ($i = 0; $i < $columns; $i++) { $field_array[] = mysql_field_name($fields, $i); } if(!in_array($column, $field_array)) { $table = "cf_users2"; } elseif(in_array($column, $field_array)) { $table = "cf_users"; } return $table; }; Cheers Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/80074-solved-mysql-is-the-column-in-table-1-or-table-2-revised-and-revisited/#findComment-405817 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.