mysql rows are deleted by themself - php

I am developing a web application based on php/laravel. It is about to finish and we are testing it nowadays. But there is a problem with mysql table.
I have a table called follows.If someone follows another, I create a record on this table. There is no problem here but periodically records on this table is deleted and it is not triggered by user who doesn't want to follow someone. Or there is no intervention by system admin. Even more, I have 20+ tables and just follows table has this problem.
I checked my codes and I couldn't find any reason what causes this problem. I inspected mysql table on phpmyadmin and again I couldn't see anything wrong. Now Can you say 'there is something like that and causes this problem'? Or how can I debug this problem on mysql? Is it good trying to go for triggers? I need to find what is causing this problem.
There is no more I have to say, and I am about to go crazy..
Please write down what do you need to here extra that I forget to write here..

Related

Create Table Just Once?

I have a few nagging questions about creating tables:
If I use PHP to create a MySQL function to create a table, I know it works the first time (to create a database for usernames and passwords) but what about the following times when the database sees the code to "create table". It seems to ignore it on my virtual server, but I was just wondering if this is wrong. Does it keep trying to create a new table each time? Is it okay to leave that code in?
Another question I have is, let's say I go into PHPMyAdmin and add a column called "role" (to define the user's role). The sign in page will crash since I added a column in PHPMyAdmin, but if add the column using PHP/MySQL it is perfectly fine. Why is that?
CREATE TABLE is executed each time you run the function. It's better to replace the syntax with CREATE TABLE IF NOT EXISTS.
The keywords IF NOT EXISTS prevent an error from occurring if the
table exists.
If you does not add IF NOT EXISTS it will throw the error.
Reference: http://dev.mysql.com/doc/refman/5.7/en/create-table.html
Please post your code in question to help you with second query.
1.) It depends on the purpose of the table.
If you need to create tables dynamically then your code should check each time
if the table exists:
CREATE TABLE IF NOT EXISTS 'yourTable'
However if you create the table only ones, there is no need to check for existence over and over again, so the code to create these table(s) should execute one time only.
2.) You need to update the function that does the insert or read after adding a column via PHPMyAdmin. It's difficult to answer your second question as I don't know what your functions do.
Do not keep your CREATE TABLE ... statements in your PHP code so that they execute every single time on every single page load. It's unnecessary and error prone. The statements are not being ignored, very likely they are run and are producing errors, and you're simply not checking for errors.
Database creation is a deployment step, meaning when you upload your code to your server, that's the one and only time when you create or modify databases. There are entire toolchains available around managing this process; learn something about automated deployment processes and database schema versioning at some point.
No idea without seeing your code and the exact error message.

Best way to update data in mysql online database

I have an online sql database with a few tables for users matches and bets. When I update the result of any game, I need the status of all bets containing that game in the bet table to be updated. So for example if I update game 8 with the result home win I need all bets which have game 8 in them to be updated as either lost, won or still open.
The way I do this currently is that when the user turns on my android app, I retrieve all the information about the games and all the information about the user's bets using asynctasks. I then do some string comparisons in my app and then I update the data in my database using another asynctask. The issue is that this wastes a lot of computation time and makes my app UI laggy.
As someone with minimal experience with php and online databases, I'd like to ask is there a way to carry out these things in the database itself either periodically (every 3 hours for example) or whenever the data in the gamestable is changed using a php file for example which is automatically run?
I tried looking for some kind of onDataChanged function but couldn't find anything. I'm also not sure how to make a php file run and update data without getting the app involved.
Another idea I had was to create a very simple app which I wouldn't distribute to anyone but just keep on my phone with an update button which I could press and trigger a php file to carry out these operations for all users in my database.
I would appreciate some advice on this from someone who has experience.
Thanks :).
You can easily execute php script periodically if your hosting provider supports script executors like Cron.
About updating game status multiple times, first check tables engine. If you are using engine like InnoDB you can create relationship between those tables, so updating status of one row will affect all connected to them.

Drupal / phpMyAdmin Issue

Came across a weird issue today.
Have a Drupal site, been live for a few years now, and this is actually the first time I've had to look at anything with it.
I needed to check out one of the tables in the MySQL database, anything I select for it throws a 500 Server Error popup, but does not do this for all tables.
I thought maybe a repair would do, but of course InnoDB doesn't support that, so I am at a loss to what the issue could be, and how I can get into this table to view/manually modify some records.
Can anyone help?

How do I restrict MySQL user from dropping, and adding tables and databases

I am trying to find the SQL command to create a user that can read and write in tables found in one database, but is not allowed to do anything else.
Also if you can recommend other limits I should add to the MySQL user I would appreciate it.
The reason I am asking is because one of my tables was dropped and I have no idea how... I think it's PHP but I am not sure... Maybe someone connected via my SSH... The thing is all my mysql logs are empty and no evidence of a another user trying to do something are to be found in the other logs...
So for now I am assuming there is something wrong with my PHP... This is why I would like to limit it to only adding, removing, and dropping rows and content in the rows... But not more. So that I don't loose my table again or my other DBs.
Create the user using CREATE USER, and specify the privileges using GRANT.
http://dev.mysql.com/doc/refman/5.1/en/grant.html (click here for a list of MySQL privileges)
If you have phpMyAdmin running, the interface will give you a complete set of privileges to check/uncheck :)
For a basic web application, you'll only need SELECT, INSERT, UPDATE and DELETE :)
If you think there was a security issue with your application, check this page to know more.

Optimizing and compressing mysql db with drupal

I have developed a news website in a local language(utf-8) which server average 28k users a day. The site has recently started to show much errors and slow down. I got a call from the host saying that the db is using almost 150GB of space. I believe its way too much for the db and think there something critically wrong however i cannot understand what it could be. The site is in Drupal and the db is Mysql(innoDb). Can any one give directions as to what i should do.
UPDATE: Seems like innoDb dump is using the space. What can be done about it? Whats the standard procedure to deal with this issue.
The question does not have enough info for a specific answer, maybe your code is writing the same data to the DB multiple times, maybe you are logging to the table and the logs have become very big, maybe somebody managed to get access to your site/DB and is misusing it.
You need to login to your database and check which table is taking the most space. Use SHOW TABLE STATUS (link) which will tell you the size of each table. Then manually check the data in the table to figure out what is wrong.

Categories