Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot outputfirst colum of record ( primary key )

Status
Not open for further replies.

luxgud

Instructor
Jan 16, 2007
4
GB
Hi

I am new to MySQL/PHP. I wanted to see if I could set up a database table in an existing database and output the data into a web page. Everything works OK except that I cannot see the ID column. This is the primary key (auto_increment)

Here's the strange thing, I can see the ID 1,2,3,4,5 .. when I view the database table in phpMyAdmin. I just wanted to do it all in code and output it.

Can you explain why I cannot see the ID column ?? I have tried and tried. Below shows code bits for (1) creating the table (2) insering data (3) outputing data

Please note that the data is inserted from a form that uses a seperate php handler file.

Tthank you if you can help me please


// Create table in your database

mysql_select_db("pstwin-g", $con);


$sql = "CREATE TABLE person

(
personID int auto_increment primary key,

fname varchar(15),

sname varchar(15),

age int

)";





// Insert a row of information into the table "person"

mysql_query( "INSERT INTO person VALUES (' ' ,'$_POST[fname]','$_POST[sname]','$_POST[age]') " )

or die(mysql_error());







// store the record of the "PERSON" table into $row

echo "<table border='1 'bgcolor ='yellow' cellpadding = '5'>

<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>

</tr>";

while($row = mysql_fetch_array($result))

{
echo "<tr>";

echo "<td>" . $row['personID'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";

echo "</tr>";
}


echo "</table>";
 
I can't see anywhere in your code where you issue a SELECT statement to acquire the data from the DB to be displayed.

Code:
$sql="SELECT *FROM mytable";



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 

Hi

This is the code I have used in the output file

$result = mysql_query("SELECT * FROM person")
or die(mysql_error());
 
There is no apparent reason from what you posted why there would be no ID. Can you see the ID for the records from PHPmyadmin?



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yes ! I can see the ID field when using phpMYAdmin - this is what is confusing me ??? It is autoincrementing perfectly.

Why can I not see it when I output the data ???

Thanks
 
Well unless the code you posted is not exactly the code you are using, I fail to see why the personId is not showing.

The only thing i can think of, is since you are using yellow for the table the white font is not the best contrast for it, and although it might be displaying you cant see it. Try changing the color of your table





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I found the problem.

When you want to dispaly the primary key (that's autoincremented)you need to explicitly state that in the insert command ie

INSERT(id,name,age) values('', 'name','age')

In the above I have id in the columns but not the values.

This solved the problem.

Thank you for your kind help anyway

Lux.
 
You are mixing up an INSERT with a SELECT, when you select the column from your table it will show.

You also DO NOT have to specify an auto increment column. You can also insert like this:
INSERT(name,age) values('name','age')

and the id column will be updated and be searchable with a SELECT statement

Another important thing, you should not use '', you should use NULL above like this:

INSERT(id,name,age) values(NULL, 'name','age')

if you were to use '' on mysql5 it will throw an error message. Best to get out of a bad habit now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top