im trying to get value from sql table with wordpress but its not working...
whats wrong??
function getPension($sum, $age, $percent, $gender, $type)
{
//call function from db
$promo= $wpdb->get_var( $wpdb->prepare( "SELECT promoter
FROM $wpdb->simulator1
WHERE age=$age
and precent=$percent
and gender=$gender
and type=$type", 0,0));
//the pension sum per month calc
return $sum/$promo;}
Related
I'm working on a function that compare two price.
I have a table called 'PRODUCT' that has 3 column: ID, REFERENCE, PRICE.
I get a price from a FORM that i will compare with the column PRICE. If the number that i get with the form, is different by PRICE from the table, i want get the REFERENCE. So i work in this way:
$pdo = new PDO ("mysql:host=$hostname;dbname=$dbname", $user, $pass);
$sql = "SELECT count(1) as NB
FROM ww_ps_product
WHERE reference = :reference AND SUBSTR(wholesale_price , 1, INSTR(wholesale_price ,'.')+2) != :price" ;
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':reference', **$referenza**, PDO::PARAM_STR);
$stmt->bindValue(':price', **$prezzoAggiornato**);
$stmt->execute();
$product = $stmt->fetch(PDO::FETCH_ASSOC);
The value that i get from the form : $referenza is FIN 260482300000 and the $prezzoAggiornato is 6.90 . Normalli, in my $product, i should have 0 but i get 1.
This data are store in DB with the same values:
When i go to compare with my function, i get NB = 1 but that's is impossible. If i check with the same values in phpMyAdmin i get NB = 0.
There is something wrong in my code?
I have a SQL statement where I want the WHERE part to be dynamically filled by the calling function. The function that calls the statement should be able to pass one or more parameters with 'OR'. Unfortunately, the statement always takes only the first entry.
$category = 'cat1' OR 'cat2' OR 'cat3';
$result = getEntries($category, $mysqli);
function getEntries($category, $mysqli){
$queryData = $mysqli->prepare("SELECT * FROM items WHERE category = ?");
$queryData->bind_param("s", $category);
$queryData->execute();
$queryData = $queryData->get_result();
return $queryData;
}
I only get results for "cat1"
Thanks for your help
You are using OR operator to assign a variable, which will assign one value at a time, if you want the number of categories dynamically then you can modify your code like below:
$categories = ['cat1','cat2','cat3'];
$result = getEntries($categories, $mysqli);
function getEntries($categories, $mysqli){
$plcs = implode(',',array_fill(0,count($categories)-1, "?");
$queryData = $mysqli->prepare("SELECT * FROM items WHERE category IN ({$plcs})");
$queryData->bind_param(str_repeat("s", count($categories)), ...$categories);
$queryData->execute();
return $queryData->get_result();
}
wrong assignment .
$category = 'cat1' OR 'cat2' OR 'cat3';
correct it . See the basics of bind param from below :
https://www.php.net/manual/en/mysqli-stmt.bind-param.php
And multiple where statment can be like this :
SELECT * FROM items WHERE category in ("cat1","cat2","cat3")
also you can use following when your database has extra space or want to match anything containing cat1,cat2,cat3:
SELECT * FROM items WHERE category like '%cat1%' or category like '%cat2%' or
category like '%cat3%'
In a php page, I have an array, similar to this:
$category = array(16, 22, 23);
Then I am doing a database query with a prepared statement. I would like to get all rows where the field category contains one of the values from that $category array and where price is lower than a value stored in the variable $price.
Among others I read the answers to this question and tried to use find_in_set() as described there (and at a lot of other places), but somehow I can't make it work within the prepared statement. I tried this:
/* database connection "$db" is established beforehand and works */
if($ps = $db->prepare("
SELECT id, product, category, price
FROM products
WHERE price <= ? and find_in_set(?, category)
ORDER BY id") {
$ps->bind_param("ds", $price, $category);
$ps->execute();
$ps->bind_result($id, $name, $cat, $pr);
while($ps->fetch()) {
/* ... echo the results ..... */
}
$ps->free_result();
$ps->close();
}
But I get an empty result.
If I try to use "dd" or "di" instead of "ds" in the bind_param() line, I do get results, but the wrong ones - I get all rows with category 1.
I also tried to use category IN ? instead of find_in_set(?, category), but that won't work either.
What can I do to make that work? Any help appreciated!
Two issues:
The list should be passed as second argument to find_in_set, so it should be:
find_in_set(category, ?)
That argument should be of type string (comma separated values). So first convert your array to such a string with implode:
$csv = implode(",", $category);
Code:
if($ps = $db->prepare("
SELECT id, product, category, price
FROM products
WHERE price <= ? and find_in_set(category, ?)
ORDER BY id") {
$csv = implode(",", $category);
$ps->bind_param("ds", $price, $csv);
$ps->execute();
$ps->bind_result($id, $name, $cat, $pr);
while($ps->fetch()) {
/* ... echo the results ..... */
}
$ps->free_result();
$ps->close();
}
If you want to find values where $category belongs to a certain set try using IN. Just a note you cannot pass an array into a string like you have above.
Also don't forget to convert your array to a CSV string using implode
$category = array(16, 22, 23);
$cat_str = implode(",",$category); //16,22,23
$ps = $db->prepare("
SELECT id, product, category, price
FROM products
WHERE price <= ? and category IN (?)
ORDER BY id") {
$ps->bind_param("ds", $price, $cat_str);
if i tried the same case , then i would probably have done it like this
SELECT id, product, category, price
FROM products
WHERE category = '16' || category = '22' || category = '23' and price < $price`
i am assuming your $category array variable is fixed , it's not dynamically appending value .
I answered only your query part . i'd hope you have php coding convention idea rather than putting the entire statement in the if statement .
if still has anything to ask about , please go ahead
I have the following lines of code to retrieve records from a database:
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname; charset=utf8;", $username, $password);
$sql = $dbh->prepare("SELECT * FROM usa WHERE code = :code AND window1 = :oldrepeat AND spare <> :americinn AND url IS NOT NULL ORDER BY user ASC");
$sql->execute(array(':code' => $code, ':oldrepeat' => $oldrepeat, ':americinn' =>$americinn));
/*** fetch the results ***/
$result = $sql->fetch();
Amongst other criteria, this query is supposed to return records that only have some content in the field called url, but this is not happening. It is returning records that also have an empty url field.
I assume that I am doing something fundamentally wrong but cannot see what it is.
Can anyone shed some light please?
Best wishes
Well, write your query in following way:
SELECT * FROM usa WHERE code = :code AND window1 = :oldrepeat AND spare <> :americinn AND !ISNULL(url) ORDER BY user ASC
ISNULL is an inbuilt MySQL function that checks if value of a column is null. By using ! (negation), you will get only required rows.
In case if a column has blank value, you can try url!=''.
hey im trying to make a shopping cart using php iv talked to other students in my class and they think for some reason there is something wrong with my getProductByTypeId query... it picks up everything except my ProductID from the database.
Below is my function...
function getProductsByTypeID($TypeID){
//pull in global connection
global $connection;
//execute query to select all product types
$query="select * from product where TypeID = ".$TypeID;
$result = mysql_query($query, $connection);
testQuery($result);
//return list of customers
return $result;
}