Mysql query with PHP

Asked

Viewed 656 times

1

I’m making an appointment at Mysql to return the quantity of orders that were not delivered.

To see what was delivered I do:

$result = mysql_query("SELECT operacional FROM `pedidos` WHERE `status` = 'ENTREGA REALIZADA'");

However, there are some requests that status is: DELIVERY HELD ON 20/10/2017 or even DELIVERY HELD ON 20/11/2017 - that is, the dates vary.

And besides, I need to take the ones that were not delivered. How can I receive only the items that do not have this text of "DELIVERY PERFORMED"? Because this space never goes blank, there will always be something like OUTING or UNIT ENTRY, among others...

There is something like:

$result = mysql_query("SELECT operacional FROM `pedidos` WHERE `status` != 'ENTREGA REALIZADA'");
  • 2

    SQL is still on my list for in-depth studies, but doing something like status not like "ENTREGA REALIZADA% functionary?

  • Guys, I did it like this: I created a new column in DB, "active" being it of type INT, if it is 0 php reads and updates, if it is 1 it jumps... it worked!

  • Peter, feel free to draft an answer and post it here as a solution. It is not bad conduct to do this, in fact, it is even well supported since it well done. Read more on Answer your own question.

  • 1

    The problem with the original code is that a violation of the 1NF. You are using two distinct data in the same column. The best option would be to have status and data, That way there would be no problem, each one serves for a thing and only saves one information. As also stated by @Alisson Acioli you can use numeric fields (TINYINT) to decrease the space used and make Indexes more efficient, another option (maybe not the best) is to use the ENUM.

  • 1

    Remember that the mysql_* functions are obsolete and have been removed in php 7. It is recommended to use mysqli_* or PDO

  • Thank you guys, you helped me a lot, the fact that I need to make all this complication is because the information comes to my bank through a webservice, only after that I can manipulate... All helped me a lot! Strong hug!

Show 1 more comment

2 answers

4

Friend a tip I give you is never use text in fields like status. Instead, you could solve this easily using the field status as INT. Doing something like that:

0 = Não Realizado
1 = Entrega Realizada
2 = Saída para Lista
3 = Entrada na únidade

And in the question of the delivered date, you could create one more field in the table called (for example) from data_status and whenever the status is 1 you update this column and put the date.

If you want you could also create a separate table only with status for consultation, something with the structure like this:

id
id_status
nome_status

So you can avoid future problems.

With your case above

I could test the following command and see which one fits best:

$result = mysql_query("SELECT operacional FROM `pedidos` WHERE `status` LIKE '%ENTREGA REALIZADA'");

$result = mysql_query("SELECT operacional FROM `pedidos` WHERE `status` LIKE '%ENTREGA REALIZADA%'"); //Talvez dê problemas no futuro caso tenha frases muito parecida no campo status

$result = mysql_query("SELECT operacional FROM `pedidos` WHERE `status` LIKE 'ENTREGA REALIZADA%'");

But do as I said at the beginning of the post, it is better, you will avoid problems and future headaches.

Remembering how the Anderson Carlos Woss quoted, use the function mysqli_ for all mysql_ are obsolete in new versions of PHP.

  • 1

    Like commented, it may be interesting to adapt the solution to mysqli_* or at least put the warning that the functions mysql_* are obsolete.

0


Thank you all! I confess that I solved my problem and learned a function that I did not yet know.

For this problem I decided to create an "Active" table to know whether or not the order enters the operation (which is to receive the data update):

MySql - Coluna Ativo

If it is 0 he gets the update when the routine happens, if it is 1 he skips this request.

If the order that received the information has as Status -> DELIVERY PERFORMED the asset changes to 1:

inserir a descrição da imagem aqui

I think that makes it safer! Strong hug and thanks for the information, were of great importance!

Browser other questions tagged

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