Solved

Intermittent failure of mysql_fetch_assoc to return large result from select query

Posted on 2009-05-19
6
279 Views
Last Modified: 2013-12-12
I have a database that stores reports as html in a longtext field, these can be a reasonable size up to say 6Mb.  I retrieve one at a time them using a reference number and a report type number as criteria and a limit 0,1.  The query works intermittently though sometimes it returns the query other times I get a blank record.  The query is correct as it will work sometimes, and it works in phpmyadmin.  The included code is the general form of the mysql php interface in the software.
I don't get a mysql error or a php error, it simply sometimes gives a blank result when it shouldn't.
I hope someone can help asI have had no luck so far.
mysql_select_db($this->database, $this->databaseConnection);

		$selectSQL = "SELECT " . $this->SQLSelect . " FROM " . $this->SQLFrom;

		if ($this->SQLWhere != ""){ $selectSQL .= " WHERE " . $this->SQLWhere; };

		if ($this->SQLOrder != ""){ $selectSQL .= " ORDER BY " . $this->SQLOrder; };

		
 

		if ($this->SQLLimit != ""){ $selectSQL .= " LIMIT " . $this->SQLLimit; };

		$selectSQL .= ";";

		

		$data = mysql_query($selectSQL, $this->databaseConnection);

		$this->selectedData_totalRows = mysql_num_rows($data);

		$_SESSION['current_selectedData_totalRows'] = $this->selectedData_totalRows;

		$_SESSION['currentSelectSQL'] = $selectSQL;

	

		$i = 0;

		while ($row_data = mysql_fetch_assoc($data)) {

			$this->selectedData[$i] = $row_data;

			$i++;

		} 

		mysql_free_result($data);

		

		if ($returnData == -1){

			$this->setSelectedDataToSession();

		} else {

			return $this->selectedData;	

		}

Open in new window

0
Comment
Question by:photondesign
  • 3
  • 3
6 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24423088
Given the code above you have no way of knowing whether the MySQL query has run correctly or not!

Try using something like the code snippet...

Best regards, ~Ray
$data = mysql_query($selectSQL, $this->databaseConnection);

if (!$data)

{

   $err = mysql_errno() . ' ' . mysql_error();

   echo "<br/>QUERY FAIL $selectSQL <br/>\n";

   die ($err);

}

Open in new window

0
 
LVL 1

Author Comment

by:photondesign
ID: 24424681
I forgot to mention.  I have been running the code with a mysql_error() inserted into the php error log, but it was not producing any errors.  I have also tried high_priority and sql_no_cache on the select statement, although it is an innodb table so not sure the high_priority has any effect anyway.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24424865
OK, add something like the mysql_error() checker to the above code.  Any clues as to what determines the intermittent nature of the error?  Particular time of day or any other factor that can help generate a repeatable error?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 1

Author Comment

by:photondesign
ID: 24427168
What I meant by my last comment was I have tested it with MySQL error written to the log.  No errors are written even when the empty result is returned.  It will be a case of works then two minutes later it doesn't return anything.  No other users are accessing the row, no other users are even accessing the table.  It is not at any time of the day in particular.  It can be when no other users are even connected to the server or database. It can work then not work and then work again in the space of five minutes.  It really is frustrating.
0
 
LVL 1

Accepted Solution

by:
photondesign earned 0 total points
ID: 24431229
I have finally managed to fix the issue and it turned out to be MySQL rather than the PHP code or query itself.  I changed the transaction isolation type READ-COMMITTED in the my.ini (my.cnf for linux,etc) using the line transaction-isolation = READ-COMMITTED in the [mysqld] section.  The queries now all return a result every time.

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24431395
Excellent - glad you were able to find a solution.  Best regards, ~Ray
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Integrate Office 365 calendar events into WordPress sites 2 52
How can i modify my File download link ? 6 47
Trigger usage 2 59
how to use a function in heredoc 5 21
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now