run statement

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
kyroolUnwar
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 9:45 pm

run statement

Post by kyroolUnwar »

who can help me???

for($x=0; $x<$rowsCount; $x++)
{
$sql = "INSERT INTO PRS_TEST
(
NO_KAD_PENGENALAN,
NAMA_PERIBADI,
NO_FAIL_KPM,
KOD_IPT
)
VALUES
(
".$_SESSION['loading']['content'][$x][0].",
'".$_SESSION['loading']['content'][$x][1]."',
'".$_SESSION['loading']['content'][$x][2]."',
'".$_SESSION['loading']['content'][$x][3]."',
'".$_SESSION['loading']['content'][$x][4]."'
)";

$myQuery->query($sql,'RUN');
}


Why when I run this, the output is:

INSERT INTO PRS_TEST (NO_KAD_PENGENALAN,NAMA_PERIBADI,NO_FAIL_KPM,KOD_IPT) VALUES( 843,'SHOB','A01/E520LP/R1','A01')

but it empty in database.. I not echo the statement.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: run statement

Post by JakeJ »

Easy:

Turn this: "$_SESSION['loading']['content'][$x][0]"

In to this: '$_SESSION[loading][content][$x][0]'

Furthermore, you have specified 4 column names and 5 variable names (0-4 is 5), that will blow it up too.

I hope this helped.
kyroolUnwar
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 9:45 pm

Re: run statement

Post by kyroolUnwar »

ohh.. thanks.. i already correct that.. but it still not work.. but when I run the sql in database, it will stored the data.. I think the problem cause from $myQuery->query($sql,'RUN');
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: run statement

Post by JakeJ »

kyroolUnwar wrote:ohh.. thanks.. i already correct that.. but it still not work.. but when I run the sql in database, it will stored the data.. I think the problem cause from $myQuery->query($sql,'RUN');

Ah, I didn't notice that at first. try this:

Code: Select all

mysql_query($sql)
Also, you're missing a semi-colon AND a close quote here:

Code: Select all

#This
$sql = "INSERT INTO PRS_TEST
#Should be this
$sql = "INSERT INTO PRS_TEST";
kyroolUnwar
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 9:45 pm

Re: run statement

Post by kyroolUnwar »

I don't sure, mysql_query($sql) can use for oracle database?

one more thing, my sql statement actually,

$sql = "INSERT INTO PRS_TEST (NO_KAD_PENGENALAN, NAMA_PERIBADI, NO_FAIL_KPM, KOD_IPT ) VALUES (".$_SESSION['loading']['content'][$x][0].",'".$_SESSION['loading']['content'][$x][1]."','".$_SESSION['loading']['content'][$x][2]."','".$_SESSION['loading']['content'][$x][3]."')";

$myQuery->query($sql,'RUN');

I actually familiar to php mysql database, this year, i try to learn php oracle database... it hard to me... I think oracle can do more then mysql can do.. so I try to learn oracle.. but... why this error happen..
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: run statement

Post by JakeJ »

kyroolUnwar wrote:I don't sure, mysql_query($sql) can use for oracle database?

one more thing, my sql statement actually,

$sql = "INSERT INTO PRS_TEST (NO_KAD_PENGENALAN, NAMA_PERIBADI, NO_FAIL_KPM, KOD_IPT ) VALUES (".$_SESSION['loading']['content'][$x][0].",'".$_SESSION['loading']['content'][$x][1]."','".$_SESSION['loading']['content'][$x][2]."','".$_SESSION['loading']['content'][$x][3]."')";

$myQuery->query($sql,'RUN');

I actually familiar to php mysql database, this year, i try to learn php oracle database... it hard to me... I think oracle can do more then mysql can do.. so I try to learn oracle.. but... why this error happen..
I had no idea it was Oracle. Nevermind. It might also be helpful to assign your $_SESSION variables to something more reasonably named. for example: $session1 = "$_SESSION['loading']['content'][$x][2]"

I see you still have your single quote marks in the wrong place and you also need to eliminate the periods before and after the variable name. So then your SQL statment will be as follows,

Code: Select all

$sql = "INSERT INTO PRS_TEST (NO_KAD_PENGENALAN, NAMA_PERIBADI, NO_FAIL_KPM, KOD_IPT )  VALUES ('$session1', '$session2', '$session3', '$session4')";
That will make your statement much easier for you to read and is less prone to mistakes. And if you really wanted to get fancy, test each of your variables with an If(isset($var)) before attempting to write to the database.
Post Reply