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!

Whats wrong with this loop? 1

Status
Not open for further replies.

BigBadDave

Programmer
May 31, 2001
1,069
EU
$unid = mysql_num_rows($result) + 1;
$ow = "0";
while (strlen ($unid) < 5) {
$ow .= &quot;0&quot;;
}
$funid = $ow . $unid;

This code keeps causing a timeout error anybody know why? Is it something simple I have overlooked?? Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
cause it's an infinite loop. The clause is always true. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Cheers anikin I completely lost the plot there :

$unid = mysql_num_rows($result) + 1;
$ow = &quot;0&quot;;
$foo = strlen ($unid);
while ($foo < 5) {
$foo++;
$ow .= &quot;0&quot;;
}
$funid = $ow . $unid; Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
Actually anikin I was wondering is this the best way of inserting the Auto ID into a new entry :

$sql = &quot;SELECT * FROM students ORDER BY sname ASC&quot;;
$result = mysql_query ($sql);
$unid = mysql_num_rows($result) + 1;
$ow = &quot;0&quot;;
$foo = strlen ($unid);
while ($foo < 5) {
$foo++;
$ow .= &quot;0&quot;;
}
$funid = $ow . $unid;
$fsid = strtoupper (&quot;emc-&quot; . $funid . &quot;-&quot; . $fdob . &quot;-&quot;. $ffname[0] . $fsname[0]);
mysql_query (&quot;INSERT INTO students (fname, sname, dob, sid) VALUES ('$ffname', '$fsname', '$fdob', '$fsid')&quot;); Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
no.
the best way of inserting an auto_id in the mysql is using an auto_increment field.

See in more documentation. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I have changed it now to this :

$foo = $fdob[0] . $fdob[1] . &quot;/&quot; . $fdob[2] . $fdob[3] . &quot;/&quot; . $fdob[4] . $fdob[5];
mysql_query (&quot;INSERT INTO students (fname, sname, dob) VALUES ('$ffname', '$fsname', '$foo')&quot;);
$sql = &quot;SELECT * FROM students WHERE fname = '$ffname' AND sname = '$fsname' AND dob = '$foo'&quot;;
$result = mysql_query ($sql);
while ($line = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$unid = $line[&quot;id&quot;];
}
$ow = &quot;0&quot;;
$foo = strlen ($unid);
while ($foo < 5) {
$foo++;
$ow .= &quot;0&quot;;
}
$funid = $ow . $unid;
$fsid = strtoupper (&quot;emc-&quot; . $funid . &quot;-&quot; . $fdob . &quot;-&quot;. $ffname[0] . $fsname[0]);
mysql_query (&quot;UPDATE students SET sid='$fsid' WHERE id = '$unid'&quot;);

I think the likelyhood of a student having the same first name, surname and dob is remote

But is there a way of returning the id of a row you just inserted?? Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
why you don't prevent that problem using a new field in the table

id int auto_increment?
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I have go the auto_increment_field for an id field, what I mean is I want to use this number to make up a student id number made up of id, dob, 1st letter fname and 1st letter sname I have made this all work but I need to get the id of the entry Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
Probably a good idea to keep DoB as a mysql standard date (2002-05-28) this canbe converted on the fly using;
SELECT DATE_FORMAT(DoB, '%W/%M/%Y');

which would return:
28/05/2002

then

update students set student_id=concat(id,date_format(dob,'%W%M%Y'),left(fname,1),left(sname,1));

for john smith, id 11, dob 22/11/78:
which would return a student id of:
11221178js

this any use or shall I can my mindless ramblings ? :) ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top