asevie Posted March 6, 2012 Share Posted March 6, 2012 First project in php and mysql, I've searched but can't find the answer to stringing together two numbers and a "-" without php giving me the results as it thinks I'm creating an equation. I need the string as text. Example, I need the following as a text string, id_have (INT) and id_have (INT) joined together as id_have-id_want, or, as they will be numbers, 23-45. When I use this; $havewant = $id_have . "-" . $id_have; The result will be $id_have (23) minus $id_want (45), I get -22, not what I'm looking for!!! I need the text string of "23-45" to store in a table. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/ Share on other sites More sharing options...
marcus Posted March 6, 2012 Share Posted March 6, 2012 <?php $id_have = 23; $id_want = 45; $haveWant = $id_have . "-" . $id_want; var_dump($haveWant); // results: string(5) "23-45" ?> Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324352 Share on other sites More sharing options...
asevie Posted March 6, 2012 Author Share Posted March 6, 2012 <?php $id_have = 23; $id_want = 45; $haveWant = $id_have . "-" . $id_want; var_dump($haveWant); // results: string(5) "23-45" ?> Thanks for the quick reply! I'm using the variable in a mysql statement, how could I force that inside of the select statement? It looks like this; $query = "INSERT INTO Want (id_version,id_customer,id_have,havewant,date_created) VALUES ($id_version,$_SESSION[id_customer],$id_have,$havewant,'$created')"; One other thing, I noticed that you changed the variable name from havewant to haveWant, any reason? Personal preference or something I missed!? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324355 Share on other sites More sharing options...
marcus Posted March 6, 2012 Share Posted March 6, 2012 If you want it as a string value you need to wrap it in quotes. The variable change is irrelevant, I like camel case. $query = "INSERT INTO `Want` (`id_version`,`id_customer`,`id_have`,`havewant`,`date_created`) VALUES('".$id_version."','".$_SESSION['id_customer']."','".$id_have."','".$havewant."','".$created."')"; Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324356 Share on other sites More sharing options...
asevie Posted March 6, 2012 Author Share Posted March 6, 2012 I've tried several different versions of this; $havewant = "$id_have"."-"."$id_want"; or this $havewant = "$id_have-$id_want"; I still end up with the resultant of id_have minus id_want. Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324545 Share on other sites More sharing options...
ManiacDan Posted March 6, 2012 Share Posted March 6, 2012 MySQL and PHP are separate languages. Your problem is not in PHP. your problem is that you're building a MySQL string that says: INSERT INTO mytable ( someField ) VALUES ( 19-23 ); That will put -4 into someField. QUOTE YOUR STRING INSIDE THE MySQL QUERY. Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324550 Share on other sites More sharing options...
asevie Posted March 6, 2012 Author Share Posted March 6, 2012 MySQL and PHP are separate languages. Your problem is not in PHP. your problem is that you're building a MySQL string that says: INSERT INTO mytable ( someField ) VALUES ( 19-23 ); That will put -4 into someField. QUOTE YOUR STRING INSIDE THE MySQL QUERY. Thank you kindly! This works as needed... INSERT INTO Want (id_version,id_customer,id_have,havewant,date_created) VALUES ($id_version,$_SESSION[id_customer],$id_have,CONCAT($id_have,'-',$ridv),'$created')"; Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324560 Share on other sites More sharing options...
ManiacDan Posted March 6, 2012 Share Posted March 6, 2012 Not really, no. Simply putting quotes around it inside the SQL would have been fine, but working is working. Quote Link to comment https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324562 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.