I am trying to send a response back to my front end with the amount of days in a customers billing period. I query this number but would like to append " Days" after it. So far I have:
<?php
require "../../inc/dbinfo.inc";
$projectnum =$_POST['projectnum'];
$sql = $conn->prepare("SELECT (SELECT TermsofPayment FROM tblCustomers WHERE CUSTOMERID = ProjectCustomer) AS NetTerms FROM tblProjects WHERE PROJECTNOID = ?");
if($sql){
$sql->bind_param("i", $projectnum);
$sql->execute();
$hold = $sql->get_result();
$obj = $hold->fetch_assoc();
$addOn = " Days";
$obj->NetTerms = $obj->{'NetTerms'. $addOn};
echo json_encode($obj);
}
$sql->close();
exit();
?>
After trial and error it appears as though the second last line has no effect on the result. I've looked through the web to find no solutions to this (maybe my search didn't contain the right keywords).
Current Response: {"NetTerms":30}
Desired Response: {"NetTerms":30 Days}
You have/had two problems there:
1st you wanted to concatenate a variable to another one and had a wrong syntax.
$addOn = " Days";
$obj->NetTerms = $obj->{'NetTerms' .$addOn};
// this would try to get a value of `$obj->NetTerms Days`, which doesn't exist.
should have been
$obj->NetTerms = $obj->NetTerms . $addOn;
// or
$obj->NetTerms .= $addOn;
All of this threw an error, because $obj is an array (as return from fetch_assoc()), not an object.
So treat it as an array and it should work:
$obj['NetTerms'] = $obj['NetTerms'] . $addon;
Related
I have a class named orders and I want to see all the orders that were made this week; for that, I have the following:
$sevenDaysAgo = date('Y-m-d\TH:i:s.u', strtotime('-7 days'));
$query = new ParseQuery("orders");
$query->greaterThanOrEqualTo("createdAt", $sevenDaysAgo);
$count = $query->count();
// The count request succeeded. Show the count
echo "Only " . $count . " Results.";
Knowing that I've already Data that can be shown, with the above code, I am getting 0 results.
Do I have something wrong?
I'm having an issue with converting an array to individual strings and outputting the strings. I'm trying to store each converted string into it's own variable and output each variable to the string using an echo statement. I have already set up a mysqli connection. $connection is the mysqli connection. I've already tried using serialize() and implode() functions. There are four database colums (id, name, email, phonenumber). I commented out some code so you can see what I've been trying. I still can't seem to figure it out. I've included my code below. Thanks.
function get_random_info($connection)
{
$temp = mysqli_query($connection,"SELECT RAND() * FROM distributors LIMIT 1");
$info = $temp;
return $info;
}
//$random_distributor = serialize(get_random_info($con));
//$random_distributor = implode(get_random_info($con));
$random_distributor = $info;
//TEST WITH print_r
print_r ($random_distributor);
//$random_id = $random_distributor[0];
//$random_name = $random_distributor[1];
//$random_email = $random_distributor[2];
//$random_phonenumber = $random_distributor[3];
// TEST OUT INFORMATION. DISPLAY TO SCREEN.
echo "Random name is" . $random_name. "His id in the database is" . $random_id . ".
His email address is {$random_email} and his phone number is {$random_phonenumber}";
these are all commented out so it couldn't possibly work.
//$random_id = $random_distributor[0];
//$random_name = $random_distributor[1];
//$random_email = $random_distributor[2];
//$random_phonenumber = $random_distributor[3];
and you have a function that you never call
get_random_info
I figured it out with some help from you guys. Thanks for pointing me in the right direction. Below is the solution.
<?php
function get_random_info($con)
{
$random_distributor = mysqli_query($con, "SELECT * FROM distributors ORDER BY RAND() LIMIT 1");
return $random_distributor;
}
$row = mysqli_fetch_array(get_random_info($con),MYSQLI_ASSOC);
// TEST OUT INFORMATION. DISPLAY TO SCREEN.
echo "Random name is ". $row["name"] . ". His id in the database is ". $row["id"]. ". His email address is ". $row["email"]. " and his phone number is ". $row["phonenumber"];
mysqli_close($con);
?>
Alright,
I've got a multiple select dropdown on a page called week-select, its selections get passed via ajax to my php page.
I can get the data just fine, but when the query runs it doesn't complete appropriately.
I've got this:
//Deal with Week Array
$weekFilter = $_GET['week']; /*This is fine, if it's 1 week the query works great (weeks are numbered 12-15), but if it is 2 weeks the result is formatted like this 12-13 or 13-14-15 or whichever weeks are selected*/
$weekFilter = str_replace("-",",",$weekFilter); /*This works to make it a comma separated list*/
.../*I deal with other variables here, they work fine*/
if ($weekFilter) {
$sql[] = " WK IN ( ? ) ";
$sqlarr[] = $weekFilter;
}
$query = "SELECT * FROM $tableName";
if (!empty($sql)) {
$query .= ' WHERE ' . implode(' AND ', $sql);
}
$stmt = $DBH->prepare($query);
$stmt->execute($sqlarr);
$finalarray = array();
$count = $stmt->rowCount();
$finalarray['count'] = $count;
if ($count > 0) { //Check to make sure there are results
while ($result = $stmt->fetchAll()) { //If there are results - go through each one and add it to the json
$finalarray['rowdata'] = $result;
} //end While
}else if ($count == 0) { //if there are no results - set the json object to null
$emptyResult = array();
$emptyResult = "null";
$finalarray['rowdata'] = $emptyResult;
} //end if no results
If I just select one week it works great and displays the appropriate data.
If I select multiple options (say weeks 12, 14 and 15) it runs the query but only displays week 12.
When I manually input the query in SQL, how I imagine this query is getting entered - it runs and displays the appropriate data. So if I put SELECT * FROM mytablename WHERE WK IN ( 12, 14, 15 ) it gets exactly what I want.
I can't figure out why my query isn't executing properly here.
Any ideas?
**EDIT: I make the array from the multiple selections a string using javascript on the front end before it is passed to the backend.
Your resulting query with values probably looks like this with a single value in IN:
… WK IN ("12,14,15") …
Either use one placeholder for each atomic value:
if ($weekFilter) {
$values = explode(",", $weekFilter);
$sql[] = " WK IN ( " . implode(",", array_fill(0, count($values), "?")) . " ) ";
$sqlarr = array_merge($sqlarr, $values);
}
Or use FIND_IN_SET instead of IN:
$sql[] = " FIND_IN_SET(WK, ?) ";
I don't think you can bind an array to a singular ? placeholder. Usually you have to put in as many ? values as there are elements in your array.
If your HTML is correct and your week select has name="week[]", then you will get an array back with $_GET['week'];, otherwise without the [] it will only give you 1 value. Then, you're doing a string replace, but it's not a string. Instead, try this:
$weekFilter = implode(',', $_GET['week']);
I have this query here which I use in order to get the sum of a certain column, however I keep getting the error Undefined index.
$sql_shuma="SELECT SUM(vlera) AS shuma "
."FROM servis_pjeset_perdorura "
."WHERE random = $random";
$resultshuma = odbc_exec($connection, $sql_shuma) or die(odbc_error());
while( $rowshuma = odbc_fetch_array($resultshuma) ) {
echo $total1 = $rowshuma['shuma'];
}
?>
What am I doing wrong here? Maybe it's the $total1 value, I don't know how to save the result.
Thanks
Is $random an integer? If not, you must use ' ' to delimit the string, like this:
$sql_shuma="SELECT SUM(vlera) AS shuma FROM servis_pjeset_perdorura WHERE random = '$random'";
I am almost certain that this is a boneheaded question with a very simple answer, but I've been knocking my brain against the desk for the last 30 minutes or so and figured it was time to ask for help.
I need to fetch the current highest existing keyID in the database. Simple! So I did this:
$newIDQ = "SELECT MAX(mediaKey) FROM `imd_media`";
$newIDResult = $con->query($newIDQ);
$row = mysqli_fetch_array($newIDResult);
echo "Highest ID should be: " . $row['mediaKey'];
But it never spits anything out in $row['mediaKey']. It's been awhile since I used mySQL for anything and this is my first tussle with mysqli, so I'm sure I'm just looking right past the answer or misunderstanding something.
$row[0] will do it I believe.
Always debug your code. Say, for your current problem print_r($row); can help
Try this:
$newIDQ = "SELECT MAX(mediaKey) AS mediaKey FROM `imd_media`"; // rename the result col
$newIDResult = $con->query($newIDQ);
$row = mysqli_fetch_array($newIDResult);
echo "Highest ID should be: " . $row['mediaKey'];
or this:
$newIDQ = "SELECT MAX(mediaKey) FROM `imd_media`";
$newIDResult = $con->query($newIDQ);
$row = mysqli_fetch_array($newIDResult);
echo "Highest ID should be: " . $row['MAX(mediaKey)']; // your probable current result
$newIDQ = "SELECT MAX(mediaKey) FROM 'imd_media'";
$newIDResult = $con->query($newIDQ);
$row = $newIDResult ->fetch_array(MYSQLI_ASSOC);
echo "Highest ID should be: " . $row['mediaKey'];`