How to Auto run PHP file on windows server? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am a newbie of PHP
and I would like to create a php file that can autorun on the windows server at a specific time.
any idea to achieve??
My PHP File
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
Anyidea, Thank you very much.

You may execute a php on windows machine thru the command line:
“php c:\[directory]\[folder]\[phpscript].php”.
So if you want the machine to execute it every day at a specified time, please put the command into the windows scheduler and set the time.

First create .bat file and show the file location as per below.
php D:\wamp\www\test\test.php
You can do by window scheduler from your window machine. Then your file will auto run at your specific mentioned time.

Related

php can't Select or Insert table data inside mariadb database ( Uncaught mysqli_sql_exception: Table 'X' doesn't exist in engine ) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm working on a larger Project that installs a php interpreter via xampp and mariaDB together with a larger project, containing a database and some php files on a blank windows 10 system.
i got everything running so far, the php server is running on command and the mariadb server seems to be reachable too.
The problem I'm facing lies in the fact that our project (which works perfectly under phpstorm and via command line on ANOTHER SYSTEM) doesn't seem retrieve the table data of our database in this VM.
The connection from inside the php is working and the database itself gets recognized too. I checked that by printing it like this:
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "toor";
$dbname = "emensawerbeseite";
$dbport = "42069";
$link= mysqli_connect ($dbhost, $dbuser, $dbpass,$dbname,$dbport);
if(! $link ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully</br>';
$link->set_charset("utf8"); //enables the good old äüö's
$db_list = mysqli_query($link, "SHOW DATABASES"); //mysqli
while ($row = mysqli_fetch_object($db_list)) {
echo $row->Database . "</br>";
}
this prints me this output:
I can also list the all table column names.
The database itself I just Copied in the data folder of mariadb.
It persists of the same .frm and .ibd and .opt file(s), that my working version has.
Problem lies when i try to insert or retrieve any sort of actual data from the database e.g. like this.
$sql2 = "insert into gericht (id, name, beschreibung, erfasst_am, vegetarisch, vegan, preis_intern, preis_extern) values (22, 'CCurrywurst mit Pommes', '', '2022-11-14', false, false, 4.20, 6.90)";
if ($link->query($sql2) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql2 . "<br>" . $link->error;
}
Which is when the the following Error appears:
"Fatal error: Uncaught mysqli_sql_exception: Table 'emensawerbeseite.gericht' doesn't exist in engine in C:\xampp\php\index.php:43 Stack trace: #0 C:\xampp\php\index.php(43): mysqli->query('insert into ger...') #1 {main} thrown in C:\xampp\php\index.php on line 43"
What does this actually mean?
And what could i do about it?
Some Posts suggested to just restart or other things that didn't work.
"Table 'emensawerbeseite.gericht' doesn't exist in engine"
This means that your database is corrupted. In case of InnoDB the dictionary (ibdata1) doesn't contain information about table gericht or is missing at all.

Connecting to a database in SQL and PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm making a website that uses SQL and PHP functionalities. How do I connect to a database?
I would advise you begin by looking here.
You need to ensure that you have created user credentials with the correct permissions to query the database before you try this. You can do this through the cPanel of your web server (I'm going to assume you are using a web hosted server for this question).
Once you have a working and tested connection to the database, you can then start looking at the mySQLi documentation here. Which will show you how to execute and retrieve results from a database query and how to handle the returned data with PHP.
I see you are seriously downvoted.
I learned it the hard way and I am still learning to post here.
Stack sites are supposed to be searched first. If your question is already answered then people downvote you.
The solution to your question:
In your mysql or phpmyadmin you can set whether you use a password or not. The best way to learn is to set mysql with a password in my opinion. If you will launch a website online finally, you have to take security measures anyway.
If you make contact to your mysql database with you have to set:
username, password, database and servername ( for instance localhost).
The most secure way is using the OOP / prepared method:
$servername ='localhost';
$username='yourname';
$password='12345';
$dbname='name_database';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($stmt = $conn->prepare("SELECT idnum, col2, col FROM `your_table` WHERE idnum ='5' ")) {
$stmt->execute();
$res = $stmt->get_result();
$qrow = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
var_dump($qrows); // number of rows you have
$total = implode(" / " , $row);
var_dump($total);
$idnum = $row['idnum'];
var_dump($idnum);
}
The easiest way that I do with my site is make a file called db.php containing:
<?php
$host = 'localhost';
$user = 'root';
$pass = 'password';
$db = 'databasename';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
..then in the index.php file, at the top:
<?php
require_once('db.php')
?>

Running SSH command with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have this code
<?php
$host = "MYSERVERIP";
$user = "root";
$password = "123456";
$command = $_GET["command"];
if($command != null) {
//HERE I WANT THE PHP TO SEND THE COMMAND TO THE SERVER
} else {
echo"No command were given!";
}
?>
Simply I want it to work like this: Let's say I go to MYSERVERIP/sendcommand.php?command=reboot then I want my server to reboot, how can I do this? My server is running Debian.
I've tried to understand it by reading http://php.net/manual/en/function.ssh2-exec.php, but that doesn't get me anywhere.
if you want to run command on the same server php runs - just use exec or shell_exec
ther is an example right there on your link...
mix with your stuff would look like
$host = "MYSERVERIP";
$user = "root";
$password = "123456";
$command = $_GET["command"];
if($command != null) {
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, $_GET["command"]);
} else {
echo"No command were given!";
}
Assuming the server would accepts commands like this
If your file is same server with your php file, you can use
exec() or shell_exec( string $cmd )
If Not same server, it's not possible, that all i know
If you want to issue a command on a different server than the server that runs PHP, you need to start with ssh2_connect.
If you want to issue commands locally on the server, start out by reading about program execution.
I suggest you pay close attention to the security aspect of this.
You should also know that these two options are probably disabled if you are on a shared hosting environment.

PHP: Displaying table contents on html page [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
New to PHP. Trying to simply output the contents of a mySQL table onto my HTML page. Here is my html code:
<body>
<div>
<?php
$hostname = "XXXXX";
$username = "XXXXX";
$password = "XXXXX";
$db_name = "XXXXX";
//connect to database
mysql_connect($hostname, $username, $password) or die("cannot connect to server");
mysql_select_db("$db_name") or die("cannot select database");
$sql="SELECT * FROM myTable";
$result=mysql_query($sql);
if (!$result) {die("SQL error retrieving data.");}
while ($row = mysql_fetch_array($result)) {
echo $row['field1'] . $row['field2'] . "</br>";
}
mysql_close();
?>
</div>
</body>
My output is simply this:
"; } mysql_close(); ?>
I know that there is data in the table. I can't see what I am doing wrong. Can anyone help? Thanks!
Try change your file extension to .php instead of .html
Please change your connection to PDO or mysqli because mysql is soon deprecated
The code is not interpreted as PHP code. Thus, the Browser interprets <?php as the start of a HTML element that is closed by /br>. This HTML element is unknown to the browser and therefore ignored. The following lines
";
}
mysql_close();
?>
are then visible in the browser, because they are not part of a HTML element.
You might need to uncomment an appropriate extension for mysql from php.ini, if you have php installed of course.
Also I would suggest you to use sqlite. It is easy to start and manage you DB, check it out: http://php.net/manual/en/book.sqlite.php

Not able to connect to MySQL server using PDO [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a PHP script which I use to connect to a MySQL database. Connection through mysql_connect works perfectly, but when trying with PDO I get the following error:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'hostname' (3)
the code I use to connect is below:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$hostname_localhost ="hostname";
$database_localhost ="dbname";
$username_localhost ="user";
$password_localhost ="pass";
$user = $_GET['user'];
$pass = $_GET['pass'];
try{
$dbh = new PDO("mysql:host=$hostname_localhost;dbname=$database_localhost",$username_localhost,$password_localhost);
echo 'Connected to DB';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT check_user_company(:user,:pass)");
$stmt = $dbh->bindParam(':user',$user,PDO::PARAM_STR, 16);
$stmt = $dbh->bindParam(':pass',$pass,PDO::PARAM_STR, 32);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row)
{
echo $row['company_id'].'<br />';
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Thanks in advance
Got the same problem. Mine solution was another database port. I wrote localhost:1234 and got this error.
Fixed with:
mysql:host=$hostname_localhost;port=1234;dbname=$database_localhost",$username_localhost,$password_localhost);
echo 'Connected to DB';
It does seem pretty straightforward, here is what I use to build my PDO connectors(noticed your dbname and host are done differently than mine, dunno if that's relevant, but worth a check):
PDO Creation function
require_once('config.inc.php');
function buildDBConnector(){
$dsn = 'mysql:dbname='.C_BASE.';host='.C_HOST;
$dbh = new PDO($dsn, C_USER, C_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
config.inc.php
define('C_HOST','localhost');// MySQL host name (usually:localhost)
define('C_USER','sweetUsername');// MySQL username
define('C_PASS','sweetPassword');// MySQL password
define('C_BASE','superGreatDatabase');// MySQL database
And while it makes no sense, when I tried to declare $dsn inline including variables during the newPDO call, I kept getting failures too. I broke it apart and used the $dsn variable to do so. And haven't had an issue since.
Wondering if you're in shared hosting by chance?
NOTE:
If you don't have a dedicated IP, and instead are going through a NAT, your IP won't properly translate to your actual server.
That help at all?
UPDATE:
Just thought of another thing. Are you trying to connect to a mysql database that is on a different IP than you are running your scripts from? If so, you will likely need to enable remoteSQL access for the ip you are calling the database from. Fairly easy to do, but CRITICAL if you are not accessing localhost.
You dont seem to specify the database host dns details or IP address. Adding that will solve the problem

Categories