What in this program - mysql

I have been complete my class that will connecting to mysql server.
When I running it I got an error:
Warning: mysql_connect()
[function.mysql-connect]:
php_network_getaddresses: getaddrinfo
failed: This is usually a temporary
error during hostname resolution and
means that the local server did not
receive a response from an
authoritative server. in
C:\wamp\www.php on line 18
Warning: mysql_connect()
[function.mysql-connect]: [2002]
php_network_getaddresses: getaddrinfo
failed: This is usually a (trying to
connect via tcp://test:3306) in
C:\wamp\www.php on line 18
Warning: mysql_connect()
[function.mysql-connect]:
php_network_getaddresses: getaddrinfo
failed: This is usually a temporary
error during hostname resolution and
means that the local server did not
receive a response from an
authoritative server. in
C:\wamp\www.php on line 18 Error
cannnot connect to mysql server
I check my sql server that it had high privileges and dont have a password.
my code:
<?php
class mysql
{
var $user;
var $password;
var $database;
var $host;
function mysql($username, $password, $database, $host)
{
$this->user = $username;
$this->password = $password;
$this->database = $database;
$this->host = $host;
}
function connect()
{
$conn = mysql_connect($this->host, $this->user, $this->password, $this->database)
or die("Error cannnot connect to mysql server");
echo "Connected successfully to Mysql server";
mysql_close($conn);
}
}
$connect = new mysql('127.0.0.1','root','','test');
$connect->connect();
?>

try:
$connect = new mysql('root','','test','127.0.0.1');

I suggest you to defined each property:
var $user;
var $password;
var $database;
var $host;
like these:
private $user;
private $password;
private $database;
private $host;
And create setter and getter methods for each property, an example:
public setUser($user){
$this->user = $user;
}
public getUser(){
return $this->user;
}
Now, I'm sure you are asking yourself: Why? Here there is the answer :-)
bye

Related

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\account\classes\class.sql.php on line 34

I am new to mysqli_* and I am getting these errors:
Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\account\classes\class.sql.php on line 34
I got the error when i change from php5 to php7
Codes:
private static function select_db() {
if ( Sql::$base != NULL )
Sql::$db = mysqli_select_db(Sql::$base, Sql::$connect);
}
and this is the declaration
code:
Class Sql {
private static $host; // Host tobeconnected
private static $user; // User name
private static $password; // Password
private static $base; // Database
private static $connect; // Connection resource
private static $db; // Database resource
private function Sql() {}
// Connects to the host
public static function connect($Host, $User, $Password, $Base) {
Sql::$host = $Host;
Sql::$user = $User;
Sql::$password = $Password;
Sql::$base = $Base;
Sql::$connect = mysqli_connect(Sql::$host, Sql::$user, Sql::$password);
Sql::select_db();
return Sql::$connect;
}

How can I check if database exists and user of that database has permission to connect that database - Laravel

I'm getting this kind of error SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' when I'm trying to check this
if(DB::connection())
{
//do something
}
Now what I want to do is I want to check if the database on which application is trying to connect exists. If yes then it should check if the user defined in the application has privileges to connect to that database. And if yes then I want to migrate all the tables.
Please help me with this. Thank you
You can try catch the exception:
public function test($connectionName)// connection name
{
$state = true;
try {
\DB::connection($connectionName);
} catch( \PDOException $e) {
$state = false;
}
return $state;
}
public function index()
{
dd($this->test('mysql'));
}

AS3 socket cannot load data from

I have searched internet for a lot of times but could not find any clue. I am using sockets to connect to socket server. (Also we tryed different socket policy files, but none worked)
private var SERVER_URL : String = "http://192.168.0.105";
private var PORT_NUMBER : int = 1435;
private var _socket : Socket;
// signal
private var _signal : Signal;
public function BackendService()
{
// nastavenie socketu
_socket = new Socket();
_socket.addEventListener(Event.CONNECT, connectHandler);
_socket.addEventListener(Event.CLOSE, closeHandler);
_socket.addEventListener(ErrorEvent.ERROR, errorHandler);
_socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
_socket.addEventListener(ProgressEvent.SOCKET_DATA, HandleMsgFromServer); // ak pride sprava zo serveru
Security.allowDomain("*");
Security.allowInsecureDomain("*");
Security.loadPolicyFile("http://192.168.0.105:8000/crossdomain.xml");
// signal na dispatchovanie eventov
_signal = new Signal();
connect(); // I am doing it from other class, but for sake of example, I am calling from here
}
public function connect():void
{
_socket.connect(SERVER_URL, PORT_NUMBER);
}
My crossdomain - socket policy file are sitting on port 8000 and looks like this:
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*" to-ports="*"/>
</cross-domain-policy>
I am getting this error:
Error #2044: Unhandled securityError:. text=Error #2048: Security
sandbox violation:
file:///C:/projects/TennisHeroes/bin/TennisHeroes.swf cannot load data
from http://192.168.0.105:1435.
Thank you very much for any advice!

PHPUnit/DbUnit "Too many connections"

While running PHPUnit tests, a lot of connections are created but not closed.
I can see this in
mysql> show processlist;
In my database class I create a db connection by implementing PHPUnit_Extensions_Database_TestCase#getConnection().
I make sure that in the teardown the connection gets closed. See snippet:
<?php
abstract class My_Tests_DatabaseTestCase extends \PHPUnit_Extensions_Database_TestCase
{
static private $pdo = null;
private $conn = null;
/**
* #throws RuntimeException
* #return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
final public function getConnection()
{
$iniFilePath = __DIR__ . '/../../../db-config.ini';
$iniFile = parse_ini_file($iniFilePath, true);
$dsn = "mysql:dbname=".$iniFile['phpunit']['dbname'].";host=".$iniFile['phpunit']['host'];
if ( $this->conn === null ) {
if ( self::$pdo == null ) {
self::$pdo = new \PDO($dsn, $iniFile['phpunit']['user'], $iniFile['phpunit']['password']);
}
$this->conn = $this->createDefaultDBConnection(self::$pdo, $iniFile['phpunit']['dbname']);
}
return $this->conn;
}
protected function getSetUpOperation()
{
return new \PHPUnit_Extensions_Database_Operation_Composite(array(
new \TestsExtensions\TruncateDatabaseOperation(),
\PHPUnit_Extensions_Database_Operation_Factory::INSERT()
));
}
protected function getTearDownOperation() {
return \PHPUnit_Extensions_Database_Operation_Factory::TRUNCATE();
}
protected function setUp()
{
parent::setUp();
$em = \ORM\Provider::getInstance()->getEntityManager(\ORM\Provider::DEFAULT_ID);
$em->clear();
}
protected function tearDown()
{
parent::tearDown();
$em = \ORM\Provider::getInstance()->getEntityManager(\ORM\Provider::DEFAULT_ID);
$em->getConnection()->close();
if ($this->conn) {
$this->conn->close();
}
}
}
Debugging showed that the connections were closed, but the processlist showed them with status "sleep".
The amount of connections rises until I get the "too many connections" error.
I do not want to increase the number of connections. I want to close the connection.
What can I modify to make this happen?
Snippet from PDO Connection management:
Upon successful connection to the database, an instance of the PDO
class is returned to your script. The connection remains active for
the lifetime of that PDO object. To close the connection, you need to
destroy the object by ensuring that all remaining references to it are
deleted--you do this by assigning NULL to the variable that holds the
object. If you don't do this explicitly, PHP will automatically close
the connection when your script ends.
Don't store the PDO or set it to null in the teardown:
Remove static private $pdo = null;
or
protected function tearDown()
{
parent::tearDown();
$em = \ORM\Provider::getInstance()->getEntityManager(\ORM\Provider::DEFAULT_ID);
$em->getConnection()->close();
if ($this->conn) {
$this->conn->close();
}
self::$pdo = null;
}
I don't think you need to store the PDO if you are going to pass it into the PHPUnit DB connection

Need some help debugging this PHP

I coded this up a bit ago, but it's not working.
I realize I could do this simpler, but I'm trying to use OOP.
So here is the code..
database.class.php
<?php
class config {
public $host;
public $user;
public $pass;
function __construct($host=NULL,$user=NULL,$pass=NULL) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
}
function __destruct() {}
}
class database {
private $host;
private $user;
private $pass;
private $config;
function __construct($config) {
$this->config = $config;
}
function __destruct() {}
public function openCon() {
mysql_connect($this->host,$this->user,$this->pass);
if (mysql_errno()) {
printf('Failed to connect: %s', mysql_error());
} else {
echo 'Connected to DB successfully.<br/><br/>';
}
}
public function closeCon() {
try {
mysql_close();
echo 'Successfully closed connection.<br/><br/>';
} catch (exception $e) {
echo $e;
}
}
}
?>
index.php
<?php
include('db.class.php');
$con = new config('localhost','root','');
$etc = new database($con);
$etc->openCon();
mysql_select_db('tester') or die(mysql_error());
$query1 = mysql_query('SELECT * FROM person') or die(mysql_error());
$rows = mysql_fetch_array($query1) or die(mysql_error());
echo 'First: ' . $rows['First'];
echo 'Age:' . $rows['Age'];
$etc->closeCon();
?>
I'm sure there are blaring errors in this. Could anyone help me find and fix them?
It says: Access denied for user ''#'localhost' to database 'tester'
Not sure why.
I specified root as the user, but it came back with '' as the user I requested to use.
It got the host and the database right.
There is no password to my local root user, so...
Any ideas?
Okay, the problem is that the host, user and password are never set. Look in the database constructor. You only set the configuration.
Either set the private database properties somehow, or use the configuration object in openCon().
Or, even better, just scrap the config object. Right now it plays no significant role. Just change the database constructor like the following and be done with it:
function __construct($host, $user, $pass) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
}
Finally, if the __destruct() method does not do anything then you do not need to include it.
Your every thing is correct just you need to change in your database class. You pass $config as an object to the constructor of the database class so to access the variable from that object you need little change. It should be
class database {
private $host;
private $user;
private $pass;
private $config;
public $Connectionlink;
function __construct($config) {
$this->host = $config->host;
$this->user = $config->user;
$this->pass = $config->pass;
}
// function __destruct() {} as your destruct function doing noting so you need not to include this.
public function openCon() {
$this->Connectionlink= mysql_connect($this->host,$this->user,$this->pass);
if (mysql_errno()) {
printf('Failed to connect: %s', mysql_error());
} else {
echo 'Connected to DB successfully.<br/><br/>';
}
}
public function closeCon() {
try {
mysql_close();
echo 'Successfully closed connection.<br/><br/>';
} catch (exception $e) {
echo $e;
}
}
}
mysql_select_db need two parameters, so it should be
mysql_select_db('tester', $etc->Connectionlink) or die(mysql_error());
just try to navigate to your mysql directory using cmd
in xampp its "C:\xampp\mysql\bin"
and then type
"mysql -u root" and check whether you are getting access