Jump to content

[SOLVED] MySQL - is the column in table 1, or table 2? -> Revised and Revisited


MasterACE14

Recommended Posts

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

Link to comment
Share on other sites

haha, you got a point their, as soon as I added a ELSEIF, it is now working perfectly  ;D

 

<?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  :D

 

Regards ACE

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.