Zen Cart Logo
Forums / Code Collaboration / Error 500 when accessing db

Error 500 when accessing db

Views: 9,578

Results 1 to 3 of 3
07 May 2019, 04:50
#1
oopsibrickedit avatar

oopsibrickedit

Zen Follower

Join Date:
Mar 2012
Posts:
132
Plugin Contributions:
0

Error 500 when accessing db

ZC =1.5.0 PHP=5.6

Hi all,

I tried to connect to db and I got and error 500 with my custom code. First I thought the problem is on my custom code but then I tried an following example and still got the server error 500:

<?php
$theProductId = 25;
global $db;
$sql = "select zen_products_model from " . TABLE_PRODUCTS . " where products_id = :productID:";
$sql = $db->bindVars($sql, ':productID:', $theProductId, 'integer');
$result = $db->Execute($sql);

if ($result->RecordCount() > 0) {
  echo 'Model number = ' . $result->fields['products_model'];
} else {
  echo 'Sorry, no record found for product number ' . $theProductId;
}
?>

Any help would be greatly appreciated. There were no errors in zencart error.log

07 May 2019, 09:32
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Error 500 when accessing db

If this is in a file of its own, the reason no errors are in the zencart logs file is because the zencart code is not a part of the above "application".

To incorporate the zencart code, at the top of the file after the opening php statement add:

require 'includes/application_top.php';

Then as is, there will be a sql error thrown for a standard Zen Cart store because the field 'zen_product_model' doesn't exist. If it did, the retrieval of $result->fields['products_model'] would fail because the field is not in the query.

The field in a standard Zen Cart install is products_model.

09 May 2019, 00:37
#3
oopsibrickedit avatar

oopsibrickedit

Zen Follower

Join Date:
Mar 2012
Posts:
132
Plugin Contributions:
0

Re: Error 500 when accessing db

mc12345678:

If this is in a file of its own, the reason no errors are in the zencart logs file is because the zencart code is not a part of the above "application".

To incorporate the zencart code, at the top of the file after the opening php statement add:

require 'includes/application_top.php';

> 
> The field in a standard Zen Cart install is products_model.

Thank you again mc12345678. I added the ../require 'includes/application_top.php'; because custom search was in the subfolder and got an installation page but after I moved it to a shop folder it started working.

I am trying to make a custom part search and now it can fetch and show the model-number. However I would like to show also product image thumbnail and search also from metadata field for part numbers because updaters have put extra oem-part numbers in metadata field.

Are there any suggestions where to start? Below is the current query:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['pn']) === true && empty($_POST['pn']) === false) {
$sql = 'select * from ' . TABLE_PRODUCTS . ' where products_model like :modelID:';
$sql = $db->bindVars($sql, ':modelID:', '%' . $theProductId . '%', 'string');
$result = $db->Execute($sql);