Why am I not seeing an error message? [duplicate] - php

This question already has answers here:
PHP errors are not shown
(2 answers)
Closed 9 years ago.
I'm feeling my way around php for the first time in years. I'm trying to perform a simple select statement. I've confirmed the statement works directly against mysql. My php does not complete. I'd like to know why my real_query isn't working, I'd also like to know how to coax an error message out of this scenario. Here's the code:
function getRoot($nid)
{
echo $nid; //displays 0
try
{
echo "Hello?"; //This displays
//Why doesn't this work?!
if($mysqli->real_query("SELECT * FROM gem, bundles WHERE gem.nid = bundles.baseNid AND bundles.nid = " . $nid))
{
echo "dafuq?"; //does not display
}
else
{
echo "foo"; //doesn't display
echo $mysqli->error; //doesn't display
}
}
catch (Exception $e)
{
echo "Tralalalala"; //doesn't display
}
echo "teeheehee"; //doesn't display
}
Thanks for your help!

There is no $mysqli variable declared in the function, so your code produce a FATAL ERROR - you are calling a method of a non-object. if $mysqli is a global variable, you have to add global $mysqli; in the beginning of the function

$mysqli is not defined in the function, and PHP throws a fatal error, which is not catcheable using standard exceptions.
You need to enable error reporting (display_errors set to On in php.ini, or through ini_set, eg: ini_set('display_errors', '1'); );

Related

Prevent PHP from echoing if exception occured

I noticed that PHP's echo successfully prints strings even if somewhere in the script an error is thrown and not handled. For example:
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "started";
throw new Exception("big error");
echo "done";
prints "started", even though an error occured. The status code is 500 indeed, but I don't think displaying partial results works for all cases.
Using ob_start() and ob_get_contents() offers some flexibility, but I expected that PHP offers a switch to set displaying to none if error occured. Does this switch exist?
when you say throw new Exception() this means you are saying move the program control to caller and don't execute the further statements after this throw statement.
thanks to: Difference between "throw new Exception" and "new Exception"?
echo "started"; // <- This will occurs
throw new Exception("big error"); // <- And here the Exception will be thrown
echo "done"; // <- therefore, this line won't be reached
One way to solve this would be using a variable to store what you want to echo and only echo it if there are no uncaught exceptions
$echoStr = "";
$echoStr .="started";
throw new Exception("big error");
$echoStr .="done";
echo $echoStr;
Echo instantly send the data to the server (at least in this code it does) and no longer can be affected by what happens next. It's generally bad practice to work like that (eg: after an echo you can no longer change headers like a redirect, which can be very inconfinient), better would be to stored everything in a variable and output it when you want:
$output = "started";
throw new Exception("big error");
$output.= "done";
echo $output; // or if in a function, return instead of echo

How to move functions to another file [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I developed a query to list fields with checkboxes for the user selection of ones to include in a query result. It was working in the template file (based on page.php). Then I decided to move the logic that I will be using in other templates to a db-functions.php file. Now, I'm getting
Fatal error: Call to a member function get_results() on null in /home/...
printf($the_db) returns the proper login credentials, but apparently an error because 'affected_rows' = -1.
I can't find what's missing or misconfigured. Code is below.
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . $folder . '/wp-load.php');
//db-setup
function dbsetup($usr, $passwd, $databas, $hst){
global $wpdb;
$the_db = new wpdb($usr, $passwd, $databas, $hst);
}
//disp-field-list
function dbfldfrm(){
$fieldlstress = $the_db->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = 'index_records'");
Before asking, please search for solution on this site, with the error string. Btw, you need to globalise the variable from local scope to be available globally:
function dbsetup($usr, $passwd, $databas, $hst){
global $wpdb;
global $the_db;
$the_db = ...
// same as: $GLOBALS['the_db'] = ...
and secondly, to obtain from global scope, do the same:
function dbfldfrm(){
global $the_db;
.....

Salesforce PHP Toolkit create() Does not Work

I am using the PHP Toolkit (v20) with the Enterprise WSDL to access my Salesforce objects and records in my webapp.
I successfully got everything working on my localhost.
When I uploaded the script to my webhost (inmotion), the create function simply does not work. It does not show any errors or warnings (although I have set error reporting to E_ALL), but execution of the script simply stops when it encounters the create function.
However, running a query using the query function or something like getting the server timestamp works fine.
Again, it works fine on my localhost, so I dont think its a code problem, but for the life of me I cannot figure out the problem, as no errors/warnings are displayed nor exceptions thrown.
I should clarify that although in this example I am trying to insert into my custom object, I have tried to insert into a standard Account or Contact object as well and it has not made any difference.
It will be great if somebody can guide me on how I can go about trying to troubleshoot the problem.
Thanks.
Code is as follows:
<html>
<head></head>
<body>
<?php
ini_set("soap.wsdl_cache_enabled", "0");
define("USERNAME", "myusername");
define("PASSWORD", "mypassword");
define("SECURITY_TOKEN", "mysecuritytoken");
require_once ('soapclient/SforceEnterpriseClient.php');
try {
$sflink = new SforceEnterpriseClient();
$sflink->createConnection("soapclient/auv.wsdl.xml",null,array('trace'=>true));
//$sflink->createConnection("soapclient/auv.wsdl.xml");
$sflink->login(USERNAME, PASSWORD.SECURITY_TOKEN);
// Test to get the server timestamp - Works fine.
echo "Getting Timestamp<br>";
$resp = $sflink->getServerTimestamp();
print_r($resp);
echo "<br>";
// Test to query the server - Works fine.
$query = "SELECT Id, AccountId, FirstName, LastName FROM Contact WHERE Email = 'ankur#alfauv.com'";
$response = $sflink->query($query);
echo "Select result is: <br>";
print_r($response);
echo "<br>";
// Insert into UVSI Search Object (My custom object) - Does not work
$sObject = new stdclass();
$sObject->Account__c = "00190000006yxUrAAI";
$sObject->Contact__c = "00390000005ZYLcAAO";
$sObject->Application__c = "AAP";
$sObject->Flow__c = 66;
$sObject->Flow_Unit__c = "m3-hr";
$sObject->Dose__c = 33;
$sObject->UVT__c = 99;
echo "Attempting creation<br>";
$createResponse = $sflink->create(array($sObject), 'UVSI_Search__c');
echo "This line never prints.<br/>";
} catch (Exception $e) {
echo "In error condition<br>";
echo $sflink->getLastRequest();
echo $e->faultstring;
exit();
}
?>
</body>
</html>
After breaking my head over this for 3 days, I finally found a solution. I changed the PHP version for my site from 5.2 to 5.3 and the problem just went away.
I still dont know why it did not work, but this is the solution in case anybody else has a similar problem.

$write_result->errors throws "Undefined Property" when there are no errors

Using pre-written code from Bronto, it builds a soap client, calls a function on it, then parses the results. The parsing code looks like this:
if ($write_result->errors) {
print "There was a problem adding or updating the contact:\n";
print_r($write_result->results);
exit;
} elseif ($write_result->results[0]->isNew == true) {
print "The contact has been added. Id: " . $write_result->results[0]->id . "\n";
} else {
print "The contact's information has been updated. Id: " . $write_result->results[0]->id . "\n";
}
Whenever there ARE errors, they get caught and printed by the first if statement. But when there AREN'T errors, the console gets an "Notice: Undefined property: stdClass::$errors" message printed out. Is this right? Is there a way to turn off the notice? It doesn't cause any problems, but I can see how it would confuse a non-techy reading the output logs.
Check if the property exists instead of accessing it directly:
if (isset($write_result->errors))
Or to check if it exists and not empty at once (just to be sure in case the API changes and provides an actual empty array or empty string if no errors occured):
if (!empty($write_result->errors))
You could check first if the property exists:
if (property_exists($write_result, 'errors'))
Check that the property exists first:
if (property_exists($write_result, 'errors') && $write_result->errors)
{
// ...
}
See: property_exists.

Can't use function return value in write context, PHP can't figure out why

This is my code :
PHP:
if(isset($_COOKIE("cookie_roof_angle")) && isset($_COOKIE("cookie_roof_direction")))
{
$roof_angle = intval($_COOKIE("cookie_roof_angle"));
$roof_direction = $_COOKIE("cookie_roof_direction");
$solarsell_page05_rendement = mysql_query("SELECT value FROM solarsell_page05_pvgis WHERE angle = $roof_angle AND azimut = " . $roof_direction. " ");
echo $solarsell_page05_rendement;
}
else
{
echo "no values";
}
I'm getting this error message :
Fatal error: Can't use function return value in write context in C:\xampp\htdocs\Development\phpFunctions.php on line 19
After some searching around on the web & stackoverflow.com, I found out it may be caused by the isset function, could anyone please explain if this is the problem and why?
If this is not the problem, maybe I did something wrong in my code part, but I can't figure out why.
The Cookies where both set when I got the error.
Sincerly,
Harmen Brinkman.
Syntax error:
$_COOKIE["cookie_roof_angle"] instead of $_COOKIE("cookie_roof_angle")

Categories