I'm working with SQLite on my webserver and have had no problems until now.
$sql = "SELECT * from TeammateCurrent;";
$ret = $db->query($sql);
if($ret != false) {
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
$uniqueid = $row['uniqueID'];
$name = $row['Name'];
$lineID = $row['LineID'];
$job = $row['Job'];
$sunday = $row['Sunday'];
$monday = $row['Monday'];
$tuesday = $row['Tuesday'];
$wednesday = $row['Wednesday'];
$thursday = $row['Thursday'];
$friday = $row['Friday'];
$saturday = $row['Saturday'];
//echo $name."<br>".$lineID."<br>".$job."<br>".$sunday."<br>".$monday."<br>".$tuesday."<br>".$wednesday."<br>".$thursday."<br>".$friday."<br>".$saturday."<br><br>";
$sql = "INSERT INTO TeammateHistory (uniqueID, Name,LineID,Job,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)
VALUES (NULL, '$name','$lineID','$job','$sunday','$monday','$tuesday','$wednesday','$thursday','$friday','$saturday');";
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
}
$sql2 = "UPDATE TeammateCurrent set Sunday='$day0', Monday='$day1', Tuesday='$day2', Wednesday='$day3', Thursday='$day4', Friday='$day5', Saturday='$day6' where uniqueID='$uniqueid';";
$ret = $db->exec($sql2);
if(!$ret){
echo $db->lastErrorMsg();
} else {
}
}
} else {
echo "Query not successful";
}
When the code above executes, I get the error Fatal error: Call to a member function fetchArray() on a non-object in /home/xxxx/public_html/xxxx.com/trg/rc2/db/startNewWeek.php on line 39
I've ran code just like this on other pages and they work fine, I'm at a total loss here.
My tables:
you're reusing the variable $ret. You should have a different return variable for the SELECT statment and UPDATEs. In the second loop you're trying to do a fetchArray() of the return of an UPDATE statement.
Related
I have a android app which submit data through php but sometime the data insert twice when I submit the data.
I'm sure the issue is not on the app as the button is disabled once I press the button. Please help me spot my mistake. Thanks!
Following is the data inserted:
Following is my query:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$area = $_POST['area'];
$date = $_POST['date'];
$batch = $_POST['batch'];
$workerName = $_POST['workerName'];
$remarks = $_POST['remarks'];
$time = $_POST['time'];
$tank = $_POST['tank'];
$sid= '';
$submissionDateTime = $_POST['submissionDateTime'];
require_once 'connectdb.php';
$stmt = $conn ->prepare("SELECT DISTINCT sid FROM feedingforms WHERE area = :area AND tank = :tank AND date = :date AND batch = :batch AND time = :time");
$stmt->bindParam(":area", $_POST['area']);
$stmt->bindParam(":tank", $_POST['tank']);
$stmt->bindParam(":date", $_POST['date']);
$stmt->bindParam(":batch", $_POST['batch']);
$stmt->bindParam(":time", $_POST['time']);
$stmt->execute();
$stmt->fetch();//fecth
switch ($stmt->rowCount() > 0){
case "0":
$stmt2 = $conn->prepare("INSERT INTO feedingforms(sid, area, date, batch, workerName, submissionDateTime, remarks, time, tank) VALUES(:sid, :area, :date,:batch,:workerName,:submissionDateTime, :remarks, :time,:tank)");
$stmt2->bindValue(":sid", $sid);
$stmt2->bindValue(":area", $area);
$stmt2->bindValue(":date", $date);
$stmt2->bindValue(":batch", $batch);
$stmt2->bindValue(":workerName", $workerName);
$stmt2->bindValue(":submissionDateTime", $submissionDateTime);
$stmt2->bindValue(":remarks", $remarks);
$stmt2->bindValue(":time", $time);
$stmt2->bindValue(":tank", $tank);
if ($stmt2->execute()) {
$result["success"]= "1";
$result["message"] = "success";
echo json_encode($result);
$conn = null;
exit;
break;
}else{
$result["success"]= "0";
$result["message"] = "error";
echo json_encode($result);
$conn = null;
exit;
break;
}
default:
$result["success"]= "2";
$result["message"] = "duplicate";
echo json_encode($result);
exit;
$conn = null;
}
}
?>
Firstly I got the workers name from BIRTHDAYS and then want to get e-mail address from USERS.There is no problem to take workers name's from Table1 but when I try to get the e-mail addresses the db returns me NULL.My DB is mssql.
<?php
include_once("connect.php");
$today = '05.07';
$today1 = $today . "%";
$sql = "SELECT NAME FROM BIRTHDAYS WHERE BIRTH LIKE '$today1' ";
$stmt = sqlsrv_query($conn,$sql);
if($stmt == false){
echo "failed";
}else{
$dizi = array();
while($rows = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
$dizi[] = array('NAME' =>$rows['NAME']);
$newarray = json_encode($dizi,JSON_UNESCAPED_UNICODE);
}
}
foreach(json_decode($newarray) as $nameObj)
{
$nameArr = (array) $nameObj;
$names = reset($nameArr);
mb_convert_case($names, MB_CASE_UPPER, 'UTF-8');
echo $sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn,$sql2);
if($stmt2 == false)
{
echo "failed";
}
else
{
$dizi2 = array();
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
}
}
?>
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
you put in $rows1 and would take it from $rows NULL is correct answer :)
take $rows1['EMAIL'] and it would work
and why foreach =?
you can put the statement in while-loop like this:
while ($rows = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$names = $rows['NAME'];
$sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn, $sql2);
if ($stmt2 == false) {
echo "failed";
} else {
$dizi2 = array();
while ($rows1 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
$dizi1[] = array('EMAIL' => $rows1['EMAIL']);
echo $newarray1 = json_encode($dizi1, JSON_UNESCAPED_UNICODE);
}
}
}
I'm a learner of PHP. This commit and rollback process does the task at each line. How can I commit and rollback all at once?
<?php
$con = mysql_connect('localhost','user', 'abcdefg');
if(!con)
{
echo "Can't connect to db.";
}else {
mysql_select_db('test');
}
if(!file_exits('test.csv')
{
echo "Can't find the file.";
}
$ar_1 = file('test.csv', FILE_IGNORE_NEW_LINES);
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === true)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}else {
//Rollback
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
}
}
mysql_close($con);
?>
you need to commit out of the loop, i used $bool to see if there somewhere the $result isn't true
it will look something like :
edit:
breaking from the loop when something is wrong as you don't need to continue looping to other elements.
$bool = 1;
foreach ($ar_1 as $ar1)
{
$test = explode(",", $ar1);
$sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
$result = mysql_query_($sql);
$sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
$result = mysql_query($sql);
if($result === false)
{
//Rollback
$bool = 0;
$sql = "rollback";
mysql_query($sql);
echo "Rollback";
break;
}
}
if($bool == 1)
{
//Commit
$sql = "commit";
mysql_query($sql);
echo "Committed";
}
mysql_close($con);
I know that the error says that format isn't a valid code, but I used it before and it worked fine. So I hope someone knows a code to replace it with or now how to fix it.
require_once 'config.php';
$checker = "SELECT StartTime FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$checker2 = "SELECT EndTime FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$checker3 = "SELECT Minutes FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$result1 = mysqli_query($mysqli, $checker);
$result2 = mysqli_query($mysqli, $checker2);
$minutes = mysqli_query($mysqli, $checker3);
$starttime = new DateTime($result1);
$endtime = new DateTime($result2);
while($starttime <= $endtime)
{
echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";
$starttime = date_add($starttime, date_interval_create_from_date_string($minutes, ' min'));
}
echo " </select>";
and above I grab the vars out of the database
The problem is mysqli_query returns a mysqli_result which you need to read before using:
require_once 'config.php';
$checker = "SELECT StartTime, EndTime, Minutes FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$result = mysqli_query($mysqli, $checker); //Execute query
if (!$result) { //Result may be false if there's an error
echo "Error getting result";
}
$row = mysqli_fetch_assoc($result); //Put first result in an array
if (!$row) { // $row will be false if there's no result
echo "No data found";
}
//Notice how the array entry keys match the columns in the query.
$starttime = new DateTime($row["StartTime"]);
$endtime = new DateTime($row["EndTime"]);
$minutes = $row["Minutes"];
while($starttime <= $endtime)
{
echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";
$starttime = date_add($starttime, date_interval_create_from_date_string($minutes, ' min'));
}
echo " </select>";
Rightnow i am facing a problem of print Long Text. In my database i have data in long text format and that field has around 2000 line of text so how can i print that.
I am giving here full code of my file please go through it and try to solve my problem
<?php
if($objConnect = mysql_connect("localhost","root","")){
$objDB = mysql_select_db("android");
$from_date=$_GET['from_date'];
$to_date=$_GET['to_date'];
$time = strtotime($from_date);
$time1 = strtotime($to_date);
$fdate = date( 'Y-m-d', $time );
$todate = date( 'Y-m-d', $time1 );
$tmpdate="";
$time2 = strtotime($tmpdate);
$tempdate = date( 'Y-m-d', $time2 );
if($fdate==$tempdate){
echo "if block";
$strSQL = "SELECT LessonText FROM lesson_details ";/*Here this field LessonText is Long Text*/
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
$Jdata=(json_encode(array('lessons:'=>$resultArray)));
if($Jdata!=""){
$error_code=0;
$error_massage="Success";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code,'error_massage:'=>$error_massage));
echo json_encode($Result);
}
else{
echo "Else Block";
$error_code=1;
$error_massage="No Record Found";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code),array('error_massage:'=>$error_massage));
echo json_encode($Result);
}
}
elseif($fdate!="1970-01-01" && $todate!="1970-01-01"){
$strSQL = "SELECT * FROM lesson_details WHERE UpdatedDate>='$fdate' &&UpdatedDate<='$todate'";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
$Jdata=(json_encode(array('lessons:'=>$resultArray)));
if($Jdata!=""){
$error_code=0;
$error_massage="Success";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code,'error_massage:'=>$error_massage));
echo json_encode($Result);
}
else{
echo "Else Block";
$error_code=1;
$error_massage="No Record Found";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code),array('error_massage:'=>$error_massage));
echo json_encode($Result);
}
}
else{
$strSQL = "SELECT * FROM lesson_details WHERE UpdatedDate>='$fdate'";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
$Jdata=json_encode($resultArray);
$Jdata=(json_encode(array('lessons:'=>$resultArray)));
if($Jdata!=""){
$error_code=0;
$error_massage="Success";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code,'error_massage:'=>$error_massage));
echo json_encode($Result);
}
else{
echo "Else Block";
$error_code=1;
$error_massage="No Record Found";
$Result=array(array('Lesson_details:'=>$resultArray),array('error_code:'=>$error_code),array('error_massage:'=>$error_massage));
echo json_encode($Result);
}
}
}
else{
$error=array("ErrCode"=>2,"ErrMsg"=>"Database connection error");
echo (json_encode($error));
}
?>
I have find out the solution so i am sharing here
in my case it is the problem of utf8 encoding. so i have add following line below my select query
mysql_query('SET CHARACTER SET utf8')
Something like this, I believe:
print $longtext;