dp69_2001 Posted September 17, 2011 Share Posted September 17, 2011 WTF is wrong with this syntax?? I've mixed it up quite a bit since I first typed it so hopefully it's not worse off than when I started. $refresh = "INSERT INTO temp (name, alliance, x coord, y coord " . "SELECT 'player', 'alliance', 'x', 'y' " . "FROM 'players' ") ; Again, I love and appreciate all of your help... I see myself becoming a regular here once I actually learn something :/ Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/ Share on other sites More sharing options...
dp69_2001 Posted September 17, 2011 Author Share Posted September 17, 2011 Also, sorry for the double post. But, I had it typed in the same manner as shown: <a href="http://www.1keydata.com/sql/sqlinsert.html">here</a> and was still experiencing the same sort of errors... which currently is: Parse error: syntax error, unexpected ')' in /home/content/64/4940664/html/artofwar/refresh.php on line 6 line 6 is "FROM 'players' ") ; Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270205 Share on other sites More sharing options...
Shadow Jolteon Posted September 17, 2011 Share Posted September 17, 2011 I believe that since they are two SQL statements, they need to be separated by a colon. Something like this: $refresh = "INSERT INTO temp (name, alliance, x coord, y coord " . "SELECT 'player', 'alliance', 'x', 'y' " . "FROM 'players' ") ; $refresh = "INSERT INTO temp( name, alliance, xcoord, ycoord ); SELECT 'player', 'alliance', 'x', 'y' FROM 'players';"; I hope this helps. =) Edit: Also, the cause for that parse error was likely because the ending parenthesis was outside of your last quotation mark. Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270206 Share on other sites More sharing options...
dp69_2001 Posted September 17, 2011 Author Share Posted September 17, 2011 Awesome, that did fix the syntax errors. But, I'm not getting any data into the new 'temp' table... I'm simply trying to pull the columns player, alliance, x, y, from the 'players' table; and place them in name, alliance, x coord, y coord, in the temp table... Well actually, it's more complicated than that... I want to SELECT those four columns from the data which are LIKE 'whatever column data' and have it placed into the 'temp' table... Which doesn't seem to be working... lol. Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270207 Share on other sites More sharing options...
creata.physics Posted September 17, 2011 Share Posted September 17, 2011 Of course your query isn't inserting anything, you need to define what values to insert. "INSERT INTO table (name, alliance, x coord, y coord) VALUES ($name,$alliance,$x,$y)" Also don't forget to call the function itself: <?php $query = mysql_query($refresh); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270209 Share on other sites More sharing options...
cssfreakie Posted September 17, 2011 Share Posted September 17, 2011 your missing values (edit: as creata.physics already mentioned) as far as that second query, I highly doubt that you can add a second query like that in php mysql ( $mysqli->query) edit: ah it is possible, see: http://www.php.net/manual/en/mysqli.multi-query.php Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270210 Share on other sites More sharing options...
creata.physics Posted September 17, 2011 Share Posted September 17, 2011 cssfreakie is right, you'll need to separate queries to perform this task. <?php mysql_query("INSERT INTO table (name, alliance, x coord, y coord) VALUES ($name,$alliance,$x,$y)"); $refresh = mysql_query("select player, alliance, x, y from players"); ?> Of course to do what you want you'll need to figure out what else to do with the code above, but it's a far better point than where you were at. Edit: yeah it's possible with mysqli but the thread starter didn't say anything about using mysqli Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270211 Share on other sites More sharing options...
Shadow Jolteon Posted September 17, 2011 Share Posted September 17, 2011 Ah, I think I understand... That would be a bit more complex, and I'm not sure of how to do it just with SQL (if it's even possible, I'm not very good with more advanced SQL XD), but you can definitely do it with PHP... If you first run the SQL to select what you want to place into your temp table, then fetch the result(s), binding them to a variable and then run another SQL that inserts a new row with that data... What I mean is something similar to this: <?php $mysqli = new mysqli( 'localhost', 'my_user', 'my_password', 'world' ); $thing1 = 'something'; $getSql = "select `player`, `alliance`, `x`, `y` from `players where `something` like ?;"; if( $runSql1 = $mysqli -> prepare( $getSql ) ) { $runSql1 -> bind_param( 's', $thing1 ); $runSql1 -> execute(); $runSql1 -> bind_result( $var1, $var2, $var3, $var4 ); $runSql1 -> fetch(); $runSql1 -> close(); } $placeSql = "insert into `temp` values( ?, ?, ?, ? )"; if( $placeIt = $mysqli -> prepare( $placeSql ) ) { $placeIt -> bind_param( 'ssii', $var1, $var2, $var3, $var4 ); $placeIt -> execute(); $placeIt -> close(); } $mysqli -> close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270212 Share on other sites More sharing options...
dp69_2001 Posted September 17, 2011 Author Share Posted September 17, 2011 Ha, I'm entirely confused now... Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270222 Share on other sites More sharing options...
jcbones Posted September 17, 2011 Share Posted September 17, 2011 You should be with everything that was posted. You were on the right track to begin with. $refresh = "INSERT INTO temp (name, alliance, x coord, y coord ) SELECT player, alliance, x, y FROM players"; //missed a space. INSERT...SELECT MySQL Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270260 Share on other sites More sharing options...
creata.physics Posted September 17, 2011 Share Posted September 17, 2011 Hm, seems it is possibly but not very efficient. the link that jcbones posted contains this important information: The order in which rows are returned by a SELECT statement with no ORDER BY clause is not determined. This means that, when using replication, there is no guarantee that such a SELECT returns rows in the same order on the master and the slave; this can lead to inconsistencies between them. Your code may do what you want it to do, or it may not. Go ahead and try what jcbones suggested, just make sure you execute the query by doing <?php mysql_query($refresh); ?> If your code doesn't work after that let us know. Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270280 Share on other sites More sharing options...
dp69_2001 Posted September 21, 2011 Author Share Posted September 21, 2011 You should be with everything that was posted. You were on the right track to begin with. $refresh = "INSERT INTO temp (name, alliance, x coord, y coord ) SELECT player, alliance, x, y FROM players"; //missed a space. INSERT...SELECT MySQL Hm, seems it is possibly but not very efficient. the link that jcbones posted contains this important information: The order in which rows are returned by a SELECT statement with no ORDER BY clause is not determined. This means that, when using replication, there is no guarantee that such a SELECT returns rows in the same order on the master and the slave; this can lead to inconsistencies between them. Your code may do what you want it to do, or it may not. Go ahead and try what jcbones suggested, just make sure you execute the query by doing <?php mysql_query($refresh); ?> If your code doesn't work after that let us know. You guys are absolutely amazing... That worked like a charm! Exactly what I was after.. Also, I'm glad I wasn't too far off. Because I wasn't thinking it should be too complicated, and half of the suggestions made me think I was retarded.... Quote Link to comment https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1271573 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.