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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why can't I get a for loop working

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
I cant see what I am doing wrong in this for loop. I can do it easy in PHP, but because I am new to Delphi I just don't get it.Can some one point me in the right direction?
code--------------------------------------
procedure TMainForm.CdrButtonOKClick(Sender: TObject);
var
i : integer;
j : integer;
k : pchar;
begin
Assignfile(CdrText, CdrImportEdit.Text);
Reset(CdrText);
while not EOF(CdrText) do begin
ReadLn (CdrText, Buffer1);
ExtVar := PhoneNum(buffer1);
for i = strlen(Pchar(ExtVar)) downto 1 do
begin
areacode := copy(ExtVAr, 0,i) : string;
mySQLCollexClient.Query.PrepareTask (Pchar(SELECT * FROM stdcodes
WHERE areacode = '+areacode,nil,nil,AnyThreadedError;
SQLClient.Execute;
j := mySQLCollexClient.Query.DataCount;
SQLClient.Query.Clear;
if j > 0 then break;
end;
begin
mySQLCollexClient.Query.PrepareTask (Pchar(SELECT * FROM stdcodes
WHERE areacode = '+areacode,nil,nil, AnyThreadedError;
SQLClient.Execute;
k := mySQLCollexClient.Query.Data;
SQLClient.Query.Clear;
end;
edit1.Text := (ExtVar);
end;
CloseFile(CdrText);
end;
----------------------------------------------------

Errors---------------------------------------

[Warning] datacollex.pas(117): Comparing signed and unsigned types - widened both operands
[Error] datacollex.pas(117): For loop control variable must be simple local variable
[Error] datacollex.pas(118): Expression expected but 'BEGIN' found
------------------------------------------------
WhaDaYaTink?
 
Without studying the entire code, one thing stands out-- You need the assignment operator instead of a simple equal sign:
Code:
  for i = strlen(Pchar(ExtVar)) downto 1 do
should be
Code:
  for i
[blue]
Code:
 :=
[/color][/code] strlen(Pchar(ExtVar)) downto 1 do
[/code]


 
looks like your missing the :

Code:
for i := strlen(Pchar(ExtVar)) downto 1 do

Leslie
 
OK I give up. I was trying to use the TMysql component to query a mysql database, but there is no documentation anywhere and while thats OK for some, it is difficult for a beginner. I got my application to compile but it crashed miserabily and I did'nt know where to start. So it's back to DBExpress. So................

What would be the equivilent of this PHP code in delphi using the dbExpress components? An explaination would be helpful, so I learn something.

Code-----------------------------------------------

<?php
include('pageconnect.php');
$connection = @mysql_connect($host, $user, $pass) or die (&quot;Unable to connect to database&quot;);
mysql_select_db($db) or die (&quot;Unable to select database: $db &quot;);

for ($i = strlen($callno); $i != 0; $i--) {
$areacode = substr($callno, 0,$i);
$query = &quot;SELECT *
FROM stdcodes
WHERE areacode = '$areacode' &quot;;
$queryResult = mysql_query($query);
$numrows =mysql_num_rows($queryResult);

if($numrows >0) {
break;
}
}
$query = &quot;SELECT *
FROM stdcodes
WHERE areacode = '$areacode' &quot;;
$queryResult = mysql_query($query);

if ($queryResult) {
while($row =mysql_Fetch_array($queryResult)) {

echo &quot;the country for $row[areacode] is $row[country]&quot; ;
}
} else {
echo &quot;Query failed. SQL=$selectresult error=&quot;.mysql_error();
}
?>
--------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top