{"id":1223,"date":"2018-01-19T19:22:34","date_gmt":"2018-01-19T19:22:34","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/mysql-error-not-a-valid-result-resource\/"},"modified":"2018-01-19T19:24:37","modified_gmt":"2018-01-19T19:24:37","slug":"mysql-error-not-a-valid-result-resource","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/mysql-error-not-a-valid-result-resource\/","title":{"rendered":"MySQL Error: Not a valid result resource"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Brett Patterson<\/div>\n<div class=\"\">on June 27, 2005<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<p>Those of you who are new to PHP programming and are using MySQL may run into many problems in getting started. Of course most of us wish to gather some data from the database and view it in a browser. Some times we run into problems where this just won&#8217;t work. One of the most frequent problems I have seen is the &#8220;Not a valid result resource&#8221; error provided by MySQL. Thankfully I can tell you how to work through it to get what you want done.<br \/>Basically when you run a query on the MySQL server, the result is returned. That&#8217;s fairly obvious, right? So let&#8217;s look at some queries:<\/p>\n<p>SELECT * FROM `table_name` ORDER BY column_title DESC;<\/p>\n<p>SELECT column_title, column_title, column_title, column_title<br \/>\u00a0\u00a0 \u00a0FROM `table_name` ORDER BY column_title DESC;<\/p>\n<p>SELECT column_title FROM `table_name` ORDER BY column_title DESC;<\/p>\n<p>The first query will select ALL column data in the table referenced. The second will only grab the column data specified. And the third is the most basic query of all&#8211;it will select only the one columns data in the table. Now, with that, if you limit the third query to one row to be returned, the result will be the data you&#8217;re after. So let&#8217;s say we have a table with the following setup:<\/p>\n<p>Table Name: members<br \/>|\u00a0 id\u00a0 | Username\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\u00a0 Password\u00a0\u00a0\u00a0\u00a0\u00a0 |<br \/>+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>|\u00a0 1\u00a0\u00a0 | patterson_b\u00a0\u00a0\u00a0 | password1\u00a0\u00a0\u00a0\u00a0\u00a0 |<br \/>|\u00a0 2\u00a0\u00a0 | killer_bee\u00a0\u00a0\u00a0\u00a0 | pbcup\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |<br \/>+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;-+<\/p>\n<p>Let&#8217;s say we want to grab the password of the first user in the table. We would run the following query:<\/p>\n<p>$query = &#8220;SELECT Password FROM `members` ORDER BY id ASC LIMIT 1&#8221;;<br \/>$result = mysql_query($query);<br \/>echo $result;<br \/>The code above will run a query on the database and grab the first password in the table.\u00a0 When we echo the variable $result, we will get &#8220;password1.&#8221;\u00a0 Let&#8217;s look at another snippet of code:<\/p>\n<p>Code: $query = &#8220;SELECT * FROM `members` ORDER BY id DESC&#8221;; $result = mysql_query($query); echo $result;<\/p>\n<p>What we&#8217;re doing there is running the query on the database and getting some data back as a result, and then we print the result of the query.<br \/>Now, what we should see (if the query is working properly) is something like:<br \/>Resource ID #?? where &#8220;??&#8221; is equal to an integer. If that is not the output you get, then you have to add a die statement to the query to see what your query error is. So your code could look like:<\/p>\n<p>$query = &#8220;SELECT * FROM `members` ORDER BY id DESC&#8221;;<br \/>$result = mysql_query($query) or die(mysql_error());<\/p>\n<p>The mysql_error() will spit out the exact error that mysql encounters. If you would like even more information, you can use mysql_errno() to give you the error number that corresponds to the error. This is helpful for googling for answers to your problems.<br \/>If you get an actual result, not a resource ID or anything like that, but something that you expect, then you need to select more than one column and more than one row. Here&#8217;s some examples of working &amp; non-working code.<\/p>\n<p>\/\/ Basic Connection settings<br \/>$conn = @mysql_connect(&#8216;localhost&#8217;, &#8216;user&#8217;, &#8216;pass&#8217;);<br \/>if($conn)<br \/>{<br \/>\u00a0\u00a0\u00a0 mysql_select_db(&#8216;test_database&#8217;, $conn);<br \/>}<br \/>else<br \/>{<br \/>\u00a0\u00a0\u00a0 echo &#8220;Couldn&#8217;t connect to database!!&#8221;;<br \/>}<\/p>\n<p>$query1 = &#8220;SELECT * FROM `test_table` ORDER BY id DESC&#8221;;<br \/>$result1 = mysql_query($query1);<br \/>echo $result1.'&lt;hr&gt;&#8217;;<\/p>\n<p>$query2 = &#8220;SELECT id FROM `test_table` ORDER BY id DESC LIMIT 1&#8221;;<br \/>$result2 = mysql_query($query2);<br \/>echo $result2.'&lt;hr&gt;&#8217;;<\/p>\n<p>$query3 = &#8220;SELECT id, name FROM `test_table` LIMIT 0,2&#8221;;<br \/>$result3 = mysql_query($query3);<br \/>echo $result3;<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0That will output:<\/p>\n<p>Resource ID#?? &#8212;&#8212;&#8212;&#8212;&#8211; 1 &#8212;&#8212;&#8212;&#8212;&#8211; Resource ID#??<\/p>\n<p>Note: The &#8220;??&#8221; will be replaced with a numerical value. It is dependant upon other factors and can not be determined.<\/p>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"patterson20050620_2.html\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"patterson20050620_3.html\">3<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"patterson20050620_4.html\">4<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"patterson20050620_5.html\">5<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"patterson20050620_2.html\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This being the first in our New Guy&#8217;s PHP Helper series, author Brett Patterson discusses some of the topics that are frequently mentioned at the<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1223","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1223"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1223\/revisions"}],"predecessor-version":[{"id":3125,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1223\/revisions\/3125"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}