I have a PHP script designed to scrape data from websites. The script checks a locally-hosted mysql database each time it finds a new item to see whether or not that item has already been downloaded and already exists in the Mysql database. If it sees the item already exists in the database, it should ignore it and move on. This is the code I am using to do that:
$result = mysql_query("SELECT * FROM web_media WHERE sourceForum LIKE '%$ForumtoGrab%' AND titleThreadNum=$threadTitleExists");
if((!mysql_num_rows($result)) && (mysql_num_rows($result) !== FALSE))
{}
In other words, if it comes up with zero results, then that item is considered new. This script ran fine on my old hosting company for several months. I have recently moved to a new hosting provider, and I'm suddenly running into a very strange issue. Every 12 hours or so, the expression seems to randomly fail and the script finds a bunch of "new" data that already exists in the mysql database. I've tried running the query manually, and the code appears to have no problem finding the pre-existing entry no problem.
Does anyone have any idea what's going on here? I already checked with the hosting provider, and they say that the number of aborted Mysql connections we have is very low and isn't anything to worry about…so it doesn't seem like it's an issue with MySQL itself. I suspect it may be an issue with the mysql query?
Thanks
Try to check the MySQL error log for errors in your query.
PS: I hope you are preparing the $threadTitleExists for usage in a MySQL query (smth like (int)$threadTitleExists or mysqli_real_escape_string($threadTitleExists))
Related
I have a MySQL db that I am accessing via a mysqli instance. All CRUD operations were working flawlessly until I exported the db and imported it onto a new server (using the new server now). Now, every table is working great and returning all records with a simple SELECT * FROM tablename ORDER BY ASC, except for one particular table where I am getting a completely empty response (not even an empty array, literally nothing at all) from this table while all the others return as expected.
The code is exactly the same for all tables with only the table name changed. Using WHERE to filter the results and obtain a single record works, I'm able to obtain a single filtered result as expected. Adding records to the table also works.
When copying and running the SQL queries directly in phpMyAdmin, I get the full table results just fine.
Tried using procedural methods rather than object oriented as well as iterating through results with while(), same result.
Both servers are running PHP 7.0, and in phpMyAdmin everything looks completely identical.
Code removed at the advice of commenters.
I'm really banging my head against the wall on this one.
After spending all morning on this, I figured it out a few minutes after posting here. Leaving it up in case anyone else runs into this issue since it was a real head-scratcher.
Somehow one of the records in the database became... Corrupted maybe is the right word? I started removing records one by one and as soon as I found the right one to remove, everything started working.
Hope that this helps someone else!
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..
I have some code that creates a user and later down the line it checks if the user is real. Essentially this:
// INSERT statement fired in here
$user = self::_createUser( $params );
// Performs a sanity check by hitting DB with
// SELECT for the ID returned from creation within object
if ( !$user->isReal() ) {
throw new Exception( "User failed to create: " . var_export( $params, 1 ), MYCODE );
}
Since the user was just created this exception should never be thrown. This never happens in production, or my sandbox environment. However our test environment uses Jenkins to kick off multiple tests at once, where the lines above live.
The exception will be thrown at random, on different tests at each run of our suite.
We turned on all MySQL logging and found that the sanity SELECT is being called BEFORE the INSERT however the SELECT is clearly selecting the proper ID from the DB - which it couldn't have unless the create worked.
How can MySQL server randomly receive a query in the wrong order? Never seen anything like this before.
EDIT Here is more code for clarification
function _createUser( $params ) {
// db returns connection using Zend, which translates to something like
// INSERT INTO users SET name='a'
// Returns ID of row inserted
$this->_id = self::db()->insert( 'users', $params );
}
function isReal() {
// Returns false when row is not there
return self::db()->fetchRow( "SELECT * FROM users WHERE id={$this->_id}" );
}
Also the MySQL logs show the query as I expect in all cases, with NO DEFER
EDIT 2
Using command line rather than Jenkins to run tests parallel is still making this happen.
Meanwhile it will only run up to 7 tests at the same time, and there no where in the code that would delete the user at the time. There are no blanket deletes except BEFORE all the tests run.
EDIT 3
Okay so in the tests that run, there are some persistent connections. One is for MySQL and one is for Mongo. Before my suite runs it was wiping MySQL by rebuilding DB from scratch, and erasing memcache. It was not doing this for mongo and thus was causing some other random errors. Once I added mongo to the reset script, it seems that the MySQL errors went away. This makes zero sense to anyone on my team or myself. Can anyone make sense of that?
I don't know why, but two days later, this error went away. It went away like my 3rd edit said... after I made sure that when I emptied MySQL and memcache, I also emptied mongo. The best guess I have is that when bad mongo data caused an exception, PHP sputtered in some way. Not sure if it's a PHP bug or if I'd sound crazy trying to report it.
Although I'm posting this answer, I'd still appreciate any additional input.
a very puzzling and exhausting problem of mine:
I developed an API to allow others to draw information from my database. My server collects the POSTed info, writes up a mysql query, performs the query [$query = mysql_query($string, $connection);] , and returns the results (very simple).
the problem is that sometimes (say 1 out of every 5 tries) no info is returned. My server logs say that the resource ($query) is boolean (and therefore no results). My server receives the info from the remote users of the API every single time, the problem seems to be that my queries are sometimes just not being performed...
Why is this happening?
Is it a mysql performance issue? I never seem to have even a hint of a performance issue for queries made on my own page (i.e. not from the API)!
please help...
Your query might be failing. Try doing this:
mysql_query($string, $conn)or die(mysql_error());
If the query is generating an exception/error, it will stop the script and display the MySQL error. Using that error, you can fix your query so that everything will work fine eventually.
By the way, you are using $string, but it might be a better idea to use $builtQuery, because "string" might be confusing if you are going to need to edit the script later on.
Greetings.
I have added a new record to a table using phpMyAdmin
I then run select * within phpMyAdmin and it works listing all records.
However if run the same select * from a PHP page the new record is missing.
I am using PHP 5 and am not sure what is going on here. It used to work in PHP 4.
.php files run as PHP 5 Apache module mode.
Here are a few things you can investigate:
You could be connecting to a different database from the one you think you are (do you have test and production databases?).
You may have an error or typo in your SELECT statement. Copy and paste exactly the query that works into your code. Do not manually edit it afterwards.
You might select the row correctly but display it incorrectly so that it appears not to be there. Try checking how many rows there are in your result set.
Try put sql query to variable and then dump it to log or use var_dump. Then put query to phpMyAdmin and check errors.
Thanks for the answers but found out the answer now.
I had bookmarked a link to an older phpMyAdmin server which looked almost identical and allowed me to log in etc. My host should have forwarded me to the latest server.
I am now using the correct server. Hope this helps someone else.