SQL - Can not SELECT data from a specific table - sql

$rs77 = $connector->query("SELECT * FROM content WHERE topic='1' AND date='$date' ORDER BY cpc DESC LIMIT 4");
while ($rw77 = $connector->fetchArray($rs77))
{
switch($rw77['type']) {
case "vid":
?>.... This is just a little part of my code.
It worked ok, and in one moment (didn't any kind of changes in db or in code).I tried to display mysql_error. But it doesnt show me nothing.
$your_query = "SELECT * FROM content WHERE topic='1' AND date='$date' ORDER BY cpc DESC LIMIT 4";
$value = mysql_query($your_query) or die("A MySQL error has occurred.<br />Your Query: " . $your_query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
What can be the problem? Thank you.
UPDATE: all other tables work nice. I can select data from them.
Here is my table:

Related

I'm developing small inventory model and I'm trying to send email if stock is low

I am working on a simple pdo mail function. As on stock is column name, when stock is less than 10 then mail should go automatically in pdo. I don't wish to use SMTP.
include_once('database-config.php');
$email="abc#gmail.com";
$query = "SELECT slno, itemname FROM item WHERE as on stock = 10";
foreach ($dbh->query($query) as $row) {
// Safe name for 70 char/line limit
$itemname = (strlen($row['itemname']) > 40) ? (substr($row['itemname'], 0, 10) . '...') : $row['itemname'];
// Prepare message data
$subject = 'Out of stock - ' . $row['itemname'];
$body = 'Product "' . $itemname . '" is out of stock.' . "\r\n";
$body .= 'Manage from http://localhost/oftest/login-system-in-php/guru-able/guru-able/default/adminhome.php?slno=' . $row['slno'] . "\r\n";
mail($email, $subject, $body);
you are only selecting items where the stock is exactly equal to 10
WHERE as on stock = 10
perhaps this will help you:
https://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html
also, see this regarding column names with spaces:
How to select a column name with a space in MySQL

How to do select statement after THEN of SELECT CASE in SQL query?

Is it possible to write query which has select statement after THEN of SELECT CASE in SQL query?
Example of logic
SELECT Id, CASE WHEN level=2 THEN
(select something here)
else 0 end as Grade FROM CLASS
You could declare the second select as a variable.
For example:
DECLARE #var as int
SET #var = (SELECT COUNT(*) FROM table)
SELECT
Id,
CASE WHEN level=2 THEN #var else 0 end as Grade
FROM CLASS
Based on the PHP Documentation you could run something along these lines:
Start by creating your database connection:
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
This if statement is checking for the table test if it does not exists then drop it OR create the table test with the field id which is an INT
if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
Then if the conditions are try you run your queries here; here they have three queries.
First - counts the total rows in the table (which will equal 0)
Second - add a value to the table
Third - count the total rows again, which this time will output "1"
// this is what you may be asking?
// they add to their sql query
$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";
Make sure that the queries have been built correctly
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
Then run the code
do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC)); // this will gather all records from the Database
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result()); // You can find the links for documentation for more_results() and next_result() below
?>
Documentation for more_results() and next_results()
Note the $sql as you can see they just create multiple queries that will run together if everything is true.
It will output this:
array(1) {
[0]=>
array(1) {
["_num"]=>
string(1) "0"
}
}
array(1) {
[0]=>
array(1) {
["_num"]=>
string(1) "1"
}
}
Please note, this is not my code, I got it from here

opencart 2.0.1.1 - Rewards points are not subtracting when order is placed

I have opencart 2.0.1.1 and I am facing a problem that let say a customer of my store has 500 rewards points and it place order for buying products by using 100 rewards points. The order get placed but when the same user again come to place order it still shows 500 rewards points to that customer until the admin of store change the status of that customer's previous order to complete.
I have found this one link having same issue as mine , but the solution he used is not solving my problem. Plus I have not found any solution on opencart forums as well. Any Help?
https://github.com/opencart/opencart/issues/3637
UPDATE :
I guess I need to modify the insert query in this confirm () function in this file catalog/model/total/reward.php . I am on it but any help would be appreciated!
public function confirm($order_info, $order_total) {
$this->load->language('total/reward');
$points = 0;
$start = strpos($order_total['title'], '(') + 1;
$end = strrpos($order_total['title'], ')');
if ($start && $end) {
$points = substr($order_total['title'], $start, $end - $start);
}
if ($points) {
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward SET customer_id = '" . (int)$order_info['customer_id'] . "', order_id = '" . (int)$order_info['order_id'] . "', description = '" . $this->db->escape(sprintf($this->language->get('text_order_id'), (int)$order_info['order_id'])) . "', points = '" . (float)-$points . "', date_added = NOW()");
}
}
The problem is solved by changing the default order status to Processing.
Before the default order status was Pending due to which the rewards points were not subtracted. So rewards points only subtracted when order status is not pending.
Problem solved!

opencart getcategories function goes into infinite loop

I have an opencart shop with around 1300 categories and i'm not able to insert a new category or edit the existing category. I have installed pagination function, but it speeds up only category view. i did some debugging and found that the below function goes into an infinite loop once i click insert or edit inside the admin panel. Can anyone help me how to fix this
public function getCategories($parent_id = 0) {
$category_data = $this->cache->get('category.' . (int)$this->config->get('config_language_id') . '.' . (int)$parent_id);
if (!$category_data) {
$category_data = array();
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY c.sort_order, cd.name ASC");
foreach ($query->rows as $result) {
$category_data[] = array(
'category_id' => $result['category_id'],
'name' => $this->getPath($result['category_id'], $this->config->get('config_language_id')),
'status' => $result['status'],
'sort_order' => $result['sort_order']
);
$category_data = array_merge($category_data, $this->getCategories($result['category_id']));
}
$this->cache->set('category.' . (int)$this->config->get('config_language_id') . '.' . (int)$parent_id, $category_data);
}
return $category_data;
}
First Solution:
Do you also have Category Descriptions in database? If so, these so many descriptions being loaded by MySQL may be making burden over server. Try optimize the Query, select only required columns, you may change query as under:
$query = $this->db->query("SELECT c.category_id, cd.name FROM ...
Second Solution
You can do one more debug trick which once solved my problem. Add an echo statement:
echo $result['name'];
$category_data = array_merge($category_data, $this->getCategories($result['category_id']));` `
This way you'll get to know the exact location of the category where process gets hanged. There may be some problem with that particular category only.
Third Solution
Try changing
'name' => $this->getPath($result['category_id'], $this->config->get('config_language_id')),
To:
'name' => $result['name'],
Hope this helps.
I have a current but not permanent solution that worked for me... I dont know whether it's the right way to do, but it works for me.I changed the query to this
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c ORDER BY c.parent_id,c.sort_order");
And commented off
//$category_data = array_merge($category_data, $this->getCategories($result['category_id']));
This currently works for me, but i don't know whether it's the correct way or not. Thanks for the quick help everyone.

getting data from 2 different table with JOIN sql. Codeigniter

I use codeigniter, and I need to get data from 2 different table. for now it returns data only from works_image table. how can I get data from both 2 table ?
thanks a lot!!
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "";
}
As long as you're doing a SELECT * (Why is this a bad idea?), you shouldn't need to specify tables with the call to select(). It will select all fields by default.
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
Should work fine.
Instead, what you really should be doing, is specifying exactly which fields you need:
$this->db->select('works_image.id, works_image.name, works_image.id_work, works.id, works.name'); // (or whichever fields you're interested in)
$this->db->from('works_image', 'works');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
This way you can be sure that (a) you're not pulling unnecessary data from your DB, and (b) your code won't break if/when you modify your DB schema.
This post should answer your question: http://www.whypad.com/posts/codeigniter-activerecord-join-tip/178/
In short, you need to rewrite your select statement from
$this->db->select('works_image.*', 'works.*');
to this:
$this->db->select('works_image.*, works.*');
Note that this was the first result on Google for 'CodeIgniter join'. Try Googling your questions first. You can often get yourself a faster answer :)
You should be to write below code
$this->db->select('works_image.*', 'works.*');
$this->db->from('works_image');
$this->db->join('works', 'works.id = works_image.id_work');
$result = $this->db->get();
foreach ($result->result() as $row) {
echo " # " . $row->id . " - " . $row->thumb . " - " . $row->wname . "
";
}
I think that you get your result