{"id":7284,"date":"2014-06-03T23:35:09","date_gmt":"2014-06-03T23:35:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/simple-mysql-returning-unexpected-values-closed-collection-of-common-programming-errors\/"},"modified":"2014-06-03T23:35:09","modified_gmt":"2014-06-03T23:35:09","slug":"simple-mysql-returning-unexpected-values-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/simple-mysql-returning-unexpected-values-closed-collection-of-common-programming-errors\/","title":{"rendered":"Simple MySQL returning unexpected values? [closed]-Collection of common programming errors"},"content":{"rendered":"<p>First of all, start by initializing your variables. Just in case.<\/p>\n<pre><code>$cashset = 0;\n$scash   = 0;\n$ecash   = 0;\n$current = 0;\n<\/code><\/pre>\n<p>Then run the loop<\/p>\n<pre><code>while ($row=$resultzero-&gt;fetch_assoc()) {\n    $cashset = $row['cashset']; \/\/ value 0\n    $scash = $row['scash']; \/\/ value 0\n    $ecash = $row['ecash']; \/\/ value 0\n    $current = $row['current']; \/\/ value 0\n    \/\/ DEBUG\n    print \"Read row: ($cashset,$scash,$ecash,$current)\\n\";\n<\/code><\/pre>\n<p>}<\/p>\n<p>Then use comparison, as you already tried (don&#8217;t know why it didn&#8217;t work &#8212; I&#8217;m betting it had something to do with missing initialization)<\/p>\n<pre><code>if ((0 == $cashset) &amp;&amp; (0 == $scash) &amp;&amp; (0 == $ecash) &amp;&amp; (0 == $current))\n{\n    $firstTime = 1; \/\/ var_dump($firstTime) = NULL\n} else\n{\n    $firstTime = 0; \/\/ var_dump($firstTime) = NULL\n}\n<\/code><\/pre>\n<p>The <code>(0 == $variable)<\/code> instead <code>($variable == 0)<\/code> for comparisons is a pet trick of mine, so that if you mistakenly forget a = sign it will ensure you get an error, instead of the instruction becoming an assignment.<\/p>\n<p>Also, verify that you are not retrieving <strong>more than one row<\/strong>, since the last row will overwrite the first. The code above dumps each row to screen when it is loaded for debug purposes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First of all, start by initializing your variables. Just in case. $cashset = 0; $scash = 0; $ecash = 0; $current = 0; Then run the loop while ($row=$resultzero-&gt;fetch_assoc()) { $cashset = $row[&#8216;cashset&#8217;]; \/\/ value 0 $scash = $row[&#8216;scash&#8217;]; \/\/ value 0 $ecash = $row[&#8216;ecash&#8217;]; \/\/ value 0 $current = $row[&#8216;current&#8217;]; \/\/ value 0 \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7284","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7284"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7284\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}