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();
?>
Related
I am creating a messaging website, which uses a MySQL database to store the message data. However, when there are only 5 users online at the same time, it starts failing with "MySQLi: Cannot Connect: Too many MySQL connections".
I have heard that my host, HelioHost, limits the maximum MySQL connections to four, which explains my error (see this for details).
I want to know how I can change my scripts (which currently connect when the message form is submitted, add it to the database, then disconnect) can only use a maximum of four connections over the server.
Here's some code:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("MySQL server connection failed: " . $conn->connect_error);
}
//Process data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_SESSION["username"];
$parent = $_POST["thread"];
$content = test_content($_POST["content"]);
$sql = "INSERT INTO `threads_replies`(`Parent`, `Author`, `Content`) VALUES ('" . $parent . "','" . $name . "','" . $content . "')";
$conn->query($sql) or die ("Failed MySQL query to save reply");
echo "Reply saved. <span class=\"fa fa-chevron-circle-left\"></span> Back to Thread";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function test_content($data) {
$data = str_replace("'", "'", $data);
return $data;
}
Any ideas?
Right from the docs:
http://php.net/manual/en/function.mysqli-connect.php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
$error = mysqli_connect_error();
echo "Failed to connect to MySQL: $error";
echo 'Sorry, pal, 5 already playing around...';
exit;// or location('wait.php');
}
# ....
mysqli_close($link);
For the 5th user - he should reload the page, you can redirect him to something like wait.php:
<!-- htm, head, please add.. -->
<?php
// no need in PHP, actually
?>
<h3>Sorry, having too much users, will reload automatically...</h3>
<script>
setTimeout(
()=> location.href =
location.href + '?t=' + (new Date()).getTime(),
5000 // hope 5 seconds will be enough
);
So your code:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
location('wait.php');
die("MySQL server connection failed: " . $conn->connect_error);
}
You can build a cache system which will be checked before accessing the database (and build up a connection). As an example, you don't need to read the news on the front page every time from the database, they don't change that often. So you read the news from the cache system (like some file in the file system) instead and display the data from it. And when the news entries has been changed you delete/invalidate the cache so the cache gets rebuild (once) on the next visit again.
I use the hosting company aPlus.net, and I can't seem to get past a connection error I'm getting when trying to process some php to write database content to a webpage, and I am curious as to if this is because my database appears to not be on the same server as the entire rest of my hosting account, and if there is a way to resolve this in my code? This is my first attempt at writing PHP, and it would be good to know if my code is wrong, or if my hosting company is messing me up. (and either way, how to fix it)
Here's the code that's failing to pull from the database:
{
$con = mysql_connect("localhost","2p5dq9vxmy240651","MY_PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("felineasthma_2p5dq9vxmy240651", $con);
$users_name = $_POST['name'];
$users_comment = $_POST['requests'];
$users_name = mysql_real_escape_string($users_name);
$users_comment = mysql_real_escape_string($users_comment);
$inputid = $_GET['id'];
$query = "
INSERT INTO `felineasthma_2p5dq9vxmy240651`.`submissions` (`id`,
`name`, `requests`, `inputid`) VALUES (NULL,
'$users_name', '$users_comment', '$inputid');";
mysql_query($query);
echo "<h2>Your request has been processed, reload page.</h2>";
mysql_close($con);
}
and here's some screen captures from inside my hosting account (links because I don't have enough posts here yet to upload images, sorry):
felineasthma_2p5dq9vxmy240651 doesn't appear in my hosting account
yet it clearly exists in MySQL Manager, but on a different server
I was even more confused while making the user for this database, as the control panel didn't allow me to make a username, it just randomly assigned one. Help? Advice?
I found a more modern tutorial to learn PHP with, and now everything works, I just need to add security measures now. Here's the working code snippets, if anyone ever comes here asking the same questions.
here's the form action that places the entries into the database:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
$users_name = $_POST['name'];
$users_request = $_POST['requests'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO submissions (name, requests)
VALUES ('$users_name', '$users_request')";
if (mysqli_query($conn, $sql)) {
header("Location: clv.php");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
here's the include that puts the database entries onto the page:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, requests, name FROM submissions";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "" . $row["requests"]. " - by " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
So, i got this code:
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "TestDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br>";
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO testdata (id,address,count) VALUES (DEFAULT,'$ip',1) ON DUPLICATE KEY UPDATE count=count+1";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " ;
}
$conn->close();
?>
With this code ip-addresses of customers are saved and it should increase count by 1 every time they return to the website. When i visit database.php directly it works like it should in every browser, but when i call the php-file via the index.html-page it counts up twice (most of the times, not always) in Mozilla, other browsers no problem, the code in html-file:
<img style="display: none;" src="http://servername/database.php?">
Anyone know why? Please help, i'm really stuck here
Hello I want executing a php file with crontab. This PHP file makes an update of the MySQL database and when I enter the URL in the browser I can execute the file but not as cronjob.
I add
*/5 * * * * php -f /var/www/html/.../update.php >/dev/null 2>&1
to the crontab, and other cronjobs are working well (3rd part extensions).
Do I need to add some code in my PHP file? Thank you.
Here whats inside the PHP file
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,'Instagram:', '')";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully with Facebook, ";
} else {
echo "Error updating record: " . $conn->error;
}
$sql = "UPDATE pm_videos SET `description` = REPLACE( `description` ,'Facebook:', '')";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully with Facebook, ";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
Typically, this is caused by a $PATH issue. Try adding this as the first line of your crontab via crontab -u root -e:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
If this doesn't fix it, you'll need to look in your code for places where you might be calling external commands, or depending on the values of environment variables. If this doesn't work, please edit your post and show the contents of update.php.
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!