Inserting variable data into a SQL Server table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bendertez
Forum Newbie
Posts: 2
Joined: Tue Sep 30, 2008 9:12 am

Inserting variable data into a SQL Server table

Post by bendertez »

Hello

I'm trying to insert data from a variable in to a table in SQL Server 2005.

So far my query is this which runs ok but doesn't give me the desired results:

$dsn="Server_Test";
$username="Test";
$password="srvtest";
$sqlconn=odbc_connect($dsn,$username,$password);

global $sqlconn, $stuCode, $stuFnm1, $stuSurn, $stuCount, $sql1;
$sql="SELECT top (5) STUCODE, STUFNM1, STUSURN FROM WARN_VIEW";
$row=odbc_exec($sqlconn, $sql);
while($stuCount=odbc_fetch_row($row)) {

$stuCode=odbc_result($row,'STUCODE');
$stuFnm1=odbc_result($row,'STUFNM1');
$stuSurn=odbc_result($row,'STUSURN');
$stuCount++;
}

{
$sql1="INSERT INTO DIS_WARN(SDW_CODE) VALUES ('$stuCode')";
$row=odbc_exec($sqlconn,$sql1);
}
odbc_close($sqlconn);


The Insert into statement seems to work but will not loop through the records.
If I leave it in its current state I get 1 row inserted into the table.
How can I get it to insert all the data in to the table?

Thank you
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Inserting variable data into a SQL Server table

Post by papa »

You need to loop through the insert statement.
bendertez
Forum Newbie
Posts: 2
Joined: Tue Sep 30, 2008 9:12 am

Re: Inserting variable data into a SQL Server table

Post by bendertez »

Hi
Do you mean loop through within the SQL statement?
Thanks
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Inserting variable data into a SQL Server table

Post by papa »

Well you want to insert $stuCode into the table right?

So What you need to do is to include the insert statement in the while loop so you insert the values that is generated.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Inserting variable data into a SQL Server table

Post by RobertGonzalez »

Move your insert code into your while loop code.
Post Reply