-4

I am using php and mysql to insert and select data from database. Yesterday I figured out a new 'error' which is: I am using the same query twice on the site -- the same query -- and different result

And of course I don't understand why the result is different. Even the numbers of rows are different. Just to help you imagine my error there is a similar query I am using:

$Query = $conn->query("SELECT * FROM `table` WHERE(ID='$id' AND isDeleted = FALSE) ORDER BY Date DESC");
$numRows = $Query->num_rows;

And the value of $numRows is sometimes greater than 0, sometimes 0. After reloading the page these values are the same or not. I mean... Once I reload the page, it seems correct but after that when I reload the page again, the error occurs again (or not).

This site is used to insert data too with method - ajax to php. For inserting data I am using $conn->prepare(); and $conn->execute();

Thank you for the help!

Dajs
  • 7
  • 1
  • 5
  • 1
    How are you making absolutely sure that $id is the same value each time? – Caius Jard May 29 '21 at 08:32
  • 1
    Note: For selecting you should also use `$conn->prepare(); and $conn->execute();` (because of [SQL injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) ) – Luuk May 29 '21 at 08:35
  • It is very simple. Look at this again. There is a query ($Query) and on the page I write echo $Query->num_rows ==> returns 2 and i am writing another html and so on and the end of the page I write again $Query->num_rows ==> and the result is 0 I am not changing the query – Dajs May 29 '21 at 08:38
  • @Luuk By using prepare and execute I can get these values as same values. But after inserting new values to this datatable my site refreshes automatically after 1500 ms. And when the page refreshes this solution is not working normally. I mean I insert 4 more rows and refresh and 1 or 0 values are showing up. After refreshing the page its okay but I have to refresh the page again. Is there any delay or something between insert and select? – Dajs May 29 '21 at 09:21
  • The problem is in the code that you are not showing. – Luuk May 29 '21 at 10:16
  • There is nothing else to show. And nothing can make issues in query or in table. – Dajs May 29 '21 at 10:40

2 Answers2

0

Try and print your queries and see if the value of id is same.

Maybe there is a include file or a script that is changing the value of id? Thanks

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Nope. That ID is constant. Nothing changes it just some rows are using. And I showed you what I do and when. It is simple. There is a page which prints out the rows twice. And then you can insert rows into it with JS. After that you can save the new rows. AJAX sends to php file a JSON string. There $conn->prepare("INSERT INTO table"); $conn->execute(); After that it sends a request if the queries were successful or not. If yes, the main page refreshes itself. After the refresh there are missing rows(sometimes all of them sometimes just few). When you manually refresh the page it works. – Dajs May 29 '21 at 10:29
0

Found the solution!

I'll share it of course in case someone else meets this issue. Later I found out the real problem with my uploader-script which is: When you insert into datatable by PHP the queries might return true value at check (

if($query->execute())

) but in sql the queries are firing one by one and the script returns true for you and it is true but when you select it too fast you might don't get all the data.

But if you are using TRANSACTION you can avoid this issue and even your inserts are safer!

Dajs
  • 7
  • 1
  • 5