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