find the error in haystack!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

find the error in haystack!

Post by afbase »

I have four functions in my class that pertain to this one problem: It won't store a value in my DB. Exhibit A:

Code: Select all

class stockdata {
//var's
var $multiplier;

//set methods
function set_multiplier($number){
$this->multiplier=$multiplier;
}

//get methods
function get_multiplier(){
return $this->multiplier;
}

//mysql methods
function mysql_multiplier($ticker){
	$url = "http://moneycentral.msn.com/detail/stock_quote?ipage=qd&Symbol=US%3A".$ticker;
	$ch = curl_init();    
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_FAILONERROR, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
		curl_setopt($ch, CURLOPT_TIMEOUT, 3); 
	$result = curl_exec($ch);
		curl_close($ch); 
	$pattern='!P/E</td><td class="cl1">([^<]*)</td></tr>!';
	preg_match($pattern,$result,$multiplier);
	print_r($multiplier);
	$this->set_multiplier($multiplier[1]);
}

function getdata_storemysql(){

$this->mysql_multiplier($this->ticker);

//insert values into mysql
$mysql_tickname_insert="UPDATE curldata SET multiplier = '".$this->multiplier."' WHERE ticker='$this->ticker';";
if ( $sqldb = mysql_connect( "localhost", "localuser", "localpass" ) ) {
	   print "<br>Connection to coldowl.com username: localuser complete<br>";
}
	else {
	   die ("Unable to connect to coldowl.com username: localuser . Error: <b>".
                                                                    mysql_error()."</b>");
}

	if ( mysql_select_db( intelligent , $sqldb )  ) {
	  print "Select of intelligent complete<br>";
}
	else {
	  die ("Select failed database name intelligent Error number: <b>".mysql_errno().
                                                           " Message: ".mysql_error()."</b>");
}

	if ( mysql_query( $mysql_tickname_insert, $sqldb )  ) {
	  print $mysql_tickname_insert." complete<br>";
}
	else {
	  print  $mysql_tickname_insert." failed. Error number: <b>".mysql_errno().
                                          " Message: ".mysql_error()."</b>";
}
mysql_close($sqldb);
}

}

I began troubleshooting by inserting the print_r() to test if the regex and curling in the mysql_multiplier() was working properly, which it is. I've checked the name of the database, DB table, and DB table column 100x.

***IN STEWIE'S VOICE FROM FAMILY GUY***
How on earth is this thing not storing my values?!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

standard debugging dictates to echo out important variables to see if it matches what you expect. In your case, you are building a query, so lets make sure our query is being built correctly, and if not work our way backwards.

Code: Select all

echo $mysql_tickname_insert;
P.S., you won't find it very often that people will sift through all your code and find your bugs, so you'll generally want to narrow down your inquiries.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

here is the debug

Post by afbase »

Here is a good example of my debugging script.
MMM

MMMArray ( [0] => P/E17.30 [1] => 17.30 ) Array ( [0] => Long Term Debt1,309.0 [1] => 1,309.0 )
Connection to coldowl.com username: localuser complete
Select of intelligent complete
UPDATE curldata SET dividend = '0.46', curass = '7,115.0', multiplier = '', bookval = '5.26', long_term_debt = '1,309.0', total_cur_liabilities = '5,238.0' WHERE ticker='MMM'; complete
I have the ticker, MMM (3M Corporation), and the first array's [1] entry is the P/E Ratio (AKA multiplier) of 3M. It shows that my mysql_multiplier () works fine. the long term debt ratio was another bug i just fixed and is reflected in the echo of the mysql query statment
"UPDATE curldata SET dividend = '0.46', curass = '7,115.0', multiplier = '', bookval = '5.26', long_term_debt = '1,309.0', total_cur_liabilities = '5,238.0' WHERE ticker='MMM'; "
If you notice the multiplier not inserted in the following my MYSQL statement. I'm still working on the problem and yes I realize people don't do other people's work.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You're developing with error_reporting!=E_ALL and/or display_errors!=On.
afbase wrote://set methods
function set_multiplier($number){
$this->multiplier=$multiplier;
}
would have given you an undefined variable $multiplier warning.
afbase wrote:mysql_select_db( intelligent , $sqldb )
would have given you an undefined constant intelligent, assuming 'intelligent' warning. Better change these settings in the php.ini of your development system.
function mysql_multiplier has nothing to do with mysql, renamed to read_msn_data.
At least add error handling for the case preg_match fails/the pattern does not match.
sql statements for mysql_query shouldn't be prepended by ;
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

no such thing as php.ini

Post by afbase »

I am a newly converted Linux user (formerly Windows xp user). I'm not exactly familiar with configuring php or apache 2 in ubuntu linux.




:roll:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Call <?php phpinfo(); ?> it tells you the path of the php.ini you have to edit.
Open this php.ini in a text editor.
Change the values.
Restart the apache. There's probably a script /etc/init.d/apache or /etc/init.d/apache2

Code: Select all

/etc/init.d/apache2 stop
/etc/init.d/apache2 start
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Make sure that you only make these changes locally. Your production server should have display_errors set to 'Off'. But for local development, dev development and test development, always have error_reporting set to E_ALL and display_errors set to 'On'. It is an invaluable tool.
Post Reply