I am trying to run this query from a drop down menu, the category and the destination data are working fine, just not the subcategory, it's probably an issue with my Html or Javasscript but i wanted to get the opinion of an expert with MYSQL if poss? I am quite new to PHP and MYSQL, but learning fast, Not sure my HTML will post here so i will just ask if someone can confirm that my query should work ok. Thanks guys.
$category=$_POST['Category'];
$subcategory=$_POST['Subcategory'];
$destination=$_POST['Destination'];
$result = mysql_query("SELECT * FROM travel WHERE Category='$category' AND Subcategory='$subcategory' AND Destination='$destination'")
or die(mysql_error());
$row = mysql_fetch_array( $result ) ;
Not sure if it's a culprit in your case, but you have to escape data coming from a user before passing it to the query :
$category=mysql_real_escape_string($_POST['Category']);
$subcategory=mysql_real_escape_string($_POST['Subcategory']);
$destination=mysql_real_escape_string($_POST['Destination']);
....
Related
I am trying to select some rows from a table that I have in my database but I keep obtaining null $result. I think the problem is with my sql code.
The query is the following:
$result = mysqli_query($this->conexion, $sql);
The conexion is working fine I have checked so the problem is with $sql. My vareiable $sql is:
"SELECT * FROM `invent` WHERE `id_system` IN (500,504,502,498,499)"
My table is invent and I have a row named id_system, I have checked all the names and are correctly spelt and the column id_system contains integers and all ones of the query exist in the table.
I keep getting:
$result={"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
I don't know why is it not working, it might be because of the spelling in $sql.
Could someone help me?
Thanks in advance
I don't know if this will help, but this code might work for you:
SELECT 'id_system'
FROM 'invent'
WHERE 'id_system'=500
OR 'id_system'=504
And then you could add the rest of the values you need
I am new in codeigniter and just stacked in a query to solve a report for an emergency project. Please help me Codeigniter's Experts.
I have a large database table and wants to show only Odd/Even Data rows from that table which will filtered by a table Field named is "sale_id". I tried it in PHPMyadmin in raw coding and it's worked for me. But can not apply in Codeigniter.
SELECT * FROM ospos_pak_sub_cat WHERE id %2 =0;
Worked for me in raw PHP Coding. How can I use it in Codeigniter. I used a Where Condition already on that query and now want to add the new query.
Existing Where condition is given below, which is working fine.
$this->db->where('sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].'"');
It is working and I tried the code below to get the solution which is not working and getting error.
$this->db->where('sale_id %2'=> 0);
Getting error with this line. says--
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
Please help me to get the solutions. Thanks in Advance.
In your Model, just write the query like that:
$this->db->select('*');
$this->db->from('ospos_pak_sub_cat');
$this->db->where('sale_id %2=', 0);
$query_result = $this->db->get();
$result = $query_result->result();
You missed the '=' in your code. Hope, it will work.
in SQL the % character is a wildcard rather than a modulo which would explain your error. you can use the MOD function instead http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod
so the resulting code would be :
$this->db->where('MOD(sale_id ,2) => 0');
I am not sure but i think it will work try this line
$this->db->where('sale_id %2',0);
As you mentioned, you can add the new condition in existing where condition as like below
$this->db->where('(sale_date BETWEEN "'. $inputs['start_date']. '" and "'. $inputs['end_date'].')" and ((sale_id % 2) = 0)');
I know that we can get sql query from magento model collection using this,
getSelect();
But I can get query from only the model collections, not worked in others or May be I dont know how to use it.
Here I want to know what query is running behind this,
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("28");
}
If I use this,
echo $productModel->getSelect(); exit;
I'm just get the one part the query only, like,
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Update:
This is my full code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
$color_label = $attr->getSource()->getOptionText("28");
}
$productModel->printlogquery(true);exit;
Please help me guys,
Your order condition is not visible in the query. The reason your order isn't showing is because the orders are added to the query during the load() method.
See Varien_Data_Collection_Db::load()
Try calling load(true) to see the complete SQL containing the order by clause.
$productModel->load(true);
$productModel->printLogQuery(true);
Hope it helps.
If you want to see what is the exact query, then you can get this by using:
$productModel->printlogquery(true);exit;
Use this code after you have loaded your model's object and applied all conditions.
I hope this will help you.
Magento collecting data with lot of internal queries - models and lot of checks, and may be more than 1 table. So it is not possible to get the query like what I'm looking for.
I have integrated PHPbb to my website and I am using the phpbb connection to read data from an additional table I created on the database. below is the read code to query the databse:
$sql = 'SELECT tetsin, prod_desc_short, prod_price_old, prod_price_new, prod_img
FROM ' . tet_product;
$result = $db->sql_query($sql);
$data = array();
while ($row = $db->sql_fetchrow($result))
{
$data[] = $row;
}
What I am having trouble with is finding the function to add records to the databse, can someone direct me on what technology I may be using and unaware? I saw something about the Data Absraction layer and was a little more than sketchy on if there are some "variants" on functions or something like that, anyway I know the quer function above is "$db->sql_query" so any help in the insert query would be greatly appriciated. thanks!
See the official wiki, it has guides to most things you will need https://wiki.phpbb.com/Queries_in_phpBB3
Forgive me for re-wording and re-asking this question, but the answers I received a couple weeks back didn't help much...
Basically, I'm looking to somehow combine multiple database queries in Wordpress to retrieve user IDs by searching for term in the 'usermeta' table, but only entries that have a certain 'meta_value'
I'm trying to combine:
$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_name'
FROM $wpdb->usermeta
WHERE meta_key = 'business_name'");
AND:
$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_description'
FROM $wpdb->usermeta
WHERE meta_key = 'business_description'");
To essentially have this:
$users = $wpdb->get_results("SELECT user_id, business_name, business_description
FROM
WHERE
business_name LIKE '%{$keyword}%' OR
business_description LIKE '%{$keyword}%'");
I've looked into INNER JOINs and subqueries, but cannot seem to find a good solution. I realize that I can get away with multiple queries, but this would be searching through possibly thousands of entries, so I'd like to optimize it as much as possible.
Hi #John:
If I understand your question correct (since you gave a technical example of what you were attempting but not a description of what end result you were trying to accomplish I'm not sure) it seems you are doing a user search? If yes, below is what I think you need.
<?php
// Load WordPress, but only for standalone example use
include '../wp-load.php';
// Make sure the database object is available
global $wpdb;
// Get value entered by the user
$keyword = $_GET['keyword'];
// This would be used in your code; the percent would confuse prepare() below
$keyword = "%{$keyword}%";
// Join each meta field as a separate join and reference the meta_key in the join
// prepare() below replaces "%s" with a quoted string value
$sql =<<<SQL
SELECT
users.user_nicename AS user_name,
bizname.meta_value AS business_name,
bizdesc.meta_value AS business_description
FROM
{$wpdb->users} AS users
INNER JOIN {$wpdb->usermeta} AS bizname
ON bizname.user_id=users.ID
AND bizname.meta_key='business_name'
INNER JOIN {$wpdb->usermeta} AS bizdesc
ON bizdesc.user_id=users.ID
AND bizdesc.meta_key='business_description'
WHERE 1=0
OR bizname.meta_value LIKE %s
OR bizdesc.meta_value LIKE %s
SQL;
// User prepare() to avoid SQL injection hacks
$sql = $wpdb->prepare($sql,$keyword,$keyword);
// Finally, get yer results!
$users = $wpdb->get_results($sql);
echo "<ul>";
foreach($users as $user) {
echo "<li>User= {$user->user_name}, Business= {$user->business_name}:{$user->business_description}</li>";
}
echo "<ul>";
The above is a complete working example you can copy to a file in the root of your website and call it something like /test.php allowing you to see it work by using a URL like this:
http://example.com/test.php?keyword=Accounting
Of course, this may be less performant at times than using multiple queries because of the query caching systems built-in to WordPress but it's impossible to tell without some benchmarking.
Hope this helps.
-Mike
P.S. By the way, I'm assuming you were not aware of it but since your prior question evidently hadn't gotten much WordPress love nor had this one I'll mention the WordPress Answers website which is a sister site to StackOverflow. Lots of WordPress enthusiasts are on hand over there to answer WordPress-specific questions.My experience with StackOverflow is they have some of the best developers on the web but few here have specific experience developing with WordPress so you end up with people here trying to answer MySQL questions without knowing the WordPress database schema and without knowing WordPress-specific best practices. Ask over at WordPress Answers and I think you'll an improved quality of answers to your WordPress-specific questions.
Can you post some details on the error messages you are receiving? The SQL here looks ok, but it's impossible to tell what the problem is without seeing the error messages.
For example, are you sure the $keyword field is being populated? How many results to return? Have you tried it without the WHERE statement or just a single OR to troubleshoot? Also, have you tried running this SQL directly on your server with some test keywords?
Give us more of an idea on what you have tried to fix the problem and we'll be able to assist further.
Kind Regards
Luke Peterson