Does anyone know why Mysql Select sometimes doesn’t work?

Asked

Viewed 573 times

1

I am facing a problem, in which I have a table in the BD and it contains the data. However when I do the SELECT, some data it returns and others not. For example:

On the table Codigos i have the following data.

|idEquipamento|idCodigo|
|      1      |  123   |
|      2      | abc123 |

And when I do the select one returns result and another does not:

"SELECT idEquipamento, idCodigo FROM Codigos WHERE idCodigo = 123"; 

Returns result!

"SELECT idEquipamento, cdCodigo FROM Codigos WHERE idCodigo = 'abc123'"; 

It does not return any record, as if it did not exist. But when I open the table the data is there, I can edit and everything!

Note: I am doing the SELECT by phpMyAdmin himself!

  • the idCodigo is a varchar or int? see that the fields you search for in the query are not in the question table, see Codigo and cdCodigo.

  • Sorry I misspelled the question but the fields of Where in the case are: WHERE idCodigo = 123; and WHERE idCodigo = 'abc123'; Yes they are VARCHAR!

  • 1

    @Jonatasm If you wrote something wrong, edit the question to avoid confusing the other users who answer.

  • Since varchars are, can be have been recorded with space of a trim() to ensure and remember to use simple quotes in the values.

  • 2

    What a SELECT * FROM eq_Codigos returns? Why do you have Codigos and eq_Codigos? What is the difference between these two tables?

  • Sorry was correcting the question because, the two select are in the same table, it simply returns: Mysql did not return any record. (The consultation took 0.0094 seconds.)

  • 1

    To remove the doubt you could take the create from your table and some Inserts and test in sqlfiddle there put the link here, this will eliminate the environment problem if it exists.

  • Dude I don’t know what sqlfiddle is and nor how us-Lo! I’ll give a search on this!

  • In the second consultation is cdCodigo or idCodigo?

  • What is the name of your database?

  • do select without Where copy the test you and user it in Where the way it is returning and see what’s strange.

Show 6 more comments

2 answers

2


Table: Code (with C capital)

|idEquipamento|idCodigo|
|      1      |  123   |
|      2      | abc123 |

Test this command:

SELECT idEquipamento, idCodigo FROM Codigos WHERE idCodigo = 'abc123'

It should return the second line. Then test these 2 more commands to see what returns:

SELECT idEquipamento, idCodigo FROM Codigos WHERE idCodigo = '123'

SELECT * FROM Codigos WHERE idCodigo = 'abc123'

Also try with TRIM()

SELECT * FROM Codigos WHERE TRIM(idCodigo) = 'abc123'
  • Then, when I search by idCodigo 123 it returns the first normal line, only when I search by idCodigo abc123, it appears the message: Mysql did not return any record. (The consultation took 0.0094 seconds.)

  • Tested the other 2 commands I put ?

  • Yes Yes I tested it! I even tested it using LIKE instead =

  • @Jonatasm added another command there with TRIM() see if it returns any results

  • I just tested, it gives the same message: Mysql did not return any record. (The query took 0.0018 seconds.)

  • 2

    @Jonatasm if possible, click on Exportar in your Mysql and exports the . SQL file and pastes its contents into your question, so we can do tests here on our Mysq. Doing this let me know here.

  • Try using the like and tell me if the result appears: SELECT idEquipment, idCodigo FROM Code WHERE idCodigo like '%abc123%'

  • Export the database to get a better view of the problem and solve it

  • Then, I discovered that at the time I do the Insert in the BD, it goes with a "/r" at the end of the Insert. But I’m not able to handle the variable in php not to insert with this information in the BD!

  • See if a str_replace() works.. Like $conteudo = str_replace("/r", "". $conteudo_aqui);

Show 5 more comments

0

When you use the where is the same as when.

In Where idCodigo = 'abc123', You’ll just pull the line where the column idCodigo is equal to 'abc123', if you want the two lines strip the where

Browser other questions tagged

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