Jump to content

Column 'user_name' in field list is ambiguous


aery

Recommended Posts

hi i am getting a error message as

 

Column 'user_name' in field list is ambiguous

 

here

is the database

 

$query = 'CREATE TABLE IF NOT EXISTS user_info (
	user_id			INTEGER			NOT NULL,
	user_name		VARCHAR(100)	NOT NULL,
	first_name		VARCHAR(100)	NOT NULL,
	last_name		VARCHAR(200)	NOT NULL,
	email			VARCHAR(50)		NOT NULL,
	password		VARCHAR(50)		NOT NULL,
	location		VARCHAR(100)	NOT NULL,
	state			VARCHAR(50)		NOT NULL,
	zip				VARCHAR(10)		NOT NULL,

	PRIMARY KEY (user_id) 
)
ENGINE = InnoDB';
mysql_query($query, $db) or die (mysql_error($db));

 

and the php code to retrieve  user info from the table

 


<?php
$query = 'SELECT
	user_name, first_name, last_name,  email, location, state, zip
    FROM
        user_info u JOIN
        user_info i ON u.user_id = i.user_id
    WHERE
        user_name = "' . mysql_real_escape_string($_SESSION['user_name'], $db) . '"';
$result = mysql_query($query, $db) or die(mysql_error($db));

$row = mysql_fetch_array($result);
extract($row);
mysql_free_result($result);
mysql_close($db);
?>
  <ul>
  
   <li>First Name: <?php echo $first_name; ?></li>
   <li>Last Name: <?php echo $last_name; ?></li>
   <li>User Name: <?php echo $user_name; ?></li>
   <li>Email: <?php echo $email; ?></li>
   <li>Location: <?php echo $location; ?></li>
   <li>State: <?php echo $state; ?>
   <li>Zip Code: <?php echo $zip; ?></li>
  </ul>

 

what does ambiguous means i have google but got different ans.

thanks

 

 

 

 

 

 

https://www.google.com/search?q=define%3A+ambiguous&sugexp=chrome,mod=17&sourceid=chrome&ie=UTF-8

 

Because you're joining to a table which also has the same column names, it can't tell which one you want.

 

The bigger problem is WHY you are joining the user table to itself. That's occasionally a good idea but not for what you're doing. WHY??

$query = 'SELECT
	user_name, first_name, last_name,  email, location, state, zip
    FROM
        user_info u JOIN
        user_info i ON u.user_id = i.user_id
    WHERE
        user_name = "' . mysql_real_escape_string($_SESSION['user_name'], $db) . '"';

 

WHY are you doing this? Do you understand what a join does, or how to create a query?

you should research how to use the tools and what they're for before you implement them.  a JOIN is for combining more than one table (99% of the time) into a single table instead of making successive queries.

 

Scenario: You want a user and his rank.  The user table stores a rank_id, which relates to the rank table which stores the name of the rank.

bad way:

<?php
// getting user row
$user = select * from users where user_id = 1;
// get rank with that info
select * from rank where rank_id = $user[rank_id];

better way

<?php
//get user and rank together
SELECT * FROM users where user_id = 1 LEFT JOIN rank on rank.id = user.rank_id

 

May as well stop you getting into bad habits at an early stage.

 

If you only want user and rank, use

 

SELECT user_name, rank FROM ...

 

Do not use "SELECT * " - it's wasteful on resources as the query has to return unwanted data and, when you come back to it later, it makes it difficult to understand exactly what the query is doing.

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.