Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 11-03-2005, 09:58 AM   #1 (permalink)
Registered User
 
frogza's Avatar
 
Location: Right Here
php/mysql SELECT statement not working

I have set up a table in mysql and then wrote a script to show all the entries to the table. I'm using a while loop to get the info assign it to an array then echo the results. Here is the code:

<?php
$conn = mysql_connect ("localhost", "me", "456");
mysql_select_db("Login", $conn);
$sql = "Select * FROM Users";
$result = mysql_query($sql, $conn) or die(mysqlerror());
while ($newarray = mysql_fetch_array($result)) {
$name = $newArray['userName'];
$pass = $newArray['passWord'];
echo "Name $name<br>";
echo "Password $pass<br>";
}
?>

When I run the script it prints "Name Password" the correct number of times (once for each record) but omits the values for $name and $pass. I've checked and double checked my column names to make sure they are spelled right with the right case and they are good.

Can anyone help me with this, I'm sure it's just a stupid little error but I'm stuck. Thanks in advance
frogza is offline  
Old 11-03-2005, 03:10 PM   #2 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
You don't have any error checking in your script at all. I like to sanity check things like database connections--print any errors I receive, double check that we actually connected right, etc.

But I think this is simpler than that. When you did "$newarray = mysql_fetch_array($result)", you created an array in $newarray that's indexed NUMERICALLY. You don't really want a normal array here, you want an associative array, and to create that you'd use mysql_fetch_assoc($result).

You could also access the elements of $newarray by the numerical index, of course.
ratbastid is offline  
Old 11-03-2005, 04:15 PM   #3 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Location: Seattle, WA
I must admit I've never used PHP, but..

Quote:
while ($newarray = mysql_fetch_array($result)) {
What is the conditional, and what is the iterator? Assuming that whole thing is the condition, what causes it to move beyond the first array element?

Also, notice $newarray in the while, and

Quote:
$name = $newArray['userName'];
$pass = $newArray['passWord'];
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel
Jinn is offline  
Old 11-03-2005, 05:24 PM   #4 (permalink)
Registered User
 
frogza's Avatar
 
Location: Right Here
Quote:
Originally Posted by JinnKai
I must admit I've never used PHP, but..



What is the conditional, and what is the iterator? Assuming that whole thing is the condition, what causes it to move beyond the first array element?

Also, notice $newarray in the while, and
Thanks, I knew it would be something pretty small and stupid.

The while statement essentially says "while there are still rows to read, do this" It's more intuitive than literal.
frogza is offline  
 

Tags
php or mysql, select, statement, working


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:49 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73