Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/264972-resource-id-9/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357850
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/264972-resource-id-9/#findComment-1357853
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.