I was wondering why I got an error saying Notice: Undefined variable: query_add_landdev_temp_payroll in C:\xampp\htdocs\op\ajax\ajaxLanddevNTPPayroll.php on line xx. for the past 2 weeks, it works perfect. When I tried to execute the code today, I got that kind of error. Here is my code:
$query_get_payroll = mysqli_query($new_conn, "SELECT ntp_with_ob_payroll_transaction.ntp_id, ntp_with_ob_payroll_transaction.allotment_code, ntp_with_ob_payroll_transaction.category_name, ntp_with_ob_payroll_transaction.block_number, ntp_with_ob_payroll_transaction.activity, ntp_with_ob_payroll_transaction.lot_number, ntp_with_ob_payroll_transaction.labor_cost, other_budget.quantity FROM ntp_with_ob_payroll_transaction JOIN other_budget ON ntp_with_ob_payroll_transaction.ob_id = other_budget.ob_id WHERE other_budget.transaction_id = $transaction_id AND other_budget.ob_number = '$ntp_number' AND is_payroll = 0");
while($row = mysqli_fetch_assoc($query_get_payroll)) {
$ntp_id = $row['ntp_id'];
$allotment_code = $row['allotment_code'];
$category_name = $row['category_name'];
$block_number = $row['block_number'];
$activity = $row['activity'];
$lot_number = $row['lot_number'];
$labor_cost = $row['labor_cost']; //this is equivalent to unit cost
$quantity = $row['quantity']; //this is equivalent to total percentage
$query_add_landdev_temp_payroll = mysqli_query($new_conn, "INSERT INTO temp_payroll_landdev(ntp_id, userid, transaction_id, ntp_number, contractor_name, allotment_code, category_name, block_number, activity, lot_numbers, regular_labor, quantity) VALUES($ntp_id, $_SESSION[userid], $transaction_id, '$ntp_number', '$fullname', '$allotment_code', '$category_name', $block_number, '$activity', '$lot_number', '$labor_cost', '$quantity')");
}
if($query_add_landdev_temp_payroll) {
echo 1;
//database file name
$database_file = $database.'.sql';
$new_database_file = $new_database.'.sql';
if(file_exists('backup/'.$new_database_file)) {
unlink('backup/'.$new_database_file);
//backup project database
$command = "C:/xampp/mysql/bin/mysqldump --host=$new_host --user=$new_user --password=$new_pass $new_database > backup/$new_database_file";
system($command);
} else {
//backup project database
$command = "C:/xampp/mysql/bin/mysqldump --host=$new_host --user=$new_user --password=$new_pass $new_database > backup/$new_database_file";
system($command);
}
} else {
echo 0;
}
If the query doesn't return any rows, mysqli_fetch_assoc() will return false the first time, so you'll never go into the loop, and never assign to the variable. If you then try to use the variable you get that warning.
You can give it a default initial value before the loop, or you can change your test to:
if (!empty($query_add_landdev_temp_payroll))
BTW, the variable only contains the result of the INSERT in the last iteration of the loop. Maybe you should put the if block inside the loop, right after the INSERT?
Also, there's no need to do INSERT in a loop. You can do the whole thing with a single query:
INSERT INTO temp_payroll_landdev(ntp_id, userid, transaction_id, ntp_number, contractor_name, allotment_code, category_name, block_number, activity, lot_numbers, regular_labor, quantity)
SELECT ntp_with_ob_payroll_transaction.ntp_id, ntp_with_ob_payroll_transaction.allotment_code, ntp_with_ob_payroll_transaction.category_name, ntp_with_ob_payroll_transaction.block_number, ntp_with_ob_payroll_transaction.activity, ntp_with_ob_payroll_transaction.lot_number, ntp_with_ob_payroll_transaction.labor_cost, other_budget.quantity
FROM ntp_with_ob_payroll_transaction
JOIN other_budget ON ntp_with_ob_payroll_transaction.ob_id = other_budget.ob_id
WHERE other_budget.transaction_id = $transaction_id AND other_budget.ob_number = '$ntp_number' AND is_payroll = 0
just update the if condition like this,
if(isset($query_add_landdev_temp_payroll) && $query_add_landdev_temp_payroll)
Looks like you are using a variable inside a while command. So outside that command this variable will not define. Try use $query_add_landdev_temp_payroll = null; before while command. Hope it help you!
Related
I have a query that returns all the data while running at MSSQL, but when I try to get the result with php code it returns null
SELECT:
$query = "SELECT DISTINCT (E080SER.desser) as desser,
E080SER.CODFAM codfam, e085cli.apecli apecli,
E085CLI.CODCLI codcli, E085CLI.NOMCLI nomeCli
FROM
E160CTR,
E160CVS, e080ser,
E085CLI,
E070EMP,
E070FIL
WHERE
e070emp.nomemp like '%Gestão tech%' and
e080ser.codser = e160cvs.codser and
e080ser.codser like ('%manw%') and (E160CTR.CODEMP = 1) and
((E160CTR.CODEMP = E070FIL.CODEMP) AND (E160CTR.CODFIL =
E070FIL.CODFIL) AND
(E160CTR.CODCLI = E085CLI.CODCLI) AND (E160CVS.CODEMP =
E160CTR.CODEMP) AND
(E160CVS.CODFIL = E160CTR.CODFIL) AND (E160CVS.NUMCTR =
E160CTR.NUMCTR)) AND
(E160CTR.SITCTR = 'A') and e080ser.sitser = 'a' and
E080SER.CODEMP IN (1, 9)
order by e080ser.desser";
PHP CODE:
$sql = sqlsrv_query($conn, $query);
while($item = sqlsrv_fetch_array($sql)){
var_dump($item);
}
Sometimes it's necessary to fetch all result sets with sqlsrv_next_result() to get your data. You may try with this:
<?php
...
$sql = sqlsrv_query($conn, $query);
do {
while($item = sqlsrv_fetch_array($sql)){
var_dump($item);
}
} while (sqlsrv_next_result($sql));
...
?>
There is an extra semicolon after the while loop, i.e. the body of the loop is empty. Then the result you try to read is after the last row, that's why you don't get what you expected.
I've found the problem
The problem was the encoding, I put the $query inside of utf8_encode(), and now it is returning the results.
Thank you all for your time.
I have this script executing as a cron job everyday to update days remaining to pay invoices. I first query every row of my table and attempt to store the data in a multidimensional array but this seems to be storing everything I query in the first element of my array.
Here's my script:
<?php
include '../inc/dbinfo.inc';
ini_set("log_errors", 1);
ini_set("error_log", "/tmp/php-error.log");
error_log( "################################################# UpdateVendorInvoiceDays.php #################################################" );
$three = 3;
$fetchAllInvoices = "SELECT VENDORINVOICEID, VdrInvoiceReceived, PaymentDue, COUNT(*), DATEDIFF(PaymentDue, NOW()) FROM tblVendorInvoices WHERE VdrInvoiceStatusID != ?";
$getInvoices = $conn->prepare($fetchAllInvoices);
$getInvoices->bind_param("i", $three);
$getInvoices->execute();
$result = $getInvoices->get_result();
$rows = array();
$j = 0;
while($row = $result->fetch_assoc())
{
$rows[$j][] = $row;
$j++;
}
echo json_encode($rows[0][0]); //Only outputs one row
//UPDATE DAYS REMAINING IN EACH ENTRY THAT ISNT PAID
$updateDaysRemaining = "UPDATE tblVendorInvoices SET DaysRemaining = ? WHERE VENDORINVOICEID = ? AND VdrInvoiceStatusID ! = ?";
$setDays = $conn->prepare($updateDaysRemaining);
$k = 0; //incrementor
$numberOfEntries = $rows['COUNT(*)'];
for($k;$k<$numberOfEntries;$k++){
$setDays->bind_param("iii", $rows[$k]["DATEDIFF(PaymentDue, NOW())"],
$rows[$k]['VENDORINVOICEID'], $three);
if($setDays->execute()){
error_log('Cron success');
}else{
error_log('Cron fail');
}
}
?>
Currently the output from my first query is:
[[{"VENDORINVOICEID":88,"VdrInvoiceReceived":"2018-08-21","PaymentDue":"2018-07-27","COUNT(*)":2,"DATEDIFF(PaymentDue, NOW())":-25}]]
and my error log only gives me a notice for $rows['COUNT(*)'] being undefined (which makes sense)
I've looked at other answers here but they don't seem to have the same structure as I do.
EDIT: I also have 2 rows in my database but this only puts out one. I forgot to mention this.
There are a couple of simplifications to get all of the rows. Instead of...
while($row = $result->fetch_assoc())
{
$rows[$j][] = $row;
$j++;
}
echo json_encode($rows[0][0]);
You can just return all rows using fetch_all()...
$rows = $result->fetch_all (MYSQLI_ASSOC);
echo json_encode($rows);
Then encode the whole array and not just the one element - which is what $rows[0][0] was showing you.
As for you other problem - change in your select statement to
COUNT(*) as rowCount
and then you can use this alias for the field reference...
$rows['COUNT(*)']
becomes
$rows['rowCount']
EDIT1 : used double quotes and single quotes but I am getting same error.
EDIT2 : same query is returning me result in mysql shell
I am selecting a row from a table.
if(!isset($_GET['title']) || !isset($_GET['user'])){
echo "hi"; //something come here
}
else{
$title = $_GET['title'];
$title = mysqli_real_escape_string($conn,$title);
$user = $_GET['user'];
$user = mysqli_real_escape_string($conn,$user);
echo $title ;
echo $user ;
// tried giving value directly to test but no luck
$query = "SELECT * FROM site WHERE client=\"Chaitanya\" && title=\"werdfghb\" ";
$result5 = mysqli_query($conn,$query) or die(mysqli_error());
$count = mysqli_num_rows($result5);
echo $count ;
while($result9 = mysqli_fetch_array($result5)){
$kk=$result9['url'];
echo $kk ;
}
$page = $kk;
include ( 'counter.php');
addinfo($page);
}
In my database there is a row with columns title and client and the values I entered are in that row but when I echo count(no of rows) it is showing zero.
Is there anything wrong with code ?
The error you are getting is due to the line
$page = $kk;
in this code $kk is not declared previously. The defined $kk is in the while loop scope.
declare the variable like this in the outer scope from the while loop
...
$kk = null;
while($result9 = mysqli_fetch_array($result5)) {
$kk = $result9['url'];
echo $kk ;
}
$page = $kk;
...
Error on Fetching Data
You have to crack you SQl into smaller pieces and test the code like this.
run the query SELECT * FROM site without any where and get the count
run the query SELECT * FROM site WHERE client='Chaitanya' and get the count
SELECT * FROM site WHERE title='werdfghb' and check the count
Then run the whole query
And see the results. This way u can find out in where the issue is in your SQL code. I prefer you use the mysql client to execute this queries
As I pointed out in my comment, $kk is undefined in the $page = $kk;, since it is declared in the while loop.
Do something like:
$kk = ''; //can also do $kk=NULL; if you want.
while($result9 = mysqli_fetch_array($result5)) {
$kk=$result9['url'];
echo $kk ;
}
$page = $kk;
try this one
$client = "Chaitanya";
$title = "werdfghb";
$query="SELECT * FROM site WHERE client='".$client."' and title='".$title."' ";
you can also use this
$query="SELECT * FROM site WHERE client={$client} and title={$title} ";
I need help on this.
I have to write a query 12 times with just a different value so I did this and it worked fine:
for ($q=1; $q<=11; $q++) {
$ingConcepto[$q] = "SELECT
tbl_ingresos.cantidadpagada AS cp,
tbl_conceingresos.concepto AS concepto,
tbl_ingresos.fechapertenece
FROM
tbl_ingresos
LEFT JOIN
tbl_conceingresos
ON
tbl_ingresos.idtipoingreso = tbl_conceingresos.idingreso
WHERE
MONTH(fechapertenece) = '$q'
AND
YEAR(fechapertenece) = $elcueri1";
$resIC[$q] = mysql_query($ingConcepto[$q],$tewNEW);
$ingConcepto[$q] = mysql_fetch_array($resIC[$q]);
}
The problem comes when I need to print a value it does not display anything:
echo $ingConcepto7['cp'];
Thanks.
I'm trying to write a script that gets data from a sql server based on the id of the entry in my data base. when I try to access the page using the link with the id of the entry it returns as if it does not recognize the id. Below is the php code :
<?php
require('includes/config.inc.php');
require_once(MYSQL);
$aid = FALSE;
if (isset($_GET['aid']) && filter_var($_GET['aid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {
$aid = $_GET['aid'];
$q = "SELECT aircraft_id, aircraft_name AS name, aircraft_type AS type, tail_number AS tn FROM aircraft USING(aircraft_id) WHERE aircraft_id = $aid";
$r = mysqli_query($dbc, $q);
if (!(mysqli_num_rows($r) > 0)) {
$aid = FALSE;
}
}// end isset tid
if ($aid) {
while ($acdata = mysqli_fetch_array($r, MYSQLI_ASSOC)){
echo'<h2>'. $acdata['tail_number'] .'</h2>';
}
} else {
echo '<p>This pages was accessed in error.</p>';
}
?>
Any hints?
Try to var_dump($q); before $r = mysqli_query($dbc, $q); to inspect your query and then just execute it through phphmyadmin or in MySQL server terminal directly and see what does it return.
Update:
Use var_dump($q);die(); to stop script from executing after dumping.
You are using a field alias in your query, so you must use that in your echo to:
echo'<h2>'. $acdata['tn'] .'</h2>';
Get rid of the USING(aircraft_id), it causes an error and your query doesn't execute.
"SELECT aircraft_id, aircraft_name AS name, aircraft_type AS type, tail_number AS tn FROM aircraft WHERE aircraft_id = $aid"
I guess it's a leftover from a previous version of the query? Using (id) is a shortcut for
FROM
foo
INNER JOIN bar ON foo.id = bar.id
It can be used when the tables to be joined are joined on columns which have the same name. Just shorter to write.
Since you are not joining you have to remove it.