Jump to content

Adding strings to a query string?


Petty_Crim

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

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.