Jump to content

Mysql And Php -> Getting A Specific Field Information?


MrFish
 Share

Recommended Posts

I think it's called a field. I'm trying to get the information from a field where the username is "MrFish" and I want to make it a cookie. I followed the w3c tutorials best I could and tried multiple ways but none of them worked. Here is what I'm trying-dbfetch.png

///////////////////After successfully "logging in" (finding a username/password match), this script is executed to set the cookies.				/* Login Success */				/* Set Expiration Date */		$expdate = time()+(60*60*24*30); /* 30 days*/		/* Find ID */		$queryinput = 'SELECT id FROM users WHERE username =\'' . $username . '\'';		$results = mysql_query($queryinput);		$id = mysql_fetch_field($results); /* WHAT DO I PUT HERE? NOTHING WORKS D:*/				/* Set Cookie */		setCookie("id", $id, $expdate);		setCookie("username", $username, $expdate);				echo '<script type="text/javascript">';		echo 'alert("' . $_COOKIE["username"] . $id . '");';////////////////Temporary Debugging element		echo '</script>';

The username successfully became a cookie. But the id is giving me errors. How do I get the field value correctly?

Edited by MrFish
Link to comment
Share on other sites

mysql_fetch_field isn't going to give the value of a field, it gets information about that field in the database (the name of it, the data type, length, etc). If you want the value you should get the row from the result, and then you can get the value from the row.

$queryinput = 'SELECT id FROM users WHERE username =\'' . $username . '\'';$results = mysql_query($queryinput);if ($row = mysql_fetch_assoc($results))  $id = $row['id'];else  echo 'user not found';

Link to comment
Share on other sites

I think:

///////////////////After successfully "logging in" (finding a username/password match), this script is executed to set the cookies.				/* Login Success */				/* Set Expiration Date */		$expdate = time()+(60*60*24*30); /* 30 days*/		/* Find ID */		$queryinput = 'SELECT id FROM users WHERE username =\'' . $username . '\'';		$results = mysql_query($queryinput);		$array = mysql_fetch_array($results);		$id = $array['id'];				/* Set Cookie */		setCookie("id", $id, $expdate);		setCookie("username", $username, $expdate);				echo '<script type="text/javascript">';		echo 'alert("' . $_COOKIE["username"] . $id . '");';////////////////Temporary Debugging element		echo '</script>';

Hope this helps.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...