Php command line script can't connect to server using MYSQLI - php

I have this command line script I am trying to use. It adds image to an already present database and table. The server has Wamp server installed on it and I can access it from a different computer. But everytime I run the script the script just hangs when connecting.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$filename = $argv[1];
//$filename="images/image10.jpg";
$imgData = file_get_contents($filename);
$size = getimagesize($filename);
$servername="192.168.0.34";
$username="root";
$password="password";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection successful";
// insert into table
$sql = sprintf("insert into images(image_type, image, image_size, image_name) values('%s', '%s', '%d', '%s')",
mysql_real_escape_string($size['mime']),
mysql_real_escape_string($imgData),
$size[3],
mysql_real_escape_string($_FILES['userfile']['name']));
if($conn->query($sql) === TRUE) {
echo "Inserted new image successfully";
} else {
echo "Error inserting image: " . $conn->error;
}
echo "Closing the connection";
$conn = null;
?>
I have only installed php and php-mysql on the client. The client is running debian and the sever is running windows 7. Am I missing something else I need to install?
Thank you

You need to install mysqli on your system. See: http://php.net/manual/en/mysqli.installation.php

Related

Connection failed: No such file or directory (IONOS) [duplicate]

I am working on a payroll system. Everything is working fine on localhost (xampp) but when I upload it to ionos hosting, it is loading the admin login page fine but when i enter the username & password it is giving "Connection failed: No such file or directory".
DB name: dbs4868780
and here is the code:
<?php
$conn = new mysqli('localhost', 'root', '', 'xxxxxxxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
EDIT:
I tried it but it is still giving the same error.
I also tried to connect DB with PHP code provided by ionos but it is still giving "Connection failed. File or Directory not found"
Here is the php code from ionos:
<?php
$host_name = 'db5005797255.hosting-data.io';
$database = 'dbs4868780';
$user_name = 'xxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxx';
$link = new mysqli($host_name, $user_name, $password, $database);
if ($link->connect_error) {
die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p>';
}
?>
See attached fake database example to get your data

Can't connect to MySQL via PHP "Name or service not known"

I have a website, which sends input data with AJAX via a PHP script to a MySQL database.
The JavaScript/AJAX part is working but the PHP script doesn't get executed, and if I paste the URL from the PHP script into my browser I get:
Connection failed:php_network_getaddresses: getaddrinfo failed: Name
or service not known
The host, server, password and database are 100% correct.
My PHP code is
<?php
$host = '';
$user = '';
$pswd = '';
$database = '';
$conn = new mysqli($host, $user, $pswd, $database);
if ($conn->connect_error) {
die ("Connection failed:" . $conn->connect_error);
}
echo "Connection successful";
$name = $_POST['name'];
$sql = "INSERT INTO Names(s) VALUES(?)";
if ($stmt = mysqli_prepare($conn, $sql)) {
$sql->bind_param('s', $name);
$stmt = mysqli_parse($conn, $sql);
$stmt->execute();
echo "Created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Besides the initial issue. You have it a bit jumbled up. The bind_param is off.
if ($stmt = mysqli_prepare($conn, $sql)) {
$stmt->bind_param('s', $name);
But, since you don't make it past die(), there is something wrong with either of $host, $user, $pswd, $database or maybe a firewall or DNS issue somewhere. Or the server might not be running as mentioned in the comments.

PHP can't connect to mysql

I just made my own wamp server on my computer to handle twitch donations for a friend. The friend has a program that I made using c# that connects to the my server and reads the database. This works fine, the program reads my database just fine and lists the entries. BUT I have made a php website to send the entry (a new donation) to the database but I receive a connection timed out error just seconds after I press the button... my mysql server is set to allow every user and host with the correct password. I used the same IP user and password as on the c# program!
I can't figure out why the damn php website hosted on one.com can't connect!!
this is my code:
<?php
if (!empty($_GET['act'])) {
$servername = "81.83.95.34";
$username = "root";
$password = "mypassword";
$dbname = "tbc";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("server failed: " . $conn->connect_error);
}
$sql = "INSERT INTO donations (donations)
VALUES ('1')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
} else {
?>
can anyone help me?
thank you very much!

Connection failed: Host 'xx.xx.xx.xx' is not allowed to connect to this MySQL server - AJAX and AWS

I am trying to insert some data from the client browser to a MySQL database on AWS server.
The code worked on godaddy so it looks like a permissions/security issue.
$servername = "xx.xx.xx.xx";
$username = "user";
$password = "mypass";
$dbname = "dbName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO mytable ('email')
VALUES ('" . $email . "')";
echo $sql;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
I am getting the error saying "Connection failed: Host x.x.x.x is not allowed to connect to this MySQL server "
How do I allow connections to this from a client browser?
I have already commented out the line bind-address = 127.0.0.1
If Mysql is a remote host, than use private IP address of source system to be allowed as AWS would use private IP address to communicate by default.

PHP script wont run on scueduled task (Windows 7)

Attempting to run a php script to update a database every hour with windows 7 Task Scheduler. The File updates when i open it up as a webpage but does not update when i run it from task scheduler . Under the trigger tab I have it set to run daily, and then repeat the task every hour for a duration of indefinetly. Under actions I have it set to Start Program. And the link to my file as C:\Users\jmac\Desktop\testdbconnection.php
Additionally under Start in box I have C:\Users\E15913\Desktop
I have no coditions that should be blcoking it from running. In addition when I test run the file it says it is completed succefully but the database is not updated. I will also post the code below, which once again works when I refresh it or pull it up in a web browser.
<?php
// Did modify login values for privacy
$servername = "10.100.";
$username = "myusername";
$password = "abc123";
$dbname = "informationdata";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$file = fopen("ALMGrade.csv","r");
while(! feof($file)){
$ar =fgetcsv($file);
$sql = "INSERT INTO gradetable_copy (Grade, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6)
VALUES ('$ar[0]', '$ar[1]', '$ar[2]', '$ar[3]', '$ar[4]', '$ar[5]', '$ar[6]' )";
echo $sql;
echo "<br>";\
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
fclose($file);
$conn->close();
?>

Categories