WAMP MySQL PDO_extension error - php

I want to connect MySQL database using PDO Extension. I am using PHP 5.4
$db = new PDO('mysql:host=localhost:8080;dbname=films_db;charset=UTF-8', 'root', '');
$query = "SELECT * FROM employee";
$result = $db->query($query);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
print_r($row);
}
$result->closeCursor();
But, my WAMP server is not responding. I am not getting any result. Its "Waiting for localhost" state and getting server timeout error.
Pls help me to resolve this. Thanks.

Port 8080 is for http,
normally it does not used for mysql.
and the default mysql port is 3306.
You might need to check your mysql setting to "confirm" the actual port being used.
Beside this, you might need to check how many record in the table employee.
If millions of records, it might be the reason it takes time to load.

Related

Target machine actively refuses mysqli database connection

I'm in the process of implementing a system for database table replication. I'm aware of the latter MySQL technique but I couldn't get it working. What I have now is essentially a preliminary implementation.
This is the code I have thus far.
//This is where DBHandler is defined
$documentRoot=$_SERVER['DOCUMENT_ROOT'];
require_once($documentRoot."/Classes/DBHandler.php");
$src_controller = new DBHandler();
$query = "SELECT * FROM table";
$temp = $src_controller->select($query);
//Convert temporary database contents into usable result
while($rowObject=mysql_fetch_object($temp)){
$result[]=$rowObject;
}
//Create database handler to handle database
$dest_controller = new mysqli("ip address", "user", "password", "database name");
if($mysqli->connect_errno){
$errorMessage = sprintf("'%s'", $mysqli->connect_error);
echo $errorMessage;
}
else{
$dest_controller->query("DELETE * FROM table");
//Processing of data goes here
foreach($result as $item){
//Create query to insert individual records
$start = "INSERT INTO table(field1, field2...) VALUES (";
$end = sprintf("'%s', %d, %d, %f, %f)", field1, field2...);
$start .= $end;
//Database handler uses $start to insert record
$dest_controller->query($start);
}
$dest_controller->close();
}
The database being read from is on a different server from the one being written to. I tried checking the host for the target database with the command "SHOW VARIABLES WHERE Variable_name = 'hostname'" and wound up with a site that just gives a "sorry, wrong page or we've moved" page. What's more is that the port number I see in the URL doesn't match up with what the "SHOW VARIABLES WHERE Variable_name = 'port'" command gives me. The latter command says 3306.
I'm not really sure what to do. We use cPanel X to connect to the database and there's an option to allow select IPs remote access to this database. I'm positive I granted access to the right addresses but this accomplished nothing.
Is this some kind of issue with the host or did I not use the right IP address for connection when I created the mysqli object?
If the MySQL port open on that server? Check with this:
telnet yoursever.address.com 3306
If telnet doesn't connect, then the port is blocked and you will need your hosting provider to open it up.

Connect Oracle Database server in PHP

I'm developing a website in PHP that interacts with Oracle10g remote server database. I've googled on this topic a lot and couldn't find a solution, though i got some idea on tnsnames.ora file. I've installed WAMP in my machine. What are steps to connect to Oracle remote database?? Can any one explain it step by step??
I created a site which connects to MySQL datebase a year ago which wasn't this much tough.
$con = oci_connect('username', 'password', '//server ip:port/service name');
It's throwing "Call to undefined function oci_connect()" error.
First of all , you must install and configure OCI8.
To do this please, follow this link http://antoine.hordez.fr/2012/09/30/howto-install-oracle-oci8-on-rhel-centos-fedora/
To connect to database :
$Conexion_ID =oci_connect($OracleUser, $OraclePassw, $OracleIP);
To launch a query
$sql="SELECT ...";
$id_sentence = oci_parse($Conexion_ID, $sql);
if (!$id_sentence)
{
return false;
}
$results = oci_execute($id_sentence, OCI_DEFAULT);
To view results:
while ($row = oci_fetch_array($id_sentence, OCI_ASSOC+OCI_RETURN_LOBS))
{
....
}

Database connection problems in PHP/MySQL

When I run the script below with the added line,
$count = 1;
I get a value of 1 on the screen, but when I take that line out I don't get get anything at all. tried moving it above the $count=mysql_num_rows($result); and I still didn't get a value.
$sql="SELECT EMAIL FROM CUSTOMER WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$count = 1;
echo $count;
What am I doing wrong here? I have never used PHP until now. The error is:
Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)
This means that your MySQL server socket (/var/run/mysqld/mysqld.sock) is either missing or corrupt.
It could also mean that MySQL service is not working right, try restarting in SSH using:
$ service mysqld restart
If it says the service is missing, then say:
$ service mysql restart
I would guess at an a error with your SQL query (var_dump($count); would return false in this case).
To check this, I would do 2 things:
After your query do if(!$result) echo mysql_error( ); - this will show you any errors that happen while talking to the database
To check your SQL is being formed correctly, create it in a variable (e.g. $sql = "SELECT email ...";) and echo it out.
EDIT: Ahh just seen the update - it's a connection issue. Check that your mysql_connect( ) has the right host, username & password. Otherwise it could be a problem on your system (e.g. firewall or similar)
EDIT 2: As has been rightly pointed out, mysql_connect details would cause a different connection error to the one you're seeing. I've had a quick Google for it, and this cropped up http://lists.mysql.com/mysql/204035. Not sure it'll be any use as I've not read it through, but it does describe some steps for the solving this problem on someone else's system.
When you can't connect successfully to the database, return value from mysql_query function ($result) would not be a valid value. so when you give it to mysql_num_rows function, it fails & returns FALSE value, which has no visual effect on output screen!

How to connect to MSSQL in PHP using sqlsrv api?

I am trying to connect to MSSQL in PHP using sqlsrv api. When I run phpinfo(), it shows an a section for sqlsrv. But while actually writing code, I cannot connect to MSSQL. I am using SQL Server R2. I connect to the database from the Management Studio using the specified hostname. Here is the code:
<?php
$serverName = "TEST-PC\SQLEXPRESS";
$databaseName = "TestDB";
$connectionInfo = array("Database"=>"$databaseName");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ){
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
else{
echo "Connection created";
}
$tsql = "SELECT * FROM Test";
$result = sqlsrv_query($conn, $tsql);
echo "Rows returned: ".sqlsrv_num_rows($result);
sqlsrv_close();
?>
The code prints NOTHING on the page, not even any error message. Here is the entries in php.ini file:
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll
Any help please?
Since you did not pass a user or password, the driver is trying to connect via Windows Authentication. By running PHP from IIS, it will connect with the credentials of what whatever user IIS is running under (such as NT AUTHORITY\IUSR). You need to add the IIS user to the list of allowed logins to SQL Server, and then add a user associated with that login to the database.
Did you mean to put "$connectionOptions" where you have "$connectionOptions"?
Turn on error_reporting for php for more verbose output.
[EDIT]
A few possibile solutions:
Check your Connection String properties for the MSSQL Database you're connecting to (i.e. are you matching the values exactly, or can you use (local)
It's possible the User you're logged in on doesn't have proper access to the database. That sounds like an oversimplification, but check out your IIS settings, as well as FastCGI.impersonate settings. Turn on IIS Windows Authentication settings in IIS, and fastcgi.impersonate = 1. There's a thorough article about this potential issue here: http://blogs.msdn.com/b/brian_swan/archive/2010/02/10/sql-server-driver-for-php-understanding-windows-authentication.aspx
It is because sqlsrv_query() uses SQLSRV_CURSOR_FORWARD cursor type by default. However, in order to get a result from sqlsrv_num_rows(), you should choose one of these cursor types below:
SQLSRV_CURSOR_STATIC
SQLSRV_CURSOR_KEYSET
SQLSRV_CURSOR_CLIENT_BUFFERED
For more information, check: Cursor Types (SQLSRV Driver)
In conclusion, Try this Code:
$tsql=sqlsrv_query("SELECT * FROM Test");
$row_count = sqlsrv_num_rows($tsql);
echo $row_count;

Best approach to see if a MySQL Server is up and running

I have a Master - Slave setup for a web application written in PHP. I have a pool of slaves I use for reading, and a Master that is used for writes (and reads if a write has been sent this request). I would like to incorporate an automated system for removed crashed servers from the read pool. Currently I am using:
foreach($readers as $reader)
{
$fp = #fsockopen($reader['host'],3306,$errno,$errstr,1);
if(!$fp)
{
//Remove from pool
}
unset($fp);
}
My primary question is there a more reliable method. I have had quite a few false positives, and vice versa because it is not actually checking for a MySQL server, but rather just a connection on port 3306. Is there a way to check for a MySQL server without raising an exception, which is the behaviour of the PDO and MySQLi extensions in PHP.
You could just use mysql_connect() and check the result for false, and close the connection right away on success. You can make a dummy account with no privileges for that if you like.
That's really the only reliable way, especially if you want to distinguish a running MySQL server from any other random process listening on port 3306.
You could use mysql_ping() to check if a current DB Connection you have open is still alive
Here is the example posted at http://www.php.net/manual/en/function.mysql-ping.php
<?php
set_time_limit(0);
$conn = mysql_connect('localhost', 'mysqluser', 'mypass');
$db = mysql_select_db('mydb');
/* Assuming this query will take a long time */
$result = mysql_query($sql);
if (!$result) {
echo 'Query #1 failed, exiting.';
exit;
}
/* Make sure the connection is still alive, if not, try to reconnect */
if (!mysql_ping($conn)) {
echo 'Lost connection, exiting after query #1';
exit;
}
mysql_free_result($result);
/* So the connection is still alive, let's run another query */
$result2 = mysql_query($sql2);
?>
The best way to check if any service is alive is to actually use it. So for MySQL try to connect and execute some fast query, for web server try to fetch some file, for PHP try to fetch some simple script...
For MySQL master/slave setup, one of the solutions is to actually check the state of replication. You can check how many transactions is the slave behind master and decide to stop using that slave when/while it has old data. (I don't do the replication myself, but I think you need to compare the variables Read_Master_Log_Pos and Relay_Log_Pos)

Categories