11-03-2005, 09:58 AM | #1 (permalink) |
Registered User
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 |
11-03-2005, 03:10 PM | #2 (permalink) |
Darth Papa
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. |
11-03-2005, 04:15 PM | #3 (permalink) | ||
Lover - Protector - Teacher
Location: Seattle, WA
|
I must admit I've never used PHP, but..
Quote:
Also, notice $newarray in the while, and Quote:
__________________
"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 |
||
11-03-2005, 05:24 PM | #4 (permalink) | |
Registered User
Location: Right Here
|
Quote:
The while statement essentially says "while there are still rows to read, do this" It's more intuitive than literal. |
|
Tags |
php or mysql, select, statement, working |
|
|