![]() |
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 |
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. |
I must admit I've never used PHP, but..
Quote:
Also, notice $newarray in the while, and Quote:
|
Quote:
The while statement essentially says "while there are still rows to read, do this" It's more intuitive than literal. |
All times are GMT -8. The time now is 03:21 PM. |
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