Php mysql unable to save result set in

Php mysql unable to save result set in

I'm getting the following error message when I execute a query (such as insert or delete or update but not select) that doesn't produce any results: Warning: MySQL: Unable to save result set in /path/to/sql.php3 on line 33 MySQL said NOTHING and the query was successful executed. I have set "display_errors = Off" in my php.ini, so no warnings should be shown , but php script can not go on at the line where the Warnning should display. I configured php with the following command: ./configure --with-mysql=/usr/local/mysql --enable-track-vars --with-gd=../../gd/gd1.3 --with-ftp --with-imap=../../mail/imap/imap-4.7c --enable-memory-limit --with-pgsql=/usr/local/pgsql MySQL is version 3.23.38 and PostgreSQL is version 7.1.2 and compiled with th following command: ./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --with-charset=gb2312 --with-extra-charsets=all ./configure --enable-multibyte --enable-unicode-conversion --with-perl --with-openssl=/usr/local/ssl --enable-odbc --with-CXX

Patches

Pull Requests

History

Please include a shortest possible example script that can be used to reproduce this problem.
I'm getting the following error message when I execute a query (such as insert or delete or update but not select) that doesn't produce any results: Warning: MySQL: Unable to save result set in /path/to/sql.php3 on line 33 MySQL said NOTHING and the query was successful executed. I have set "display_errors = Off" in my php.ini, so no warnings should be shown , but php script can not go on at the line where the Warnning should display. I configured php with the following command: ./configure --with-mysql=/usr/local/mysql --enable-track-vars --with-gd=../../gd/gd1.3 --with-ftp --with-imap=../../mail/imap/imap-4.7c --enable-memory-limit --with-pgsql=/usr/local/pgsql MySQL is version 3.23.38 and PostgreSQL is version 7.1.2 and compiled with th following command: ./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --with-charset=gb2312 --with-extra-charsets=all ./configure --enable-multibyte --enable-unicode-conversion --with-perl --with-openssl=/usr/local/ssl --enable-odbc --with-CXX BTW : The script is perfect on mysql-php-4.0.3pl1 and postgreSQL-php-4.0.6 .
The script(error.php) is : The result in browser is: Warning: MySQL: Unable to save result set in /home/0/admin/error.php on line 7 But I just found that NO error in command line root@localhost # php ./error.php X-Powered-By: PHP/4.0.6 Content-type: text/html
Try adding this lines after each mysql function: echo mysql_errno().": ".mysql_error().""; This way you should get the reason why it doesn't work. And FYI: Use mysql_query() / mysql_select_db() instead of mysql_db_query(). --Jani
Yes I use Use mysql_query() / mysql_select_db() instead of mysql_db_query(). Both mysql_errno() and mysql_error() return NOTHING. The query was successful exectued by mysql.(I can find the query reselt in mysql database) But why php generated an error?
BTW : This error occur on ALL OF my scripts after I had upgraded my php from 4.0.3 to 4.0.6
I upgraded to mysql-3.23.39 and recompiled php-4.0.6 but nothing changed.
Bogusified in favour of #12029. Please only respond to #12029! Bogus
When you have message: "Unable to save result set error 127" May be your table is corrupt Use the SQL command "CHECK TABLE table_name" to see if everything is Ok on your table. I got this error after I shutdown Windows unproprely (scandisk started). Seems I lost all my data! Make some backups! Hope this help
The LIMIT is ON, Try, LIMIT 0,10!

Источник

Читайте также:  Html javascript new date

Php – MYSQL/PHP Unable to save result set

I am running a PHP script which basically tries to find matching names from MYSQL database and then assigns the same number to records with identical names.

My problem is that the number of records are around 1.5 million. The script always runs for about 14 hours every time and gives this error : mysql_query unable to save result set in xx on line xxx. and phpmyadmin gives this error #2008 out of memeory

mysql_query("SET SQL_BIG_TABLES=1"); $res = mysql_query("SELECT company_name, country, id FROM proj") or die (mysql_error()); while ($row = mysql_fetch_array($res, MYSQL_NUM)) < $res1 = mysql_query("SELECT company_name, id FROM proj WHERE country='$row[1]'"+ "AND id<>'$row[2]'") or die ("here".mysql_error().mysql_errno()); while ($row1 = mysql_fetch_array($res1, MYSQL_NUM)) < //My calculations here >> 

Best Solution

Ok. Your query is incredibly inefficient. You say in a comment that there’s 1.5 million rows.

  1. Outside query creates a result set with all 1.5 million rows
  2. Inside query creates a new result set with all 1.5 million rows, EXCEPT the row that has the same of the row you’re looping on. So basically you’re doing 1.5 million * (1.5 million — 1) rows = 2,249,998,500,000 = 2.25 trillion rows

In other words, not only incredibly inefficient — it’s absolutely ludicrous. Given you seem to want to fetch only rows by country, why not do something like:

$sql1 = "SELECT DISTINCT country FROM proj"; $res1 = mysql_query($sql1) or die(mysql_error()); while($row1 = mysql_fetch_associ($res1)) < $country = $row1['country']; $escaped_country = mysql_real_escape_string($country); $sql2 = "SELECT company_name, id FROM proj WHERE country='$country'"; $res2 = mysql_query($sql2) or die(mysql_error()); while ($row2 = mysql_fetch_assoc($res2)) < . calculations . >> 

This’d fetch only 1.5 million + # of country records from the database, which is far far far less than the 2.3 trillion your version has.

Читайте также:  Дипломная работа html css
Php – How to prevent SQL injection in PHP

The correct way to avoid SQL injection attacks, no matter which database you use, is to separate the data from SQL, so that data stays data and will never be interpreted as commands by the SQL parser. It is possible to create SQL statement with correctly formatted data parts, but if you don’t fully understand the details, you should always use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious SQL.

You basically have two options to achieve this:

    Using PDO (for any supported database driver):

 $stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name'); $stmt->execute([ 'name' => $name ]); foreach ($stmt as $row) < // Do something with $row >
 $stmt = $dbConnection->prepare('SELECT * FROM employees WHERE name = ?'); $stmt->bind_param('s', $name); // 's' specifies the variable type => 'string' $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) < // Do something with $row >

If you’re connecting to a database other than MySQL, there is a driver-specific second option that you can refer to (for example, pg_prepare() and pg_execute() for PostgreSQL). PDO is the universal option.

Correctly setting up the connection

Note that when using PDO to access a MySQL database real prepared statements are not used by default. To fix this you have to disable the emulation of prepared statements. An example of creating a connection using PDO is:

$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'password'); $dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

In the above example the error mode isn’t strictly necessary, but it is advised to add it. This way the script will not stop with a Fatal Error when something goes wrong. And it gives the developer the chance to catch any error(s) which are throw n as PDOException s.

What is mandatory, however, is the first setAttribute() line, which tells PDO to disable emulated prepared statements and use real prepared statements. This makes sure the statement and the values aren’t parsed by PHP before sending it to the MySQL server (giving a possible attacker no chance to inject malicious SQL).

Although you can set the charset in the options of the constructor, it’s important to note that ‘older’ versions of PHP (before 5.3.6) silently ignored the charset parameter in the DSN.

Explanation

The SQL statement you pass to prepare is parsed and compiled by the database server. By specifying parameters (either a ? or a named parameter like :name in the example above) you tell the database engine where you want to filter on. Then when you call execute , the prepared statement is combined with the parameter values you specify.

The important thing here is that the parameter values are combined with the compiled statement, not an SQL string. SQL injection works by tricking the script into including malicious strings when it creates SQL to send to the database. So by sending the actual SQL separately from the parameters, you limit the risk of ending up with something you didn’t intend.

Any parameters you send when using a prepared statement will just be treated as strings (although the database engine may do some optimization so parameters may end up as numbers too, of course). In the example above, if the $name variable contains ‘Sarah’; DELETE FROM employees the result would simply be a search for the string «‘Sarah’; DELETE FROM employees» , and you will not end up with an empty table.

Another benefit of using prepared statements is that if you execute the same statement many times in the same session it will only be parsed and compiled once, giving you some speed gains.

Oh, and since you asked about how to do it for an insert, here’s an example (using PDO):

$preparedStatement = $db->prepare('INSERT INTO table (column) VALUES (:column)'); $preparedStatement->execute([ 'column' => $unsafeValue ]); 

Can prepared statements be used for dynamic queries?

While you can still use prepared statements for the query parameters, the structure of the dynamic query itself cannot be parametrized and certain query features cannot be parametrized.

For these specific scenarios, the best thing to do is use a whitelist filter that restricts the possible values.

// Value whitelist // $dir can only be 'DESC', otherwise it will be 'ASC' if (empty($dir) || $dir !== 'DESC')
Php – Deleting an element from an array in PHP

There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Deleting a single array element

If you want to delete just one array element you can use unset() or alternatively \array_splice() .

If you know the value and don’t know the key to delete the element you can use \array_search() to get the key. This only works if the element does not occur more than once, since \array_search returns the first hit only.

unset()

Note that when you use unset() the array keys won’t change. If you want to reindex the keys you can use \array_values() after unset() , which will convert all keys to numerically enumerated keys starting from 0.

$array = [0 => "a", 1 => "b", 2 => "c"]; unset($array[1]); // ↑ Key which you want to delete 

\array_splice() method

If you use \array_splice() the keys will automatically be reindexed, but the associative keys won’t change — as opposed to \array_values() , which will convert all keys to numerical keys.

\array_splice() needs the offset, not the key, as the second parameter.

$array = [0 => "a", 1 => "b", 2 => "c"]; \array_splice($array, 1, 1); // ↑ Offset which you want to delete 

array_splice() , same as unset() , take the array by reference. You don’t assign the return values of those functions back to the array.

Deleting multiple array elements

If you want to delete multiple array elements and don’t want to call unset() or \array_splice() multiple times you can use the functions \array_diff() or \array_diff_key() depending on whether you know the values or the keys of the elements which you want to delete.

\array_diff() method

If you know the values of the array elements which you want to delete, then you can use \array_diff() . As before with unset() it won’t change the keys of the array.

$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"]; $array = \array_diff($array, ["a", "c"]); // └────────┘ // Array values which you want to delete 

\array_diff_key() method

If you know the keys of the elements which you want to delete, then you want to use \array_diff_key() . You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.

$array = [0 => "a", 1 => "b", 2 => "c"]; $array = \array_diff_key($array, [0 => "xy", "2" => "xy"]); // ↑ ↑ // Array keys which you want to delete 

If you want to use unset() or \array_splice() to delete multiple elements with the same value you can use \array_keys() to get all the keys for a specific value and then delete all elements.

Related Question

Источник

Оцените статью