bingwalker Posted March 14, 2010 Share Posted March 14, 2010 This is weird, and has stumped several different MySQL and PHP "Experts". I have this INSERT statment: INSERT INTO metrics VALUES (NULL,1,CURDATE(),'angelswire',197 ,1188 ,94 ,NOW()); When I run that, it runs fine and the database is populated. However, when use mysql_query instead, the data in '$tweets' turns to zero. $query = "INSERT INTO metrics VALUES (NULL,$acct,CURDATE(),'$user',$followers,$following,$tweets,NOW())"; mysql_query($query) or die(mysql_error()); If I echo $query, then copy and paste into mysql, or navicat, or sequel pro, it works fine. The data type is INT(11). But whenever I use mysql_query to insert, that data gets turned to 0. The column allows null. EVERYBODY is stumped. Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2010 Share Posted March 14, 2010 What does this show - var_dump($tweets); Any chance your page is being requested twice, the second time with zero or nothing in the $tweets variable? Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1025924 Share on other sites More sharing options...
DaiLaughing Posted March 14, 2010 Share Posted March 14, 2010 Any reason $user is in quotes and the other variables are not? Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1025963 Share on other sites More sharing options...
PFMaBiSmAd Posted March 14, 2010 Share Posted March 14, 2010 The ones not in quotes are numeric fields. Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1025967 Share on other sites More sharing options...
DaiLaughing Posted March 14, 2010 Share Posted March 14, 2010 All I can come up with is the obvious stuff you will have already thought of. The empty variable you have covered by echoing it. I tend to use the longer method of defining the fields into which the values are being inserted just in case I change the table structure but again pasting the echoed query directly would also fail if you were putting $tweets into the wrong field. Presumably $followers, $following and NOW all save fine. Without access to the database I'm stumped (I wouldn't class myself as an "expert" though so don't add me to your total!). Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1025973 Share on other sites More sharing options...
bingwalker Posted March 16, 2010 Author Share Posted March 16, 2010 What does this show - var_dump($tweets); Any chance your page is being requested twice, the second time with zero or nothing in the $tweets variable? vardump($tweets) produces this: string(46) "113" string(44) "2" string(46) "155" string(46) "134" string(45) "97" string(46) "185" string(45) "84" string(47) "2511" string(46) "620" string(45) "11" string(46) "200" string(46) "633" string(45) "30" string(44) "4" string(46) "467" string(44) "2" string(46) "256" string(45) "75" string(45) "58" string(44) "2" string(44) "2" string(46) "217" string(46) "275" string(46) "442" string(44) "3" string(46) "577" string(46) "262" string(46) "123" string(46) "151" string(44) "1" string(46) "182" string(46) "174" string(46) "125" string(45) "74" string(46) "152" string(46) "136" string(44) "2" string(46) "103" string(46) "151" string(46) "179" string(46) "134" string(46) "569" string(45) "94" string(46) "138" string(45) "92" string(46) "185" string(46) "277" string(46) "372" string(45) "43" string(46) "245" string(46) "149" string(46) "132" string(46) "182" string(44) "8" string(46) "159" string(46) "133" string(46) "149" string(45) "97" string(44) "5" string(46) "141" string(46) "147" string(46) "612" string(45) "72" string(46) "459" string(46) "192" string(45) "12" string(44) "3" string(46) "256" string(45) "74" string(44) "8" string(45) "13" string(45) "48" string(45) "23" string(44) "8" string(44) "4" string(45) "12" string(46) "200" string(45) "73" string(45) "24" string(45) "43" string(45) "43" string(45) "39" string(44) "1" string(45) "39" string(45) "39" string(45) "39" string(45) "39" string(45) "39" string(45) "39" string(45) "50" string(44) "1" string(44) "9" string(44) "0" string(44) "0" string(44) "0" string(44) "0" string(44) "0" string(44) "4" string(45) "24" string(44) "0" string(44) "0" string(44) "7" string(44) "7" string(44) "4" string(45) "62" string(44) "0" string(44) "0" string(44) "0" string(44) "0" string(44) "0" string(44) "0" string(44) "8" string(44) "8" string(44) "7" string(44) "7" string(44) "7" string(45) "12" string(45) "12" string(45) "12" string(45) "12" string(44) "0" string(45) "17" string(44) "6" string(44) "9" string(45) "77" string(45) "10" string(44) "0" string(44) "5" Some of the data are in reality 0's, but why "string"? Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1026813 Share on other sites More sharing options...
DaiLaughing Posted March 16, 2010 Share Posted March 16, 2010 PHP is not strongly typed so it will keep data as strings or numbers and switch between the two when it feels like it. I've never used vardump but if the data came from a Web page (e.g. form) originally then by definition it would be text (even if the text is numbers). I do think PFMaBiSmAd has got you to spot your problem though. It seems $tweets is an array not a value so it will not be saved in a numeric field in MySQL. Where does $tweets come from? I always print_r arrays as I find the output more friendly (especially between pre tags). Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1026832 Share on other sites More sharing options...
bingwalker Posted March 16, 2010 Author Share Posted March 16, 2010 PHP is not strongly typed so it will keep data as strings or numbers and switch between the two when it feels like it. I've never used vardump but if the data came from a Web page (e.g. form) originally then by definition it would be text (even if the text is numbers). I do think PFMaBiSmAd has got you to spot your problem though. It seems $tweets is an array not a value so it will not be saved in a numeric field in MySQL. Where does $tweets come from? I always print_r arrays as I find the output more friendly (especially between pre tags). $tweets isn't an array, this is the output of all the $tweets data using print_r($tweets) 114215613598186852512621112016343044672257765822218276443357726212415211831751267515313721041521801355711001399318527737243248150132182816013414998514114861473459193123257748144924961220074254444401404040404040511100000042400884620000009988813131313117610771005 But when it gets pushed to the db the values all turn to zero. Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1026840 Share on other sites More sharing options...
PFMaBiSmAd Posted March 16, 2010 Share Posted March 16, 2010 Based on the length and the 'printing' part that var_dump() shows, you likely have a bunch of non-printing characters (probably nulls) as part of the value that are likely causing a zero to be inserted rather than the 'visible' value that you see when you echo it. A) Where is this value coming from (what is the code), and B) Use trim() on the value to remove the non-printing characters. Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1026893 Share on other sites More sharing options...
DaiLaughing Posted March 16, 2010 Share Posted March 16, 2010 Also what is the database field into which the data goes and what does the data represent? Quote Link to comment https://forums.phpfreaks.com/topic/195195-mysql_query-makes-data-zero-ive-stumped-the-experts/#findComment-1027128 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.