How to use PHPUnit administrative connection to query DB? - php

Using the PHPUnit administrative connection to the database would avoid "polluting" any logging or other things going on inside of our app code with an SQL command that's only being used to implement the test.
I'd like to use $this->getConnection() to grab the administrative PHPUnit connection to the database rather than call our SystemDB::query() function directly, but I can't seem to get the syntax correct. Any thoughts would be appreciated.

This worked:
$result = $this->getConnection()->getConnection();
$query = $result->query( $sql );
$my_array = $query->fetchAll();
where $sql is the query and getConnection() is implemented according to the main phpUnit manual page http://phpunit.de/manual/current/en/database.html

Related

How do I connect to the database with out using the mysql_connect when creating the new module in PrestaShop

I am using the below code to connect to the database in the module to write the several database queries which are not correct it seems, so please tell me what is the correct way of connecting to the database from the new module.
$con = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
mysql_select_db(_DB_NAME_);
use
$query ="select * from name_table"; /*example query*/
Db::getInstance()->executeS($query);
you do not need anything else to make a query
Please, before read this guidelines to create a new module: How to create a module
And then this: Best practices of the Db Class
Including the files like the elPresta says it's old and deprecated method, after that to make a simple query, read what he wrote Matteo Enna.
Just include these files and thats it!
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
And after all you can use methods as Matteo told u. Or read more here ( http://doc.prestashop.com/display/PS15/DB+class+best+practices )

Strange mysql_ multiple connections issue

I'm kinda going crazy about this problem. I can't do it myself so I need the community to help me get this thing solved. I've been spending hours on this because I didn't know where to look. I now know a possible fix but it's just messy (read on). I just need someone who knows more about this than I do.
This is my situation:
I want to use 2 or more mysql connections.
I use OOP
I have a class called dbase, it has two functions, setConnection and getConnection and two class variables called $connection and $dbaseName.
In my main project file I include dbase and create two objects:
dbase
maindbase
then I do:
$this->dbase->setConnection($server, $uname, $pword);
$this->maindbase->setConnection($server, $uname, $pword);
the setConnection function looks like this:
function setConnection ($server, $serv_Username, $serv_Password) {
$this->connection = mysql_connect($server, $serv_Username, $serv_Password, true);
// echo $this->connection . "<BR>";
}
I echo it to see the resourcenumber and added true to mysql_connect (and I know it's deprecated since 5.5, I'm not here for that).
Now, as I understand OOP, the class variables are set per object. So $connection from dbase will never be the same as maindbase (unless, of course, I use the same credentials, but even then it will create a new link because of the $new_link option I enabled). They both have different resource ID's.
My problem:
In class dbase I also have a function which runs a query like this:
$connection = $this->getConnection();
$dbase_name = $this->getDbaseName();
mysql_select_db($dbase_name, $connection);
$q = "SELECT * FROM {$table_name} WHERE {$column} LIKE '{$value}'";
$result = mysql_query($q);
Now, when I use it like this, it will ALWAYS use the FIRST $connection that has been set in class dbase and it doesn't matter which object this is, either object dbase or maindbase.
I get this error:
Table 'testdbase1.skye_domains' doesn't exist
object dbase is connected to testdbase1
object maindbase is connected to testdbase2
the above error I get when trying to select results using the maindbase object.
When I remove the $connection string from mysql_select_db it works perfectly because it will try to open a new connection as if using mysql_connect.
Why is this? This is impossible right? How can objectmaindbase have the same $connection as object dbase? They are in NO WAY connected to eachother... Is PHP somehow using a global mysql_connect variable or buffer which I'm not aware about?
I would like to keep using connectionstrings as this is just handy now and then. Leaving the $connection string out seems messy.
Does anybody have any suggestions I can try to make PHP (or my head) sane again?
Try to put echo $this->connection EVERYWHERE you use it. Also, create an "id" member and fill it with a unique value upon constructing a dbase-object and echo it along the value of $this->connection. This way you can track where what happens to your connection.
And check if there's maybe some place outside of the class that assigns $foo->connection. If you're not using "private" on the members, you're bound to have such problems when you e.g. forget to remove a hack or an experiment from unrelated parts of your code.

My project runs fine on local machine, but not on web server

My project is working fine on my local machine but not on the web server. I think it is the stored procedures, because the error that I am getting is:
Fatal error: Call to a member function fetch_array() on a non-object in ...
The collation of the database is "utf8_general_ci".
Just a simple example:
I have a stored procedure called offices:
CREATE PROCEDURE offices()
BEGIN
SELECT * FROM offices;
END//
And the php code:
<?php
require ("db.php");
$db = dbConnect();
$result = $db->query("CALL offices()");
while(list($id, $city, $address) = $result->fetch_array())
echo "($id) $city: $address ";
?>
What happens on the database server, when you CALL offices() manually? Any errors? If I had to guess, it looks like the offices() function is not defined on the server, or fails when invoked (table offices doesn't exist?).
To me it looks like the $result is not a object or not initialized. Can you show us the code in your db library. Specifically the query function...
Make sure your db user which was used to install the schema has privileges to create stored procedures. If you are unsure if they are on the server, you can do
SELECT * FROM `information_schema`.`ROUTINES`;
..to see which procedures have been succesfully created.
Keep in mind the majority of shared hosting services do not support triggers, procedures etc.
you are probably running a different version of your database at the remote server.
so the query method is failing and is not returning what should return.
Looks like your auth does not have allowed to use DB procedures at the server

To understand a line of PHP code about a connection

What is connection() in the following code?
Code which I do understand completely
if($user->connection($email,$password)){
}
Let's assume connection() is pseudo-code.
Is the pg_prepare and pg_execute` enough to create the connection?
The line caused me to omit the use of its main code in generating a login system. Subsequently, SOers pointed out severe sequrity holes in my code.
I could not find the explanation for the function connection().
The code seems to be pseudo-code to me, since the connection does not have any attributes to the database, for instance by
Example of how the connection should be formed
$result = pg_prepare($dbconn, "query22", "SELECT passhash_md5 FROM users
WHERE email=$1;");
$passhash_md5 = pg_execute($dbconn, "query22", array($_REQUEST['email']));
$user is an instance of a class.
connection is a method in that class.
$mail & $password are parameters to that method.
This has nothing todo with arrays. what you mean would be:
$foo = array("key" => "value");
maybe this can help you:
http://www.webstockbox.com/php/7-tutorials-on-how-to-create-a-php-login-system/
I haven't actually used php.net, but this just looks like connection is a method of object $user that takes 2 params. Nothing to do with arrays.

Call Multiple Stored Procedures with the Zend Framework

I'm using Zend Framework 1.7.2, MySQL and the MySQLi PDO adapter. I would like to call multiple stored procedures during a given action. I've found that on Windows there is a problem calling multiple stored procedures. If you try it you get the following error message:
SQLSTATE[HY000]: General error: 2014
Cannot execute queries while other
unbuffered queries are active.
Consider using
PDOStatement::fetchAll().
Alternatively, if your code is only
ever going to run against mysql, you
may enable query buffering by setting
the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
attribute.
I found that to work around this issue I could just close the connection to the database after each call to a stored procedure:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
//If on windows close the connection
$db->closeConnection();
}
This has worked well for me, however, now I want to call multiple stored procedures wrapped in a transaction. Of course, closing the connection isn't an option in this situation, since it causes a rollback of the open transaction. Any ideas, how to fix this problem and/or work around the issue.
More info about the work around
Bug report about the problem
I has same errors when called queries like this(variables to use in next query)
$db->query("SET #curr = 0.0;");
To fix this I've changed my config file to
'database' => array(
'adapter' => 'mysqli',
This pattern of preparing, executing and then closing each $sql statement that calls a stored procedure does work.
public function processTeams($leagueid,$raceid,$gender)
{
$db = Zend_Db_Table::getDefaultAdapter();
$sql = $db->prepare(sprintf("CALL addScoringTeams(%d,%d,'%s')",$leagueid,$raceid,$gender));
$sql->execute();
$sql->closeCursor();
$this->logger->info(sprintf("CALL addScoringTeams(%d,%d,'%s')",$leagueid,$raceid,$gender));
$sql1 = $db->prepare(sprintf("CALL updateScoringTeamTotals(%d)",$raceid));
$sql1->execute();
$sql1->closeCursor();
$this->logger->info(sprintf("CALL updateScoringTeamTotals(%d)",$raceid));
$sql2 = $db->prepare(sprintf("CALL updateScoringTeamClasses(%d,'%s')",$raceid,$gender));
$sql2->execute();
$sql2->closeCursor();
$this->logger->info(sprintf("CALL updateScoringTeamClasses(%d,'%s')",$raceid,$gender));
}
You can use prepare statement . No need to change driver
$sql = "CALL procedure()";
$stmt = $this->db->createStatement();
$stmt->prepare($sql);
$result = $stmt->execute();

Categories