Jump to content


Photo

Primary key


  • Please log in to reply
6 replies to this topic

#1 OLM3CA

OLM3CA
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationCyprus

Posted 21 August 2006 - 10:17 PM

Hi!
İ have a question about the "no" section of my table
for example i have a table that includes no,title,message,date i make
"no" =  int primary key auto_incremente etc...
mysql automaticly number the titles with "no" (1,2,3 )
but when i delete a title (ex: the 3rd one) and then add another title,the "no" is 4 for the new title.i want it to be get the number 3 ? how can i make it ?

AMD rather than Intel , PHP rahter than ASP

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 21 August 2006 - 11:40 PM

Don't even bother -- leave the UID alone.  And please rename the column to id, uid, something_id, anything but "no".  Why do you want the number to change?  You're not supposed to use it as a counter.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 OLM3CA

OLM3CA
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationCyprus

Posted 22 August 2006 - 12:20 AM

becouse I am using it in links:

<?php 
	echo "<a href='?newsID=$i'>".$row['title']."</a>";
	$i = $i + 1;	
?>

and in sql :

$sorgu2="SELECT * FROM news WHERE no=$newsID ";

and "no" is my primary key,for example if i delete 4rd news my link will be : newsID=4 the counter "i" does not change
When i click on it no news is going to appear because newsID=4=no (primary key) is deleted
AMD rather than Intel , PHP rahter than ASP

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 August 2006 - 12:29 AM

You should NEVER rely on the PK being any value at all.  You should be doing the following, which will work in the general case:

<?php 
	echo "<a href='?newsID=".$row['no']."'>".$row['title']."</a>";
?>

Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 OLM3CA

OLM3CA
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationCyprus

Posted 22 August 2006 - 01:00 AM

but my query is diffrent i am so confused can you help me with this.
I want to show 10 news on homepage and when i click one of them i have to see details of that news.

$query1 = "SELECT title FROM news LIMIT 0, 10";
$query2="SELECT * FROM news WHERE no=$newsID ";
$result1 = mysql_query($query1);
$result2 = mysql_query($query2);

// I AM USING THIS TO SHOW THE NEWS TITLES LINKS
$i=1; while ($row = mysql_fetch_array($result1)) {
 
                                     echo "<a href='?newsID=$i'>".$row['title']."</a>";
									$i = $i + 1;	
								}


// I AM USING THİS TO SHOW THE DETAILS OF NEWS

while ($row2 = mysql_fetch_array($result2)) {


      echo $row2['title];
     
      echo $row2['fullpart'];

 }  

AMD rather than Intel , PHP rahter than ASP

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 August 2006 - 03:49 AM

So get back uid & title, instead of using just title.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 OLM3CA

OLM3CA
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationCyprus

Posted 22 August 2006 - 09:34 AM

Thank you fenway I'have done it (:
AMD rather than Intel , PHP rahter than ASP




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users