Jump to content

String concatenate...


asevie

Recommended Posts

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.

 

:shrug:

 

Thanks!

 

:thumb-up:

 

Link to comment
https://forums.phpfreaks.com/topic/258362-string-concatenate/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324355
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324356
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324550
Share on other sites

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

 

:facewall:  :happy-03:

 

Link to comment
https://forums.phpfreaks.com/topic/258362-string-concatenate/#findComment-1324560
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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