Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Issue??

Status
Not open for further replies.

ndog4ever

MIS
Feb 6, 2002
92
US
I am trying to make a simple array based off what ID a user has for a login. I know the variable is getting passed correctly because I tested it with a print statement. However, I am stuck on why i cannot get my array to work. I have tried printing the $username variable and it's blank. I am not sure what else to do or try? I appreciate any help.


$db = @mysql_select_db($db_name, $connection) or die("Couldn't Select Database.");

$sql = "select username, password from $table_name where username = \"$id\"
";

$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");

while ($row = mysql_fetch_array($result)) {

$username = $row["username"];
$password = $row['password'];

}

?>
 
do an echo of the query and run it in mysql.
Your code is ok, so the problem can be a problem with the query (table_name) or about the connection. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i did an echo of the result and this is what i got....

Resource id #2


I know that the record "dan" has an id number of 2.

is that what it should look like?

 
i said the query

but if you get and resource id in the result i don't get why you don't get the values

just try one thing

instead of the while,

list($username,$password)=mysql_fetch_row($result);

echo "USERNAME: $username/$password";

and check what you gen on the screen Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i commented out the while loop and i get the following now

---->>> USERNAME: /Resource id #2

$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");

list($username,$password)=mysql_fetch_row($result);
echo "USERNAME: $username/$password";

It seems like to me that the array function is screwed up, I am new to php and mysql so I don't know. Do you have any other suggestions, thanks for all your help so far.

Nate

 
first you used singel quotes since doubles are normal I believe on
$password = $row['password'];

I check on the login if the username and the password give a result in the query like

$query="SELECT name, password FROM users where name like '$name' and password like '$password'";
$rs=mysql_query($query,$conn);
$aantal=mysql_num_rows($rs);

and then I use a bit of javascript since I'm not quite experienced with php how to do that part but I continue if the number of rows =1 and otherwise go back

<script language=&quot;javascript&quot;>
function move(url)
{
window.location.replace(url);
}
<?
if ($aantal <> 1){
print &quot;move('login.php3');&quot;;
}
else {
print &quot;move('cookie.php3?loginnaam=$naam&loginid=$lluid&sleutel=$sleutel');&quot;;
}

print (&quot;
</script>
 
hos:
you can use single, double or none quotes while refering to an array index. $array['username'], $array[&quot;username&quot;], $array[username] referes to the same thing.


i think that $table_name may not be set, and so, to check it, do:

echo $sql;

and see if the sql query is properly well. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I'd change the sql query slightly:
$sql = &quot;select username, password from '$table_name' where username = '$id'&quot;; ***************************************
Party on, dudes!
[cannon]
 
there will be an error ...

you cannot use the quotes in the table name.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Anikin, you are right about the table name quotes, my mistake.
I still think the connect looks odd tho.
------------------------------------------
here's one of mine:
$db = mysql_connect(&quot;server_ip&quot;,&quot;user&quot;,&quot;password&quot;)or die ('cannot connect to server');

mysql_select_db(my_db,$db) or die ('cannot select database');

$result = mysql_query(&quot;select * from my_table&quot;,$db) or die ('Cannot select from that table.');

***************************************
Party on, dudes!
[cannon]
 
i'm not user to mysql. I dont devellop in mysql for more than one year. I don't remember the order of functions, bu evetything seems ok in his code. It must be something strange. My odds goes to that $table_name. I don't see if he setted the value of the var before. he didn't show in the full code.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
this is my code from the beginning on the page where the person would select the name of the contact to update or look at.

<?
$db_name = &quot;test&quot;;
$table_name = &quot;contacts&quot;;
$connection = @mysql_connect(&quot;localhost&quot;, &quot;nate&quot;, &quot;nate&quot;) or die (&quot;Couldn't Connect.&quot;);
$db = @mysql_select_db($db_name, $connection) or die(&quot;Couldn't Select Database.&quot;);
$sql = &quot;select id, username, password from $table_name order by username&quot;;
$result = @mysql_query($sql, $connection) or die (&quot;Couldn't execute query.&quot;);

while ($row = mysql_fetch_array($result)) {

$id = $row['id'];
$username = $row['username'];
$password = $row['password'];

$option_block .= &quot;<option value =\&quot;$id\&quot;>$username </option>&quot;;

}


this is the output i am getting when i do an echo $sql:

select username, password from contacts where username = &quot;2&quot;


I am guessing it's passing the variable right, here is my sample data for my contacts table:


mysql> select * from contacts;
+----+----------+------------------+
| id | username | password |
+----+----------+------------------+
| 1 | nate | 5e8b7ec24bbc800a |
| 2 | dan | 7b08d75b22acbf46 |
+----+----------+------------------+

Well thanks for all the help, this is driving me crazy. If you have any more ideas I would appreciate your posts.

thanks


 
of course you are not getting nothing.

you are selecting where username is equal to 2 and not the id.

change the select to

where id=2

but the program you show, you are concatenating only options. For them appear, you must put them inside a select.

And in that code, you are not echoing nothing to the screen. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
the last code i posted was the code for the page that the user would select a username:

the option block code for that is :

$option_block .= &quot;<option value =\&quot;$id\&quot;>$username </option>&quot;;

the following php code is the array that i am trying to display back to the screen
based on the user's ID

$sql = &quot;select username, password from $table_name where username = \&quot;$id\&quot;
&quot;;
$result = @mysql_query($sql, $connection) or die(&quot;Couldn't execute query.&quot;);
while ($row = mysql_fetch_array($result)) {
$username = $row[&quot;username&quot;];
$password = $row['password'];
}

when i echo the above the sql statement i get this:

select username, password from contacts where username = &quot;2&quot;

Contacts is the correct table. when i try to echo $username or $password, nothing shows up on the screen. sorry for the confusion earlier.
 
ive figured it out, thanks for all your help, i feel like an idiot, but i guess staring at a computer screen all day can drive you crazy. I had to set my select statement to :

where id = \$id\;

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top