blacblu Posted June 28, 2012 Share Posted June 28, 2012 Hi. I'm trying to get an specific data from my database using this line of code: $strusername = mysql_query('SELECT Username FROM Users WHERE Password = "$_POST[password]" '); But it is returning the following: Resource id #9 Here goes the entire code: <html> <head> <title>Home2</title> </head> <body style="background-image:url(novofundo.jpg)"> <?php mysql_connect("mysql5.000webhost.com", "a9698509_blacblu", "nopassword1"); mysql_select_db("a9698509_blacblu") or die(mysql_error()); $strusername = mysql_query('SELECT Username FROM Users WHERE Password = "$_POST[password]" '); //$strpassword = mysql_query('SELECT Password FROM Users WHERE Username = "$_POST[username]" '); if(strlen($_POST[password]) > 0 && strlen($_POST[username]) > 0){ if($strusername == $_POST[username] && $strpassword == $_POST[password]){ session_start(); $_SESSION["Login"] = "YES"; echo "<h1>Ol? " .$_POST["username"]. "</h1>"; echo "<h2>seu grafico esta aqui</h2>";//colocar grafico aqui } else{ session_start(); $_SESSION["Login"] = "NO"; echo "<h1>Seus dados estao incorretos.</h1>"; } }else{ session_start(); $_SESSION["Login"] = "NO"; echo "<h1>Voc? deixou um ou mais campos em branco.</h1>"; } echo $strusername; echo $strpassword; ?> </body> </html> What is being done wrong? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/264972-resource-id-9/ Share on other sites More sharing options...
Pikachu2000 Posted June 29, 2012 Share Posted June 29, 2012 mysql_query() returns a result resource on successful execution. You need to extract the data from the result resource before you can echo it. See example #2 here: mysql_query. Quote Link to comment https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357844 Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 I use this function and process in all of my non PDO projects: set this function in an include file somewhere: function db_result_to_array($result) { $res_array = array(); for ($count=0; $row = @mysql_fetch_array($result); $count++) $res_array[$count] = $row; return $res_array; } Then, in your main code: $query = "SELECT * FROM BlahTable WHERE foo = 'bar'"; $result = mysql_query($query); if(!$result){//put error handling here: I echo mysql_errno() and mysql_error() to the screen to tell me why I couldn't get a result back. Then I use die(); to stop the rest of the script from running until I get the query problem resolved.} $result = db_result_to_array($result); foreach($result as $row) { //now step through: each pass through the foreach loop will be one returned row from the database. The best part about this is you can reference either the index number OR the column name. Let's say the 3rd column in your database is named "foo" and the single returned row has the value of "bar" in that column. In that case: $value1 = $row[2];//index 2 is the 3rd item in a zero-based array $value2 = $row['foo'];//remember that foo is the sql column name echo "<br/>value1 is: $value1 <br/>and value 2 is: $value2</br>"; //both will include "foo" in the echo } From that point you can use whatever variable values you assigned during the processing of your returned results. This function is very simple to incorporate but it's pretty robust and helps you get what you want. To address your specific issue, you have the variable $strusername taking the value of the returned array. Whether you use the function above or some other way, you have to step through the returned results. At the point it is in your code, it's not even an array yet. I hope that makes sense. Resource #9 kicked my butt when I first started learning PHP Quote Link to comment https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357850 Share on other sites More sharing options...
Pikachu2000 Posted June 29, 2012 Share Posted June 29, 2012 I really see no point in using that function. It does the same thing as mysql_fetch_array() does in a while() loop, only now the script uses significantly more resources. Quote Link to comment https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357851 Share on other sites More sharing options...
blacblu Posted June 29, 2012 Author Share Posted June 29, 2012 mysql_query() returns a result resource on successful execution. You need to extract the data from the result resource before you can echo it. See example #2 here: mysql_query. Thank you! It worked just as I expected. Quote Link to comment https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357852 Share on other sites More sharing options...
jcanker Posted June 29, 2012 Share Posted June 29, 2012 Quite true--you'll see no argument from me there. When I was just learning PHP in the early 4.x days I was not only new to PHP but to programming period. The first book I picked up (1st Edition Welling/Thompson's PHP and MySQL Web Development) used this function throughout. Because of my neophyte understanding of programming (really more from lack of understanding) at the time, I had difficulty trying to move beyond it and use functions like mysql_fetch_array() and get the results I expected. Now I'm using PDO for all the new projects and only use mysql_fetch_array() in the older projects and only use mysql_fetch_array() when updating other features of my earliest projects more to keep everything consistent. I remember how frustrating it was to me trying to teach myself, especially at the beginning, and that function #1 made it easier to get past returned queries and worry about the rest of the program and #2 made it easier for me to visualize what was going on so that I could understand what was happening between getting the resource and assigning values from the query to variables. I offer it up to help people who were like me for the same reasons I used it back when I bought that first PHP book circa 2003. Quote Link to comment https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357853 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.