I using ADODB to create a connection to my database. I update the data in my database, there is no error. The problem is that I can't get the number of affected rows by Affected_Rows(). I tried with very simple code but it is not working. Here is my code:
$sql = "UPDATE User SET Name=N'MyName' WHERE Id=1";
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
$cs = "provider=sqloledb;"."server=localhost;database=Test;uid=Admin;pwd=123456;Max Pool Size=100";
$conn->open($cs);
//there is no error in connecting process. I can add, update, delete normally.
if($conn->Execute($sql) === false)
{
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->ErrorMsg(), E_USER_ERROR);
}
else
{
echo $conn->Affected_Rows(); //<-- Error in here
}
I have read about this function in here. My code above is almost same with example here.
Is there any other way to get the number of affected rows in ADODB-PHP?
About Affected_Rows(), I don't know why it isn't working.
There is another very simple way to get the number of affected rows after execute query.
$conn->Execute($sql,$affected_rows);
echo $affected_rows;
$affected_rows return from Execute function will have value equal to number of affected rows of that query.
just for mark.
your code:
echo $conn->Affected_Rows()
is ADODB SDK methods: http://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:affected_rows
NOT a native COM("ADODB.connection") method.
but i have same problem because this method return "variant Object" so i can't get the value.
i fixed this by edit SDK source file : adodb5/drivers/adodb-ado5.inc.php #44 line:
$this->_affectedRows =new VARIANT;
to:
$this->_affectedRows = null;
then Affected_Rows() method can return a real number for me.
Related
I using ADODB to create a connection to my database. I update the data in my database, there is no error. The problem is that I can't get the number of affected rows by Affected_Rows(). I tried with very simple code but it is not working. Here is my code:
$sql = "UPDATE User SET Name=N'MyName' WHERE Id=1";
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");
$cs = "provider=sqloledb;"."server=localhost;database=Test;uid=Admin;pwd=123456;Max Pool Size=100";
$conn->open($cs);
//there is no error in connecting process. I can add, update, delete normally.
if($conn->Execute($sql) === false)
{
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->ErrorMsg(), E_USER_ERROR);
}
else
{
echo $conn->Affected_Rows(); //<-- Error in here
}
I have read about this function in here. My code above is almost same with example here.
Is there any other way to get the number of affected rows in ADODB-PHP?
About Affected_Rows(), I don't know why it isn't working.
There is another very simple way to get the number of affected rows after execute query.
$conn->Execute($sql,$affected_rows);
echo $affected_rows;
$affected_rows return from Execute function will have value equal to number of affected rows of that query.
just for mark.
your code:
echo $conn->Affected_Rows()
is ADODB SDK methods: http://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:affected_rows
NOT a native COM("ADODB.connection") method.
but i have same problem because this method return "variant Object" so i can't get the value.
i fixed this by edit SDK source file : adodb5/drivers/adodb-ado5.inc.php #44 line:
$this->_affectedRows =new VARIANT;
to:
$this->_affectedRows = null;
then Affected_Rows() method can return a real number for me.
I'm trying to implement pagination using PHP. I found that calling exec to the connected database prevents the further query calls from working.
The piece of code at hand:
<?php
// Pagination logic
//Here we count the number of results
$query = "SELECT COUNT(*) as num FROM gig";
$total_pages = $db->exec($query);
$total_pages = $total_pages[num];
?>
After it if I try to use a query such as:
<?php>
foreach ($db->query("SELECT sname, start, venue FROM gig WHERE start = '0000-00-00 00:00:00'") as $a) {
$row="<tr><td>$a[sname]</td><td>To be announced</td><td>$a[venue]</td></tr>\n";
print $row;
}
?>
it returns
Warning: Invalid argument supplied for foreach()
As soon as the first code block is removed, the query works fine. When I check the value of $total_pages, it's 0, so something must be going wrong along the way. As far as I know, I use it in the same way as the query(which works on its own), so is there any reason why it doesn't work?
The PDO is initialized in the following way:
try {
$db = new PDO("mysql:dbname=$db_name;host=$db_server", $db_user, $db_pw);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
session_start();
From Manual
PDO::exec() does not return results from a SELECT statement. For a
SELECT statement that you only need to issue once during your program,
consider issuing PDO::query(). For a statement that you need to issue
multiple times, prepare a PDOStatement object with PDO::prepare() and
issue the statement with PDOStatement::execute().
Used a function of the STATEMENT object had after using querying to count the rows instead of exec:
$dbq = $db->query("SELECT * FROM gig");
$rows = $dbq->rowCount();
About the latter code block not working because of the exec failing - it seems to just be the way php queries work, if one fails, all fail. The foreach() error is for the object it's provided is not an array, for it failed.
I'm using a basic SQL query with ADO and PHP but the row count always returns -1. What am I missing?
Here is my code:
include 'constants.php';
// Create an instance of the ADO connection object
$conn = new COM ("ADODB.Connection")
or die("Cannot start ADO");
// Open the connection to the database
$conn->open(DB_CONN_STR);
$query = "select * from table_name";
$rs = $conn->execute($query);
if (!$rs) {
return;
}
else {
return $rs->RecordCount();
}
I know this table has values...what am I missing?
The manual says why.
http://phplens.com/lens/adodb/docs-adodb.htm#recordcount
seems you have set
$ADODB_COUNTRECS = false
I had a similar problem using Adodb with PHP, with a PDO mssql driver.
I executed a SQL query with GetOne or LimitQuery in a try / catch block
the query resulted in an error
i caught the exception and went on to execute a valid query with Execute
the resulting calls to RecordCount would wrongly return -1 (but i could iterate on the rows).
I found out that some Adodb functions set that $ADODB_COUNTRECS flag to false before executing their query. In case of an error the flag was not set back to true, and that is why RecordCount returned -1.
I set the global variable to true in my exception constructor and that solved the problem.
i have written a basic stored procedure using mysql
DELIMITER //
CREATE PROCEDURE `sp_sel_test`()
BEGIN
SELECT * FROM category c;
END//
DELIMITER ;
now i m calling it from php
the php code is:
<?php
$txt = $_GET['id'];
$name = $_GET['name'];
$con = mysql_connect("localhost","four","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("fourthes_a", $con);
//$result = mysql_query("select * from new_c where name like %". $name ."% or c_name like %" . $name . "% order by name asc;");
$result = mysql_query("call sp_sel_test()");
if ($result === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
echo $row['category_id'] . " " . $row['c_name'];
?>
<br />
<?php
}
mysql_close($con);
echo $txt;
?>
now its giving the error
PROCEDURE fourthes_a.sp_sel_test can't return a result set in the given context
mysql_query() returns false when the query fails. You didn't check if your sproc query succeeded, so most likely you're passing that boolean FALSE to the fetch function, which is rightfully complaining.
Rewrite your code like this, as a bare mininum, for proper error handling:
$res = mysql_query('call sp_sel_test()');
if ($res === FALSE) {
die(mysql_error());
}
Never ever assume a query succeeded. Even if the SQL syntax is perfect, there's far too many other reasons for a query to fail to NOT check if it worked.
You need to set client flags while connecting for using stored procedures with php. Use this:
mysql_connect($this->h,$this->u,$this->p,false,65536);
See MySQL Client Flags for more details. PHP MySQL does not allow you to run multiple statements in single query. To overcome this you must tell PHP to allow such queries by setting CLIENT_MULTI_STATEMENTS flag in your connection.
I know last answer was a year ago, but...
CREATE PROCEDURE sp_sel_test(OUT yourscalarvariable INT/TEXT...)
Statements that return a result set cannot be used within a stored function. This includes SELECT statements that do not use INTO to fetch column values into variables, SHOW statements, and other statements such as EXPLAIN. For statements that can be determined at function definition time to return a result set, a Not allowed to return a result set from a function error occurs (ER_SP_NO_RETSET_IN_FUNC). For statements that can be determined only at runtime to return a result set, a PROCEDURE %s can't return a result set in the given context error occurs (ER_SP_BADSELECT).
So, your select shoould be like this:
SELECT * FROM category **INTO** c;
http://www.cs.duke.edu/csl/docs/mysql-refman/stored-procedures.html
I've been trying to make this work for the last couple of hours. I have a simple stored proc called get_log_entries:
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_log_entries`(p_min_id INT)
BEGIN
SET p_min_id = IFNULL(p_min_id, -1);
SELECT * FROM db_log WHERE item_id > p_min_id;
END
It's dead simple and it returns results in two columns: item_id (int) and log_description (varchar).
I am trying to execute this using MDB2 object, but so far no luck. Here's the code that is trying to do it:
$conn = MDB2::factory('mysql://myUser:myPassword#localhost/my_db');
if (PEAR::isError($conn)) {
die ("Cannot connet to DB(10): " . $conn->getMessage());
}
// loading the Function module
$conn->loadModule('Function');
$params = array('null');
$result = $conn->executeStoredProc('get_log_entries', $params);
if (PEAR::isError($result)) {
$msg = $result->getMessage() . "<br /><br />" . $result->getUserInfo();
die ($msg);
}
At this point this nice error shows up with the message:
"_doQuery: [Error message: Could not
execute statement] [Last executed
query: CALL get_log_entries()] [Native
code: 1312] [Native message: PROCEDURE
wh_search.get_log_entries can't return
a result set in the given context]"
Now I have a couple of questions:
1. Is it even possible to execute SPs using MDB2 and return result sets?
2. Or is it better to write a wrapper class for "native" PHP-MySQL functions myself?
Thanks!
Nevermind, wrapping mysqli for calling SP. Also lots of fun...