Petty_Crim Posted July 2, 2007 Share Posted July 2, 2007 I want to use my string $stringvar to show text in this sql query. ATM its sticking "$player, $score" into a field, I want it to instead print it out into the sql statement. This is my code: $stringvar="$player, $score"; $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map', $stringvar, '$result')"); This is what I want the sql query thing to look like: $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map', $player, $score, '$result')"); Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 2, 2007 Share Posted July 2, 2007 Im really not sure what your asking - it looks to me like you've answered your own question. Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Im really not sure what your asking - it looks to me like you've answered your own question. The bottom is an example of the theory of it, but I want to use $stringvar not $player and $score if that makes any sense. $stringvar contains the text "$player, $score". I want to add a string with multiple variable names in it to a mysql insert statement. Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Anyone know? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 2, 2007 Share Posted July 2, 2007 Personally im still at a loss to understand what you are asking. Are you saying you want to add a new field to your database table? Are you saying that, presently, you get the exact text "$player, $score" entered into your database, rather than the values of those variables(for example, you might expect "player 1, 100" to be entered)? Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Personally im still at a loss to understand what you are asking. Are you saying you want to add a new field to your database table? Are you saying that, presently, you get the exact text "$player, $score" entered into your database, rather than the values of those variables(for example, you might expect "player 1, 100" to be entered)? I dont want to create new fields. In my database each player has their own field, with it named after them ie John has a field in my db called John. I've got a string variable $stringvar, this variable is passed through a loop and the field names are added to it ie $stringvar.=$fld->name. So once the loops is finished the value of $stringvar equals something similar to: ,John, Fred so on. All i want to do is attach that $stringvar to a mysql insert statement I already got, I want to add this to the statement to complete it, but my problem is I don't know how to do this. This is the statement atm: $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map' $stringvar, '$result')"); That statement seems to just put $stringvar (which has the values of many fields in it) into 1 field. Ie say $stringvar=", $john, $fred" it will put that into 1 field rather then putting $john into john and $fred into fred. I want $stringvar to be treated as text and as part of that mysql query statement. Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Both these statements should do the same thing: $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map' , $John, $Fred, $Larry, $Mat, $Robert, $Kevin, '$result')"); $stringvar=", $John, $Fred, $Larry, $Mat, $Robert, $Kevin"; $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map' $stringvar, '$result')"); Quote Link to comment Share on other sites More sharing options...
Petty_Crim Posted July 2, 2007 Author Share Posted July 2, 2007 Anyone know? Quote Link to comment Share on other sites More sharing options...
Beth2 Posted July 2, 2007 Share Posted July 2, 2007 OK, I'm a newbie and probably got the wrong end of the stick, but if I read your question right then try doing this: $q = ""INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map', " . $stringvar . ", '$result')" $sql=mysql_query($q); Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 2, 2007 Share Posted July 2, 2007 You need to either escape the dollar sign or use single quotes. <?php $stringvar="\$player, \$score"; // one way $stringvar='$player, $score'; // equivalent way $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map', $stringvar, '$result')"); // This is what I want the sql query thing to look like: $sql=mysql_query("INSERT INTO $game_db VALUES ('$new_matchid', '$opponent', '$map', $player, $score, '$result')"); ?> However, that won't do what you expect it to do. The variables will never be interpolated unless you use eval(). Edit: You do know, however, that you'll be getting the same results as if you just did it the way you are originally doing it, by composing a substring "$player, $score" and putting that in the mysql_query string. It doesn't matter where those variables are interpolated, unless you change them in between. 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.