mrbuter Posted March 1, 2008 Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 1, 2008 Share Posted March 1, 2008 You can't concatenate a for loop. Build a string with a for loop and then concatenate the containing variable. Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 hmmm what exactly are u trying to do? also hav u connected to db? Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 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 ); Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 Alright thanks for the replies so far...playing around with the code QUietWhistler gave me. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 For one thing, you have to strip the last ", " in your $sql_text. You can do that like this: $sql_text = substr( $sql_text, 0, strlen( $sql_text ) - 2 ); after each for loop. Let me know if that solves your problem. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 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` Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 What is the code you are using now? Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 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()); Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 ${$variablename}, use it like that and it'll give you for example: $variablename = "butter"; $butter = "lala"; print( ${$variablename} ); that will print: lala. Hope that helps. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 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 ); Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 for( $i = 0; $i < $nbr_rangees; $i++ ) { $sql_text .= ${mysql_result($results_text,$i,"name")} . "', "; } You can use it like that. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 That one is missing a leading ' (so it comes out as $value1', $value2', $value3') and I tried putting the ' in different places but they all give a encapsed_string error or something and I cant find where to put it rofl. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 Nevermind, finally got it and working perfectly! Thanks a lot Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 1, 2008 Author Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 2, 2008 Author Share Posted March 2, 2008 bump Quote Link to comment Share on other sites More sharing options...
mrbuter Posted March 2, 2008 Author Share Posted March 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.