Jump to content

Recommended Posts

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 :/

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/
Share on other sites

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' ") ;

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270205
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270206
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270207
Share on other sites

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); ?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270209
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270210
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270211
Share on other sites

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();
?>

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270212
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270260
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1270280
Share on other sites

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....

Link to comment
https://forums.phpfreaks.com/topic/247324-and-now/#findComment-1271573
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.