I try to run a php script on ubunto, and everytime I run it with sudo php -f /opt/lampp/htdocs/scanner/server/start.php
i get this message.
Server: Running...
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' in /opt/lampp/htdocs/scanner/server/start.php:26 Stack trace: /opt/lampp/htdocs/scanner/server/start.php(26): PDO->__construct('mysql:host=loca...', 'root', 'datakvarnen') /opt/lampp/htdocs/scanner/server/start.php(51): openConnection() {main} thrown in /opt/lampp/htdocs/scanner/server/start.php on line 26
I have tried with php -m
I get
[PHP Modules] bcmath bz2 calendar Core ctype date dba dom ereg exif fileinfo filter ftp gettext hash iconv json libxml mbstring mhash mysql mysqli openssl pcntl pcre PDO pdo_mysql Phar posix readline Reflection session shmop SimpleXML soap sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xml xmlreader xmlwriter zip zlib
[Zend Modules]
All I get with sudo yum install php-pdo
and sudo yum install php-pdo_mysql
it says it's already installed.
EDIT: Here is the whole start.php file
<?php
$ip = "127.0.0.1";
$port = 5012;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
if(!$server = socket_create(AF_INET, SOCK_STREAM, 0)){
echo socket_strerror(socket_last_error()); exit;
}
if(!socket_bind($server, $ip, $port)){
echo socket_strerror(socket_last_error()); exit;
}
if(!socket_listen($server, 5)){
echo socket_strerror(socket_last_error()); exit;
}
echo "Server: Running...\n\n";
function openConnection($db = "scanner"){
$pdo = new PDO("mysql:host=localhost;dbname={$db};",'root','datakvarnen');
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo -> exec("SET CHARACTER SET utf8");
return $pdo;
}
function sendMessage($connection, $message){
$message .= "\r\n".chr(0);
socket_write($connection, $message, strlen($message));
usleep(5);
}
function readMessage($s){
//TODO: Fix so it can read any lenght
$message = @socket_read($s, 1024*10);
if($message === false){
return false;
}
return $message;
}
//The server is added in the $clients
//The reason for this is because new connection comes as read.
openConnection();
$clients = array();
$null = null;
while(true){
//Copy $clients so the list doesn't get modified by socket_select();
$read = $clients;
$write = $clients;
$read[] = $server;
//Wait for read or write
$ready = socket_select($read, $write, $null, 0);
//Check if the servers is among the $read clients
//If it is, then a someone new is trying to connect.
if(in_array($server, $read)){
//Search to find the server in $clients
//It's needed since socket_select() demand we use $read instead of $server
foreach($read as $client){
if($client == $server){
//We found the new connection, and accepts it.
//TODO: Make a verify code to check it's a scanner.jar that joins
$new = socket_accept($client);
$clients[] = $new;
continue 2;//<-- $server was found, so no need to search anymore.
}
}
}
foreach($read as $client){
$message = readMessage($client);
if($message === false){
//Socket is closed or lost connection
$key = array_search($client, $clients);
unset($clients[$key]);
continue 2;
}else{
//You got the message
echo $message;
}
}
foreach($write as $client){
sendMessage($client,rand(0,99999));
}
sleep(1);
}
socket_close($server);
mysql
installed and running? Are you sure that is the location of your socket? – prodigitalson/var/run/mysqld/mysqld.sock
is this the correct location? If your home page is running does it use PDO? – prodigitalson