Stack Out Of Space Error - php

I'm having a little trouble with a query I've written (please see below).
<?php
require("phpfile.php");
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ("hostname", $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$query = "SELECT userdetails.userid
, detectinglocations.locationid
, detectinglocations.locationname
, finds.findid
, finds.locationid
, finds.findosgb36lat
, finds.findosgb36lon
, finds.dateoftrip
, finds.findcategory
, finds.findname
,finds.finddescription
, finds.detectorsettings
, finds.pasref
, finds.additionalcomments
, detectors.detectorname
, searchheads.searchheadname
FROM userdetails, detectinglocations, finds, detectors, searchheads
WHERE finds.userid=userdetails.userid
AND finds.locationid=detectinglocations.locationid
AND finds.detectorid=detectors.detectorid
AND searchheads.detectorid=detectors.detectorid";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("findid",$row['findid']);
$newnode->setAttribute("locationid",$row['locationid']);
$newnode->setAttribute("locationname",$row['locationname']);
$newnode->setAttribute("dateoftrip",$row['dateoftrip']);
$newnode->setAttribute("findcategory",$row['findcategory']);
$newnode->setAttribute("findname",$row['findname']);
$newnode->setAttribute("finddescription",$row['finddescription']);
$newnode->setAttribute("detectorname",$row['detectorname']);
$newnode->setAttribute("searchheadname",$row['searchheadname']);
$newnode->setAttribute("detectorsettings",$row['detectorsettings']);
$newnode->setAttribute("pasref",$row['pasref']);
$newnode->setAttribute("additionalcomments",$row['additionalcomments']);
}
echo $dom->saveXML();
?>
When I run the php script through my web browser it retrieves the correct data, but when I run this through the HTML page I get an 'Out of Stack' error. From what I've read on the web, I think it may be because the SQL query is too complex.
Could you tell me please can an overly complex SQL query cause this type of error?

There's something wrong/unexpected with your data.
1) Do a file_put_contents("somedumpfile", var_export($row, true)) at the top of each loop, and see what's in the file after the process dies.
2) If that didn't help, then systematically remove one field at a time from being added as a node, from top to bottom. When you stop getting the error, you found the culprit.
3) If that still didn't help, start re-adding the fields as nodes, from top to bottom.
Make sure the PHP error-log is fully enabled and see if PHP is complaining about anything else. Also think about dumping the row index and PHP's current memory consumption (memory_get_usage) into that same file.
Good luck. Share your results.
(Vote me up if you like/accept this answer.)
Dustin

Related

phpgraph lib not showing any output

Im using phpgraph lib to create graphs on my linux server. I tried an example and it worked, but I had provided it with the data.
then I wanted to connect it to mysql database and plot a query, when I run it, nothing happens, I don't see any output on the page or any errors, I don't see any output on the page at all, even if I put wrong credentials to my database e.t.c any inputs?
I have executed the sql statement on sql server and it's working fine.
the version of php the server has is PHP 5.3.3
<?php
include('phpgraphlib.php');
$graph= new PHPGraphLib(550,350);
$link = mysql_connect('localhost', 'user', 'password')
or die('Could not connect: ' . mysql_error());
mysql_select_db('databasename' or die('Could not select database');
$dataArray=array();
//get data from database
$sql="my sql statement";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["var1"];
$count=$row["count"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Sales by Group");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
I fixed it, I was expecting to see errors on the webpage, but didn't see any on CHROME, I hen opened it in IE and saw error 500.
Troubleshooted through the log file.
Turned out the sql statement wasn't suppose to have double quotes e.g instead of
where name="john"
it's suppose to be
where name='john'

Waiting for task in PHP to finish before moving to next item

Quick question here, i've got a process running that grabs RSS feeds and adds them to a mySQL database.
During this process I'll be using the Readability API to grab the URL content as I go.
Now this works fine on single entries, but as this script can have hundreds of entries, nothing is being inserting into my database.
I'm wondering if it's not getting a chance to finish the process and immediately skipping onto the next entry in the RSS.
Can anyone suggest a way of letting it finish before moving on? Code below:
$db_hostname="localhost";
$db_username="myusername";
$db_password="mypassword";
try
{
/* query the database */
$db = mysql_connect($db_hostname,$db_username,$db_password);
if (!$db)
{
die("Could not connect: " . mysql_error());
}
mysql_select_db("MyDB", $db);
// Get stories that don't have a the readability assigned
$query="select item_id, item_url from tw_articles_parse where story_readability = '' LIMIT 0 , 1";
$result=mysql_query($query);
$num=mysql_numrows($result);
// Close the DB connection
mysql_close();
// Start the loop of source RSS feeds
$i=0;
while ($i < $num) {
$item_url=mysql_result($result,$i,"item_url");
$item_id=mysql_result($result,$i,"item_id");
// Parse the story URL into the Readability API
$url = "https://www.readability.com/api/content/v1/parser?url=$item_url&token=myapikey";
// Get the contents of the JSON returned by the API
$json = file_get_contents($url);
// Decode the JSON
$out = json_decode($json, true);
// Set the content as a variable
$story = mysql_real_escape_string($out['content']);
// Insert into the DB - Adding 0 to story_club_id as default
$item_insert_sql = "UPDATE tw_articles_parse SET story_readability=$story WHERE item_id='" . $item_id . "'";
$insert_item = mysql_query($item_insert_sql, $db);
$i++;
}// end the loop of feeds
} catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Probably nothing is inserted because you are using UPDATE statement and there are simply no such records with correspoding item_id to be updated?
Try changing UPDATE query to INSERT ... ON DUPLICATE KEY UPDATE
Unfortunately we don't know your database scheme, but something like this should work:
$item_insert_sql = "INSERT INTO tw_articles_parse (story_readability, item_id) VALUES ('$story', $item_id) ON DUPLICATE KEY UPDATE story_readability='$story'";
Maybe you're running out of memory or time? Enable warnings and error reporting:
ini_set("display_errors", 1);
error_reporting(E_ALL);

INSERT IGNORE In mySQL

I wonder whether someone can help me please.
I'm trying to put together a PHP script that takes data from an xml file and places the data in a mySQL data. I've been working on this for a few days and I'm still can't seem to get this right.
This is the code that I've managed to put together:
<?
$objDOM = new DOMDocument();
$objDOM->load("xmlfile.xml");
$Details = $objDOM->getElementsByTagName("Details");
foreach( $Details as $value )
{
$listentry = $value->getElementsByTagName("listentry");
$listentrys = $listentry->item(0)->nodeValue;
$sitetype = $value->getElementsByTagName("sitetype");
$sitetypes = $sitetype->item(0)->nodeValue;
$sitedescription = $value->getElementsByTagName("sitedescription");
$sitedescriptions = $sitedescription->item(0)->nodeValue;
$siteosgb36lat = $value->getElementsByTagName("siteosgb36lat");
$siteosgb36lats = $siteosgb36lat->item(0)->nodeValue;
$siteosgb36lon = $value->getElementsByTagName("siteosgb36lon");
$siteosgb36lons = $siteosgb36lon->item(0)->nodeValue;
//echo "$listentrys :: $sitetypes :: $sitedescriptions :: $siteosgb36lats :: $siteosgb36lons <br>";
}
require("phpfile.php");
//Opens a connection to a MySQL server
$connection = mysql_connect ("hostname", $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
mysql_query("INSERT IGNORE INTO scheduledsites (listentry, sitetype, sitedescription, siteosgb36lat, siteosgb36lon) VALUES('$listentrys','$sitetypes','$sitedescriptions','$siteosgb36lats','$siteosgb36lons') ")
or die(mysql_error());
echo "Data Inserted!";
?>
I can pull the data from the xml file, but it's the part of the script that sends the data to my database table that I'm having trouble with.
The script runs but only the last record is saved to the database.
I can parse the fields from the xml file without any problems and the check I'm trying to put in place is, if there is a 'listentry' number in the new data that is matched to one already in the table then I don't want that record to be added to the table, i.e. ignore it.
I just wondered whether someone could perhaps take a look at this please and let me know where I'm going wrong.
Many thanks
You are only calling mysql_query once. So it will only insert one row.
The sql needs to be inside the loop.

Make sure most recent SQL query result is used in autosuggest

I'm using the PHP code below to get results to return to an autosuggest / autocomplete. The problem is that if an earlier SQL query takes longer to return from the database than the most recent SQL query then the results of the older query will be displayed in the autosuggest results box. So if you start searching for Thomas, it might show the results for Tho if that SQL query takes longer. I was wondering if there is a way to cancel previous queries once a new one is implemented, or to make sure that the most recent query is used?
<?php
// begin XML output
$xmlStr = <<<XML
<?xml version='1.0' standalone='yes'?>
<authors>
XML;
// open database connection
$mysqli = new mysqli("localhost", "user", "pass", "library");
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
// retrieve author list matching input
// add to XML document
$q = $mysqli->real_escape_string($_GET['query']);
$sql = "SELECT AuthorName FROM author WHERE AuthorName LIKE '" . $q . "%' ORDER by AuthorName";
if ($result = $mysqli->query($sql)) {
while ($row = $result->fetch_row()) {
$xmlStr .= '<author name="' . $row[0] . '"></author>';
}
$result->close();
}
// clean up
// output XML document
$mysqli->close();
$xmlStr .= '</authors>';
header("Content-Type: text/xml");
echo $xmlStr;
?>
You might want to synchronize the AJAX calls. Like having a few seconds delay after the key up event and then make the AJAX call. Setting asynchronous = true can also help.
you can abort the ajax call by calling the abort method: XmlHttpRequest.abort() before making the new request

php mysql_select_db returns blank page

I'm just starting out writing this code and when I add in the db select and refresh the page, instead of showing all the other html on the page or an error, it just shows up blank.
Here's what I've got-
$link = mysql_connect('vps2.foo.com:3306', 'remote_vhost30', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('best_of', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$sections_result = "SELECT * FROM sections";
$sections_query = mysql_query($sections_result) or die(mysql_error());
$sections_array = mysql_fetch_array($sections_result) or die(mysql_error());
This code above returns a blank page. If I comment out the row starting with $db_selected the page loads fine. Obviously it doesn't do anything with the data but no errors.
What's the problem? (And yes, I am connecting to a remote server, but the $link produces no errors)
The last line of code should be:
$sections_array = mysql_fetch_array($sections_query) or die(mysql_error());
You are trying to fetch rows from the variable $sections_result, which is your query string and not the result set.
Turn on error reporting, with error_reporting(E_ALL) like mentioned in one of the other answers.
Incidentally, I suspect the problem is that PHP is throwing an error, but you've disabled the display of errors - hence the display of a blank white page. Check the status of 'display_errors' within your php.ini file.
NB: If this is a production server, you should leave display_errors set to off.
Check it really is that line by replacing this:
$db_selected = mysql_select_db('best_of', $link);
With this:
if (! $db_selected = mysql_select_db('best_of', $link)) die('Unable to select database');
As MitMaro says you've muddled _result and _query. This might be better:
$sections_query = "SELECT * FROM sections";
$sections_result = mysql_query($sections_query) or die(mysql_error());
$sections_array = mysql_fetch_array($sections_result) or die(mysql_error());
Hope that helps :)

Categories