SELECT query inside PHP class - php

I am trying to run a simple SELECT query inside of a PHP class, using the GET variable.
$this->Token = $_GET['Token'] ?? null;
function getRows(){
$query = $this->db->query("SELECT * FROM store_product_images WHERE token = ".$this->Token." ORDER BY display_order ASC");
When I run this, nothing shows, if I remove the WHERE it works fine

you missed the quote
$query = $this->db->query("SELECT * FROM store_product_images WHERE `token`= '".$this->Token."' ORDER BY display_order ASC");

Related

I need to match a String in MySql query in Laravel

I need to use a query which perfectly works in MySql. and query is
SELECT * FROM c2cxdb.jobs where job_title like '%Java%' and job_policy = 'Public' and job_status = 1;
where I need to replace java inside %% with a dynamic value that I pass to the method.
function AllJobs($job_title){
$allJobs = DB:: select('select * FROM c2cxdb.jobs where job_title like '%$job_title%' and job_policy = "Public" and job_status = 1');
dd($allJobs);
}
But It shows Division by zero exception when I run it.How to replace the value in my query with the value I Pass.I need to get the $job_title value into query.
function AllJobs($job_title){
$allJobs = DB:: select('select * FROM c2cxdb.jobs where job_title like "%'.$job_title.'%" and job_policy = "Public" and job_status = 1');
dd($allJobs);
}
function AllJobs($job_title){
$job_title="%".$job_title."%";
$allJobs = DB:: select('select * FROM c2cxdb.jobs where job_title like "'.$job_title.'" and job_policy = "Public" and job_status = 1');
dd($allJobs);
}

$wpdb->wp_users returning empty value for

I have a code Example:
function custom_func(){
global $wpdb;
$wpdb->flush(); //tried with and without this line
$getTest = 'SELECT * FROM $wpdb->wp_users LIMIT 1';
$arrayReturned = $wpdb->get_results($wpdb->prepare($getTest));
}
From what I've read I thought that $wpdb->wp_users is meant to have returned the database name and table name like so dbName.tableName; but it just returns an empty value.
I've tried:
$getTest = 'SELECT * FROM $wpdb->wp_users LIMIT 1';
which shows as the following to wordpress:
SELECT * FROM $wpdb->wp_users LIMIT 1
and
$getTest = 'SELECT * FROM '.$wpdb->wp_users.' LIMIT 1';
which shows as the following to wordpress:
SELECT * FROM LIMIT 1
I can't fathom why this isn't working since this is all based on literature from the wordpress codex, any thoughts?
You don't need to prefix the tables. As $wpdb->table will do it for you. Also you need to use double quotes " instead of single ' because you are using $wpdb variable in your query string.
'SELECT * FROM $wpdb->wp_users LIMIT 1';
^---------------------^^^-------------^---
Use it without table prefix and with double quotes ".
"SELECT * FROM $wpdb->users LIMIT 1";
Also you don't need to use prepared statement because as there are no user input.
Your code should look like this:
function custom_func() {
global $wpdb;
$getTest = "SELECT * FROM $wpdb->users LIMIT 1";
$arrayReturned = $wpdb->get_results($getTest);
var_dump($arrayReturned); // see results
}

Edit php string

I have a string that makes a function from my order status. I wan´t to have it to trigger with 2 order status´
Is that posible?
$query = "SELECT * FROM #__redshop_orders where order_payment_status ='Paid' and order_status = 'RD2'";
So insted of just trigger with status RD2 it should trigger with RD1+RD2
Plus - I wan´t to remove the order_payment_status function, so it only triggers for order status.
How will the string look like after editting?
Thank you.
It would be something like this:
$query = "SELECT * FROM #__redshop_orders WHERE order_status = 'RD1' OR order_status = 'RD2'";
You should try to learn MySQL, a simple google search would give you plenty of example and tutorials.
EDIT:
I made some tests since I posted my answer, and the benchmarks show that a much faster solution would be:
$query = "SELECT * FROM #__redshop_orders WHERE order_status IN ('RD1', 'RD2')";
$query = "SELECT * FROM #__redshop_orders WHERE order_status IN ('RD1', 'RD2')";
$query = "SELECT * FROM #__redshop_orders where order_status IN('RD1','RD2')";
//You want this

mysql FOUND_ROWS() return wrong value

I'm trying to use FOUND_ROWS() in my query, but the function returns wrong values sometimes.
SELECT SQL_CALC_FOUND_ROWS adminslog.*, admins.fullName FROM adminslog
JOIN admins ON admins.id=adminslog.userId ORDER BY date DESC LIMIT 0,12
In this query, I get the right value for some, but in others the limit has the wrong value.
LIMIT 0,12 164rows right
LIMIT 12,12 164rows right
LIMIT 36,12 164rows right
LIMIT 48,12 164rows right
LIMIT 50,12 60rows wrong
LIMIT 62,12 60rows wrong
Here is my class construct:
class list_table
{
public $number,$page_number,$all_rec,$table_rows,$query_str,$query,$fetch,$table,$db,$fields,$i=0,$page_n,$onclick;
function __construct($query_str,$fields,$page_n,$onclick,$page_number,$number,$db)
{
$this->fields = $fields; $this->page_number = (((int)$page_number<1)?1:(int)$page_number); $this->number = (int)$number; $this->db = $db;
$this->i = $this->page_number*$this->number-$this->number; $this->page_n = $page_n; $this->onclick = $onclick;
$this->query_str = substr(trim($query_str),0,7)."SQL_CALC_FOUND_ROWS ".substr(trim($query_str),7)." LIMIT ".(($this->page_number*$this->number)-$this->number).",".$this->number;
$this->query = $this->db->query($this->query_str);
$this->table_rows = $this->db->query("SELECT FOUND_ROWS()")->fetchColumn();
$this->all_rec = $this->query->rowCount();
$this->fetch = $this->query->fetch();
//$this->table_rows = $this->table_rows->fetch();
//$this->table_rows = $this->table_rows['cnt'];
print $this->table_rows;
}
other functions...
}
A mysql bug can be responsible for this issue, depending on which version you use:
http://bugs.mysql.com/bug.php?id=1468
You can workaround it by using a GROUP BY clause in your query.
question: you think your first FOUND_ROWS() is right every time in every query?
check that,if that is true you can run this code only in first query and save it to session.
if($this->page_number==1) $_SESSION['cr'] = $this->table_rows = $this->db->query("SELECT FOUND_ROWS()")->fetchColumn();
in this way you have not to check row counts every time.

PHP Retrieve results

I am having a small trouble retrieving results that I hope someone can help me with.
I have a field called $incategory which is a comma based string, and what I want to do is explode the into an array that can be used to retrieve results as below (Hope that makes sense):
<?php
$showlist = $row_listelements['incategory'];
// ** e.g. $incategory = 1,3,5,
// ** What I want to do is look at table 'category'
// ** and retrieve results with an 'id' of either 1, 3 or 5
// ** Display Results
mysql_select_db($database_db, $db);
$query_display = "SELECT * FROM category WHERE id = ".$showlist." ORDER BY name ASC";
$display = mysql_query($query_display, $db) or die(mysql_error());
$row_display = mysql_fetch_assoc($display);
$totalRows_display = mysql_num_rows($display);
?>
You can use the IN keyword of SQL directly like this.
query_display = "SELECT * FROM category WHERE id IN (".$showlist.") ORDER BY name ASC";
Another tip would be to stop using MYSQL_QUERY as it is deprecated in PHP 5.3
Edit: If $showlist = '1,3,5,' you will need to remove the last comma from the string to make it useable in the query. Just use this query then
query_display = "SELECT * FROM category WHERE id IN ('".str_replace(",", "','", substr($showlist, -1))."') ORDER BY name ASC";
Use explode function and use , as delimiter.
refer here http://www.w3schools.com/php/func_string_explode.asp
Hope this helps.
First, you have explode the $incategory string into an array containing all of the category number. For example:
$incategory = explode(",", $incategory);
And then you just have to execute this query:
$query_display = "SELECT * FROM category WHERE id = "
. $incategory[$i] . " ORDER BY name ASC";
The $i should be defined beforehand (usually using loop).

Categories