SQLite3::exec failed - php

Below is my code. $sqlite->exec cannot return true. I used echo to output the insert statement above this line of code. The statement is correct, and it can be inserted normally when executed directly on the command line. But using $sqlite->exec does not work.
static function addContent($ja, $tc, $sc, $comment) {
$ja = trim($ja);
$tc = trim($tc);
$sc = trim($sc);
$sqlite = new SQLite3('/home/bitnami/htdocs/converter');
if ($sqlite) {
$query = "insert into hanzi values (null, '$ja', '$tc', '$sc', '$comment')";
echo $query . '<br />';
$result = $sqlite->exec($query);
if ($result) {echo '$result is OK';} else {echo '$result os not OK';}
}
return $result;
}
There is no problem with the database connection, that is, the result of $sqlite = new SQLite3('/home/bitnami/htdocs/converter'); is correct, otherwise the echo below could not be executed.
This code was working fine on a Cent OS server before, and now I moved it to a LAMP environment in AWS Lightsail.
Other parts of the code that use the select statement $sqlite->query($query) can be executed.
I used .log to collect the log, but there is no record of execution failure in the log.

Related

How to make the worker continuously run in PHP - Heroku?

I am creating a php application which inserts the record in the DB for every 10 - 20 automatically in heroku to achieve that i had created a proc file and i'm using worker inside that . While deploying the application in the heroku there is found out while running the worker I am new to this heroku and PHP. Can any one help me on what this mean and how can i resolve it and make the worker run continuously to insert the record in the Database.
This is line in my proc file contains which is under root of the project
worker: php /app/worker/db_worker.php
My db_worker.php:
<?php
include_once "./db_config.php";
$conn = $connection;
$number_1 = rand(1,50);
$number_2 = rand(60,500);
$insertQuery = "INSERT INTO random_numbers (num_1, num_2) VALUES ('".$number_1."', '".$number_2."')";
$result = mysqli_query($GLOBALS['conn'], $insertQuery);
if($result) {
echo 'Details saved';
} else {
echo 'Failed to save details';
}
?>
The simplest solution would be to run your operations in a loop. For example,
<?php
while (true) {
// assume this can be included repeatedly
include_once "./db_config.php";
$conn = $connection;
$number_1 = rand(1,50);
$number_2 = rand(60,500);
$insertQuery = "INSERT INTO random_numbers (num_1, num_2) VALUES ('".$number_1."', '".$number_2."')";
$result = mysqli_query($GLOBALS['conn'], $insertQuery);
if($result) {
echo 'Details saved';
} else {
echo 'Failed to save details';
}
// recommend to do some memory clean up
// ...
// wait for 5 seconds
sleep(5);
}
But there's a problem. PHP is not considered memory safe. There can be memory leak issue(s) running it for really long time. If this didn't work well, you might simply do this as a static script with the help of scheduler services like cron-job.org.

PHP while loop break after sent

I sent a PHP infinite loop and I can't stop it, I deleted the file on the server but it still continues to enter data into the DATABASE, is there any way to deactivate it?
$q = "INSERT INTO `gancxadebebi`
($keys)
VALUES
($values)";
$var = 0;
while(true){
sleep(1);
$r = mysqli_query($c, $q);
if($r){
echo 'success';
}else{
echo 'error';
}
++$var;
echo $var;
if($var == '100000'){
break;
}
}
I'm guessing this is a development machine. If that is the case, just restart the web-server to terminate the PHP script. If you are using apache, that would look like:
sudo apache2ctl restart
Don't run this command when you are working in a production machine.

foreach Data Not Updateing in database

In my local server this script works fine. When I upload this script on live it does not work properly.
It inserts only 126 rows of data into the database, but I need to upload at least 500 rows at a time.
<?php
include 'database-config.php';
foreach($_POST['classroll'] as $row=>$classroll)
{
$sclassroll = $classroll;
$mark = $_POST['mark'][$row];
$type = $_POST['rtype'];
$session = $_POST['rsession'];
$department = $_POST['rdepartment'];
$examtype = $_POST['rextype'];
$examyear = $_POST['rexyear'];
$examsubject = $_POST['rexmarksubject'];
$stmt = $dbh->prepare("INSERT INTO exammarks(studnettype, studentsession, studentdepartment, studentclassroll, examtype, examyear, examsubjec, exammarks) VALUES (:studnettype, :studentsession, :studentdepartment, :studentclassroll, :examtype, :examyear, :examsubjec, :exammarks)");
$stmt->bindParam('studnettype', $type);
$stmt->bindParam('studentsession', $session);
$stmt->bindParam('studentdepartment', $department);
$stmt->bindParam('studentclassroll', $sclassroll);
$stmt->bindParam('examtype', $examtype);
$stmt->bindParam('examyear', $examyear);
$stmt->bindParam('examsubjec', $examsubject);
$stmt->bindParam('exammarks', $mark);
$stmt->execute();
}
header('Location: ../home.php');
?>
It is possible that your exammarks table definition on your live server contains a unique index that is not present on your local host server. If that were true some of your INSERT operations might fail.
The code you showed us doesn't check for errors. Obviously, when your program deals with high value data (such as the results of student examinations) you should check for errors.
Try this instead:
if( !$stmt->execute()) {
print_r( $arr = $stmt->errorInfo() );
}
else {
/* INSERT statement completed correctly */
}

syntax error in valid if statement

I have a PHP method which retrieves data either from memcache, or from the database if it is not yet available there.
Sometimes the data must be loaded from the DB to update the memcache, for example if something changes.
public function get_likes_per_user($u_id, $force_db = false) {
if ($this->memcache) {
$u_id = intval($u_id);
$key = 'liked_news_' . $u_id;
$res = $this->memcache->get($key);
if ($res != '') {
$res = unserialize($res);
}
// This line causes a syntax error for no obvious reason
if(!is_array($res) || $force_db){
$res = array();
$sql = "...";
$uu_query = $this->datenbank->query_result($sql);
while ($row = $uu_query->fetch_row()) {
$res[] = $row['uu_id'];
}
$this->memcache->set($key, serialize($res), 0, 3600);
}
return $res;
} else {
throw new Exception("Kein Memcache Objekt!");
}
}
For some reason I don't understand php decides to throw a syntax error for the if statement which decides whether or not to reload the data from the DB.
I tried different notations for this statement but I can't figure out what causes the error.
Did anyone encounter something similar?
PHP Version 5.4
UPDATE:
I've found the cause of the error. I am working on a mac and to write a pipe charakter you have to press alt+7. If you press alt+spacebar mac OS X will insert a non-breaking space, which is invalid for PHP. So my mistake was to keep alt pressed after typing the two pipes for the OR statement.

Calling a MS SQL stored Procedure From PHP

i have a stored procedure running in MSSQL 2008 R2, i am using PHP 5.3, i can connect successfully to the db and retrieve data, but i have been trying to call a stored procedure and haven't succeeded, i need to pass parameters to the stored procedure then get the results back here is my code but its not executing successfully. anyone please with an example on how i can do this.
<?PHP
$sql = " { call rpt_TOP_PRODUCTS } ";//my stored procedure
$param1 = 10;
$param2 = 'E';
$param3 = 'SZ';
$params = Array(Array($param1,$param2,$param3, SQLSRV_PARAM_IN)//parameters to be passed );
$stmt = sqlsrv_query($conn,$sql,$params);
if ($stmt===false) {
// handle error
echo 'Success No';//THIS IS WHERE ITS GOING WHEN I RUN THE CODE
print_r(sqlsrv_errors,true);
} else {
if (sqlsrv_execute($stmt)===false) {
// handle error. This is where the error happens
print_r(sqlsrv_errors,true);
echo 'Success Not ';
} else {
echo 'Success True Yeah';//THIS IS WHERE I WANT IT TO COME.
}
}
<?
Your print_r() calls are wrong, they should be
print_r(sqlsrv_errors(), false);
^^---missing in yours
Without the (), PHP sees that as an undefined constant and outputs nothing. With the (), it's a function call that'll return the error messages from your DB call.
Try rerunning your code with that and see what the errors are.
You need to define where your params are in the query
call rpt_TOP_PRODUCTS(?,?)
Better late than never. Marc B is right about your print_r functions. Also, with the sqlsrv PHP functions, you have to prepare a query with "sqlsrv_prepare()" before you can execute it. Just change:
$stmt = sqlsrv_query($conn,$sql,$params)
to:
$stmt = sqlsrv_prepare($conn,$sql,$params)
And it should run just fine.
Try this
<?php
$sql = " { call rpt_TOP_PRODUCTS} ";//my stored procedure
$param1 = 10;
$param2 = 'E';
$param3 = 'SZ';
$params = array($param1,$param2,$param3);//parameters to be passed );
$stmt = sqlsrv_prepare($conn,$sql,$params)or die(print_r(sqlsrv_errors(),true));
if ($stmt===false) {
// handle error
echo 'Success No';//THIS IS WHERE ITS GOING WHEN I RUN THE CODE
print_r(sqlsrv_errors(),true);
} else {
if (sqlsrv_execute($stmt)===false) {
// handle error. This is where the error happens
print_r(sqlsrv_errors(),true);
echo 'Success Not ';
} else {
echo 'Success True Yeah';//THIS IS WHERE I WANT IT TO COME.
}
}?>
If your procedure is not there, you will get an error saying the procedure could not be found

Categories