Changeset:
        c5d0af9e9124
        
https://sourceforge.net/p/mrbs/hg-code/ci/c5d0af9e9124b71b92d9129089bb913f37109a00
Author:
        Campbell Morrison <[email protected]>
Date:
        Thu Sep 29 10:14:41 2016 +0100
Log message:

Made sql_query1() throw exceptions if it results in more than one row or column

diffstat:

 web/day.php         |   5 +++++
 web/lib/MRBS/DB.php |  17 ++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)

diffs (49 lines):

diff -r 883369262044 -r c5d0af9e9124 web/day.php
--- a/web/day.php       Thu Sep 29 09:49:01 2016 +0100
+++ b/web/day.php       Thu Sep 29 10:14:41 2016 +0100
@@ -7,6 +7,11 @@
 require_once "mincals.inc";
 require_once "functions_table.inc";
 
+$sql = "SELECT id FROM $tbl_entry WHERE id=?";
+$result = sql_query1($sql, array(571407));
+var_dump($result);
+exit;
+
 // Get non-standard form variables
 $timetohighlight = get_form_var('timetohighlight', 'int');
 $ajax = get_form_var('ajax', 'int');
diff -r 883369262044 -r c5d0af9e9124 web/lib/MRBS/DB.php
--- a/web/lib/MRBS/DB.php       Thu Sep 29 09:49:01 2016 +0100
+++ b/web/lib/MRBS/DB.php       Thu Sep 29 10:14:41 2016 +0100
@@ -121,9 +121,7 @@
   // Execute an SQL query which should return a single non-negative number 
value.
   // This is a lightweight alternative to query(), good for use with count(*)
   // and similar queries.
-  // It returns -1 if the query did not return exactly one value, so error 
checking
-  // is somewhat limited.
-  // It also returns -1 if the query returns a single NULL value, such as from
+  // It returns -1 if the query returns no result, or a single NULL value, 
such as from
   // a MIN or MAX aggregate function applied over no rows.
   // Throws a DBException on error.
   function query1($sql, $params = array())
@@ -138,8 +136,17 @@
       throw new DBException($e->getMessage(), 0, $e, $sql, $params);
     }
     
-    if (($sth->rowCount() != 1) || ($sth->columnCount() != 1) ||
-        (($row = $sth->fetch(PDO::FETCH_NUM)) == NULL))
+    if ($sth->rowCount() > 1)
+    {
+      throw new DBException("sql_query1() returned more than one row.", 0, 
null, $sql, $params);
+    }
+    
+    if ($sth->columnCount() > 1)
+    {
+      throw new DBException("sql_query1() returned more than one column.", 0, 
null, $sql, $params);
+    }
+    
+    if (($row = $sth->fetch(PDO::FETCH_NUM)) == NULL)
     {
       $result = -1;
     }

------------------------------------------------------------------------------
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to