Getting error when pages are being requested - php

I had someone build out a custom CMS for a small site and now I can't get a hold of the developer. I get this error when trying to view my site:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'order by sortorder' at line 1:
select path, title
from pages
where parent_id =
order by sortorder
After some help I was able to I think pin point where the problem is:
public function getChildLinks()
{
$list = array();
$WHERE_path = substr($_SERVER['REQUEST_URI'], 1);
$WHERE_path = strlen($WHERE_path) ? '= "'.$WHERE_path.'"' : 'IS NULL';
$parentPageRowset = Axl_Db::query('SELECT id, parent_id FROM '.$this->_name.' WHERE path '.$WHERE_path);
$parent_id = $parentPageRowset[0]['parent_id'] ? $parentPageRowset[0]['parent_id'] : $parentPageRowset[0]['id'];
$listRowset = new Axl_Db_Rowset('SELECT path, title FROM '.$this->_name.' WHERE parent_id = '.$parent_id.' ORDER BY sortorder');
while($listRowset->next())
{
$list['/'.$listRowset->path] = $listRowset->title;
}
return $list;
}
This is the function causing the problem.

The issue is with your WHERE clause; it has parent_id = but nothing following. You need to provide a value into the query.

More than likely the CMS needs a GET var in the url in order for the mysql statement to work.

Well after getting great feed back from Adam I was able to really pin point what file I needed to fix and or look into a little deeper.
It turns out that I needed to quote out the parent_id variable
like so:
$listRowset = new Axl_Db_Rowset('select path, title from '.$this->_name.' where parent_id = "'.$parent_id.'" order by sortorder');
Thanks Adam for your help

Related

How to get parameters from a URL and use them in a MySQL query in PHP

So, I will have a page where a user lands with a /?ref=123 type ending on the URL.
Then, what I want to achieve, is to use that ref 123 in a MySQL query, like this:
SELECT foo FROM table WHERE table.ref = 123
The issue I have hit is that if i use $_GET to get the variable, it breaks my SQL query (obviously!)
SELECT foo FROM table WHERE table.ref = $_GET['ref']; falls over because $_GET is not a MySQL function.
Trouble is, I want to dynamically create page content based on the ref value but I can't figure how.
Any help gratefully accepted.
Timezone GMT+13 so replies will potentially be slow :)
**********EDIT**********
As I may not have given enough info in the OP, here's the code I'm struggling with:
<?php
global $wpdb;
header('Content-Type: text/html; charset=utf-8');
include "../../../wp-config.php";
$get_donation_amount = "select SUM(amount) AS total_donations from SaveContactForm7_1 where ref = 123 ";
$get_donation_amount_result = $wpdb->get_results($get_donation_amount);
$total_donation = isset($get_donation_amount_result[0]->total_donations) && $get_donation_amount_result[0]->total_donations !="" ? $get_donation_amount_result[0]->total_donations :"0" ;
?>
What I need to do is add a call to the URL for the value of ref and add it where shown with the SQL querycode. Then a particular donor who knows his 'ref' value will see results relevant to him alone.
Using PHP7 this could look something like this
$ref = $_GET['ref'] ?? null;
$sth = $dbh->prepare('SELECT foo FROM table WHERE table.ref = ?');
$sth->execute([$ref]);
$orders = $sth->fetchAll();
You should probably have some way of handling errors (ref is not set)
Note that the last variable (result of the query) is called orders. I didn't know what the result set you expected would be, but it was just to illustrate that it makes sense to call it something spesific (what it actually represents), instead of "rows", "result" or similar.
Note that PHP 7 introduces the so called null coalescing operator which simplifies isset statements
PHP7
$ref = $_GET['ref'] ?? null;
PHP < 7
$ref = isset($_GET['ref']) ? $_GET['ref']: null;

Wordpress: 500 Internal Server Error, probable issue using $wpdb

I'm using Wordpress and I'm performing a query which gives me back this error:
Failed to load resource: the server responded with a status of 500
(Internal Server Error)
My query looks like:
global $wpdb;
$session_uid = isset($_POST["session_uid"]) ? trim(strip_tags($_POST["session_uid"])) : "";
$the_data = isset($_POST["the_data"]) ? trim(strip_tags($_POST["the_data"])) : "";
$ss = "select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id";
$check_last_conversation = $wpdb->get_results($ss);
$response = print $check_last_conversation;
I'm probably missing or misunderstanding something but if I comment out $check_last_conversation and I print something like "Hello", the error goes away.
This is ok:
global $wpdb;
$session_uid = isset($_POST["session_uid"]) ? trim(strip_tags($_POST["session_uid"])) : "";
$the_data = isset($_POST["the_data"]) ? trim(strip_tags($_POST["the_data"])) : "";
//$ss = "select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id";
//$check_last_conversation = $wpdb->get_results($ss);
$response = print 'hello';
So I suppose there is some problems on how I've written my query.
$ss = "select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id";
Said that, I can't see the error.
My apache_error.log and mysql_error_log.err don't report anything
about.
My tables are empty at now but should they print nothing than produce that error.
Can you please suggest something?
EDIT
I see this error in my console
MySQL table empty
My Wordpress Debug is active like:
My debug.log file (wp-content) is not showing any error in my code.
I've discovered that there is a fatal error in the same file of my query:
PHP Fatal error: Call to undefined function get_bloginfo()
I could check it trough the server php error log. Working on MAMP you can find it here:
MAMP/logs/php_error.log
In my case, Wordpress didn't report it in wp-content/debug.log. So you know. It takes me to the conclusion that my file.php doesn't recognise wordpress hooks and could happen for $wpdb too, throughout my query.
As OP mentioned that he is working on external wordpress script and is not able to access his wordpress functionality. To ensure that you retain your wordpress functionality on your external files, please put the following code, at the start of your external script page.
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
The above lines, will include all the wordpress functionality for your script. $path variable stores the path of your wordpress installation folder.
During the development always turn on the error reporting to be aware of warnings notice or fatal errors which can occur very easily if you forget something or miss place something. So better to be aware or errors and turn the error reporting on to see the errors and when in production do disable the error reporting.
go into wp-config.php file and search for : define('WP_DEBUG',false); and change it to define('WP_DEBUG',true);
Even if your query is okay , you will still result to an error , which you will be getting due to incorrect printing of an array:
function get_results of $wpdb , is an function that will return an array as result of multi rows , and for dumping it use :
print_r($check_last_conversation);
or
var_dump($check_last_conversation);
print 'hello; works because it is a string , and $check_last_conversation is an array. Don't wrap the print_r or var_dump or print or echo inside an variable. as they are supposed to print the data inside from variables.
so in case you have more errors , you can look at the errors , by turning the error reporting on.
Sort of going on from Ajit's answer. I was also having issues similar to this that was caused by character collation which was fixed by adding one extra line.
At the top of any php external file that used wpdb I was using the following:
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
You can then use the $charset_collate in your sql statement, e.g.
$sql = "CREATE TABLE `{$wpdb->base_prefix}my_nice_table` (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
) $charset_collate;";
Hope that helps!

Proper Query in RETS

I need a little assistance regarding to RETS. I have not worked in it before. I am stuck at a point.
Here is the code
$rets_modtimestamp_field = "LastTr_260";
$previous_start_time = "2013-01-01T00:00:00";
$listing_status = "Status_383";
$listing_price = "ListPr_276";
The original query that I got was
$query = "({$rets_modtimestamp_field}={$previous_start_time}+)";
I had to update the query to add listing status and listing price. I searched around over the internet and updated the query to this.
$query = "(ListPrice=ListPr_276),(ModificationTimestamp=LastTr_260),(ListingStatus=Status_383),(".$previous_start_time."+)";
This is where the query is being used..
$search = $rets->SearchQuery("Property", $class, $query, array('Limit' => 1000));
Any ideas why the query is returning no results? I feel there is something in reference to start time...I have no idea about it...
Any help would be highly appreciated.
Thanks and Cheers
Ahmad
The last condition in your query is missing the name in the name=value pair. So your query would translate to:
$query = "(ListPrice=ListPr_276),(ModificationTimestamp=LastTr_260),(ListingStatus=Status_383),(2013-01-01T00:00:00+)";
With the name-value pair:
$query = "(ListPrice=ListPr_276),(ModificationTimestamp=LastTr_260),(ListingStatus=Status_383),({$rets_modtimestamp_field}={$previous_start_time}+)";
These look like the type of RETS field names that come from Interealty - one of the major RETS vendors. There are five variations of RETS field names - the "SystemName", "StandardName", "LongName", "ShortName", and "DBName." You appear to be querying on the DBName, which usually is not queryable. Try the SystemName, which should always be queryable. Interealty uses numeric SystemNames, so your list price field, ListPr_176, would very likely have the SystemName "176". The part of the query for the price would then look more like "(176=0+)". The entire query code should probably look more like this:
$rets_modtimestamp_field = "260";
$previous_start_time = "2013-01-01T00:00:00";
$listing_status = "383";
$listing_price = "276";
$query = "(".$listing_price."=0+),(".$listing_status."=|A),(".$rets_modtimestamp_field."=".$previous_start_time."+)";
(I added a presumed lookup value for Listing Status of active)

how to write OSClass custom query to fetch result from DB

I m new to OSClass(OpenSource Classifieds).
i know its similar to wordpress., but i'm not sure how to write my own query like how i do in wordpress.
The right side bar will have cities/regions as links to display advertisements from that city/region.
The City/Region name is passed through the URL, and as per requirement i need to change the URL as virtual subdomain.
For example if the user click city1, then the URL should be http://city1.domainname.com/ and results asusual.
I used htaccess to change the virtual subdomain redirection but stuck with city and region.
to determine whether its a city or region i need to do a check with the database, for that i need write a custom query to match the value with the table..
so that i can display the results regarding the city/region.
Any ideas for this problem i'm facing..
Thanks.
I suggest you take a look to the models inside ./oc-includes/osclass/models/ to search regions and cities and other common queries.
Here is how I did in one of my plugins:
$conn = DBConnectionClass::newInstance();
$data = $conn->getOsclassDb();
$comm = new DBCommandClass($data);
$db_prefix = DB_TABLE_PREFIX;
$query = "SELECT * FROM `{$db_prefix}t_item` WHERE b_active=1 AND b_enabled=1";
$result = $comm->query($query);
if ($result) {
$items = $result->result();
foreach ($items as $item) {
var_dump($item);
}
}

php for loop query in magento

Hello all i am new but got so many helps from here. Thanks to all the people who helped.
Now i am facing a problem in magento e-commerce for making a loop query of top 10 point holders. Well i have made a separate database which contain the points .
my database is table : magentodatabase_points
Now here is total 3 colums -
1. ID
2. User_id
3. points
well now, here i would like to make a loop of top 10 members contain the highest points i have tried but failed so can anybody help me on that.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$table = $resource->getTableName('database_points');
$query = 'SELECT user_id FROM ' . $table . ' ORDER BY points DESC LIMIT 10';
$results = $readConnection->fetchAll($query);
well but after that i have tried with php for loop but that is not supporting for magento. So can anybody know what will be the loop code for magento.
Well, one more help would be greatful for me how do i get magento customer names from the using the top results of user_id because i will get only the results not the names.
Anyways that is not that important because i hope i will solve it by my own.
So, please if anyone can help me on the loop query of top 10 point holders.
Thanks in advace
and sorry for my bad english
If your table name and fields exist then you can just add this below your code:
foreach ($results as $result) {
$user_id = $result['user_id'];
echo $user_id; // or do whatever you wanted to with the variable
}
You should consider using Magento's Models to do this instead. There's an excellent tutorial series from Alan Storm on MagentoCommerce's website: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
For the customer's names you can try this in your loop:
foreach ($results as $result) {
$user_id = $result['user_id'];
$customer = Mage::getModel('customer/customer')->load($user_id);
print_r($customer);
}
May be because your field name has uppercase for u of user_id correctd "User_id" and you are using lowercase in your query.
else your method is fine to work with magento
for gettting customer, if User_id u get from this result is a customer id then $customer_data = Mage::getModel('customer/customer')->load(User_Id); would give u all detail of customer. you can fetch name from this using $customer_data->getName() method

Categories