Jump to content

quick question with "for"


mrbuter

Recommended Posts

Hey there, I'm pretty new with php so im just wondering why this line of code doesn't work

 

$results_text = mysql_query("SELECT * FROM questions") or die(mysql_error());
$nbr_rangees = mysql_num_rows($results_text);
mysql_query("INSERT INTO `users` (" . for($i=0;$i<$nbr_rangees;$i++){ echo "`" . mysql_result($results_text,$i,"id") . "`,"; } . ") VALUES (" . for($i=0;$i<$nbr_rangees;$i++){ echo "'$" . mysql_result($results_text,$i,"name") . "', "; } . ")") or die(mysql_error());

 

I made two seperate tests with the "for" codes I used (i.e. a file with just

 

$results_text = mysql_query("SELECT * FROM questions") or die(mysql_error());
$nbr_rangees = mysql_num_rows($results_text);
for($i=0;$i<$nbr_rangees;$i++){ echo "'$" . mysql_result($results_text,$i,"name") . "', "; }

)

 

and it outputted the stuff correctly so I figured it would work in the query.

 

But when I try and run the file with the mysql_query and the fors it says:

 

 

Parse error: syntax error, unexpected T_FOR in blablabla/bla.php on line 39

 

 

How can I get around this?

 

Thanks in advance.

 

Link to comment
Share on other sites

As SemiApocalyptic said, you cannot use a for loop in such a context. this is one way how you can do it:

 

$sql_text = "INSERT INTO .... ";

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "text for each iteration";
}

$sql_text .= "rest of the query";

mysql_query( $sql_text );

Link to comment
Share on other sites

Alright I've got this so far

 

$sql_text = "INSERT INTO `users` (";

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "`" . mysql_result($results_text,$i,"id") . "`,";
}

$sql_text .= ") VALUES (";

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "'$" . mysql_result($results_text,$i,"name") . "', ";
}

$sql_text .= ")";

mysql_query( $sql_text ) or die(mysql_error());

 

But there seem to be two problems. The first of which is that I'm getting an error (dur)

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ('$fullname', '$handle', '$email', '$steamid', '$teams', '$traits', '$l' at line 1

 

and the second is that it seems that it's getting cut off...there are many more VALUES and it is getting cut off at '$l' which is actually supposed to be '$lan'

 

I really do appreciate your help and I'm sure I'm making pretty dumb mistakes which may seem pretty silly to you.

Link to comment
Share on other sites

That didn't seem to improve things.

 

If I have it in the first for loop (the one with the column names as opposed to VALUES) it will say

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'handle`email`steamid`teams`traits`lan`didtheyplay`cal`cevo`lan`knife`gun' at line 1

 

and if i remove it for that loop and only have it for the VALUES one it will say

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ('$fullname''$handle''$email''$steamid''$teams''$traits''$lan''' at line 1

 

Of particular note is that it seemed to have removed it from every one not just the last one. So it isn't '$value1', '$value2', '$value3'. Instead it is now doing $value1''$value2''$value3

 

and for the columns it's doing col1`col2`col3 instead of `col1`,`col2`,`col3`

Link to comment
Share on other sites

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "`" . mysql_result($results_text,$i,"id") . "`,";
  $sql_text = substr( $sql_text, 0, strlen( $sql_text ) - 2 );
}

$sql_text .= ") VALUES (";

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "'$" . mysql_result($results_text,$i,"name") . "', ";
  $sql_text = substr( $sql_text, 0, strlen( $sql_text ) - 2 );
}

$sql_text .= ")";

mysql_query( $sql_text ) or die(mysql_error());

Link to comment
Share on other sites

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "`" . mysql_result($results_text,$i,"id") . "`, ";

}

$sql_text = substr( $sql_text, 0, strlen( $sql_text ) - 2 );

$sql_text .= ") VALUES (";

for( $i = 0; $i < $nbr_rangees; $i++ )
{
      $sql_text .= "'$" . mysql_result($results_text,$i,"name") . "', ";
}

$sql_text = substr( $sql_text, 0, strlen( $sql_text ) - 2 );

$sql_text .= ")";

mysql_query( $sql_text ) or die(mysql_error());

 

That should work. You had 2 mistakes. The first one being; the 2 in the substr function means that the last 2 characters should be skipped, so the , and the space. You only had a , in the first part. Then, you should substract it AFTER the for loop, else it will do it every time the for loop iterates.

Link to comment
Share on other sites

ok great that worked but for some reason it's actually inputting "$fullname" etc. into the database instead of getting the inputted stuff from the forms and inputting THAT into the database.

 

I have a bunch of inputs so the VALUES ('$fullname', '$value2', '$value3') etc. should be gotten from whatever the user inputted shouldn't it?

 

That's what I was doing with an older version which I changed manually instead of getting all the things from the database.

 

(i.e. I actually typed out each column name etc.)

 

So er...how do I get it not to input the variables themselves lol.

Link to comment
Share on other sites

So...

 

like this??

 

for( $i = 0; $i < $nbr_rangees; $i++ )

{

      $variablename = "something";

      $something = "" . mysql_result($results_text,$i,"name") . "";

 

      $sql_text .= "'$" . print( ${$variablename} ) . ", ";

}

 

$sql_text = substr( $sql_text, 0, strlen( $sql_text ) - 2 );

 

 

Link to comment
Share on other sites

Alright...new question because I'm working on the other side of the script where I can read from the database (as opposed to inputting to it)

 

$results_text_question = mysql_query("SELECT * FROM questions") or die(mysql_error());

$current_question = mysql_query("SELECT * FROM users WHERE testid=" . $id . "") or die(mysql_error());

$nbr_rangees = mysql_num_rows($results_text_question);

for( $i = 0; $i < $nbr_rangees; $i++ )

{

$results_text_question = mysql_query("SELECT * FROM questions") or die(mysql_error());

$current_question = mysql_query("SELECT * FROM users WHERE testid=" . $id . "") or die(mysql_error());

$questionid = mysql_result($results_text_question,$i,"id");

echo "" . mysql_result($results_text_question,$i,"question") . "<br>";

echo mysql_query("SELECT " . $questionid . " FROM users WHERE testid=" . $id . "") or die(mysql_error());

echo "<br><br>";

}

 

The above will properly get all the questions, however, when retrieving the answers it will output all of them as "1".

 

I tried doing echo "" . $questionid . ""; as a test (to see if it would output a column per question) and it worked fine.

 

I.e. it outputted

 

<question1>

<database column corresponding to question1>

 

<question2>

<database column corresponding to question2>

 

...

 

However, when doing

 

mysql_query("SELECT " . $questionid . " FROM users WHERE testid=" . $id . "") or die(mysql_error());

 

It won't work. The actual query is formatted correctly because if I do that in phpmyadmin (ex: SELECT fullname FROM users WHERE testid=60) it will give me what I want.

 

But at the moment the script simply outputs "1" for everything.

Link to comment
Share on other sites

here i made these cute little pictures that might help heh

 

This one here shows how when I echo the variable it correctly comes up with the right output on each loop.

 

http://img183.imageshack.us/img183/4019/sql1sm5.jpg

 

but then when I use it in mysql_result to actually come up with the result (as opposed to the previous example that simply echos the name of the column...) it errors out like so:

 

http://img88.imageshack.us/img88/1209/sql2gd7.jpg

 

It does the first one but then won't do the other ones.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.