How to insert a string containing the *character in the mysql database

Asked

Viewed 2,192 times

2

I am using PHP and Mysql and am trying to add a NEXTEL and CLARO radio ID field:

23*4567

But when I check what was entered, I see that only recorded what was before *, so:

23

Using a normal query like this:

INSERT INTO tabela (id_radio) VALUES ("23*4567")

I tried to use the functions, but nothing worked.

addslashes()
mysql_real_scape_string()

Some function is needed for * to be accepted by mysql?

  • 3

    That field of yours id_radio is of what type? integer, decimal, varchar ?

4 answers

2


Just change the field type to scan and insert as string in the field.

  • Perfect my friend! I broke my head all day to simply solve in this simple way.

1

If you use a parameterized query the bank will be able to insert special characters without any problem. You only need to escape the values if you are creating the query at hand, with fixed literals.

//$dbh é a sua conexão com o BD.
$stmt = $dbh->prepare("INSERT INTO tabela (id_radio) VALUES (?)");
$stmt->execute(array("23*4567"))
  • The idea is good my friend, but I know absolutely nothing of php oriented =/. I even tried but it was giving a crazy error after another and I give up.

  • An API with prepared queries and separate parameters is more robust and secure than a string-escapement-based API, regardless of whether it is implemented with objects or not. Persevere :)

1

If the field that has the radio number is numeric(int, float, Numeric) change it to varchar, this also eliminates the problem of some number starting with zero as they are removed when they are left.

The code to alter the column type is this:

ALTER TABLE tabela MODIFY  campo varchar(50)

I did a test create a table with two columns and ran this Insert directly in the database

INSERT INTO radios(radio_int, radio_varchar) values(23*4567, '23*4567')

the result was

radio_int: 105041
radio_varchar: 23*4567

0

Try sending the character char * instead of the character itself for example:

INSERT INTO tabela (id_radio) VALUES ("23".CHAR(42)."4567")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.