Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   php/mysql SELECT statement not working (https://thetfp.com/tfp/tilted-technology/96985-php-mysql-select-statement-not-working.html)

frogza 11-03-2005 09:58 AM

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

ratbastid 11-03-2005 03:10 PM

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.

Jinn 11-03-2005 04:15 PM

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'];

frogza 11-03-2005 05:24 PM

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.


All times are GMT -8. The time now is 04:02 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, 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