jasonxxx102 Posted February 7, 2014 Share Posted February 7, 2014 Ok, so I created a test database on my server, created 1 table with 1 row in it and the row is populated with values.I can connect to the database without error, but when I try to put in a while loop to print the results I get nothing but a blank screen.I tried placing some debug code in the while loop to see where its getting hung up but the while loop doesn't want to run at all. I placed an echo directly outside of the loop and the debug text is displayed fine but anything I put in the loop doesn't work.Can anyone help me out?Here is my code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $dbuser = "webestat_testdb"; $dbpass = "password"; $dbname = "webestat_testdb"; $server = "localhost"; $conn = mysql_connect($server, $dbuser, $dbpass); if (!$conn) { echo "Failed to connect to db" . mysql_error(); } $selectdb = mysql_select_db('webestat_testdb', $conn); $sql = "SELECT * FROM 4000Series"; $results = mysql_query($conn, $sql); while($row = mysql_fetch_assoc($results)) { echo "test"; echo $row['gpu']. " " .$row['khs']. " " .$row['usd']; echo "<br>"; } mysql_close($conn); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/286021-sql-db-while-loop-error/ Share on other sites More sharing options...
requinix Posted February 7, 2014 Share Posted February 7, 2014 Put that thought on hold: if you're starting out then you need to use the PDO or mysqli extension. Not the mysql extension and its mysql_* functions: it's been deprecated for various reasons including it being slow and not as feature-rich as the others. The cause of your problem is how you're calling mysql_query(), but that's moot since you'll be changing the database code. Quote Link to comment https://forums.phpfreaks.com/topic/286021-sql-db-while-loop-error/#findComment-1468087 Share on other sites More sharing options...
.josh Posted February 7, 2014 Share Posted February 7, 2014 You have your arguments in the wrong order in mysql_query but as requinix mentioned, you should use PDO or mysqli instead. Quote Link to comment https://forums.phpfreaks.com/topic/286021-sql-db-while-loop-error/#findComment-1468091 Share on other sites More sharing options...
jasonxxx102 Posted February 7, 2014 Author Share Posted February 7, 2014 Thanks! I switched over to mysqli and changed up my code slightly and now I can display the values in my row. Can anyone explain to me why each value has 2 entries in the array though? The page displays this now: Array ( [0] => 1 [id] => 1 [1] => 4350 [gpu] => 4350 [2] => 10.000000 [khs] => 10.000000 [3] => 100.000000 [usd] => 100.000000 [4] => 20.000000 [watts] => 20.000000 [5] => 0.100000 [khd] => 0.100000 [6] => 0.500000 [khw] => 0.500000 [7] => 0.003000 [ltcday] => 0.003000 [8] => 0.010000 [usdday] => 0.010000 [9] => 9823.000000 [payoff] => 9823.000000 ) Sorry if these questions are very basic I'm very new to PHP. Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/286021-sql-db-while-loop-error/#findComment-1468092 Share on other sites More sharing options...
Barand Posted February 7, 2014 Share Posted February 7, 2014 If you use mysqli_fetch_row or fetch_assoc you will only get the values once in the array. Fetch_array() gets the values with both a numeric index and a columnname index unless you specify which with an extra parameter Quote Link to comment https://forums.phpfreaks.com/topic/286021-sql-db-while-loop-error/#findComment-1468093 Share on other sites More sharing options...
gevensen Posted February 8, 2014 Share Posted February 8, 2014 try this <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $debug_message.="START DEBUG<br/>"; $dbuser = "webestat_testdb"; $dbpass = "password"; $dbname = "webestat_testdb"; $server = "localhost"; $conn = mysqli_connect($servername, $dbuser, $dbpass, $dbname) or $debug_message.=mysqli_connect_error($conn)."</br>"; if (!mysqli_error($conn)) { $debug_message.= "Failed to connect to db" . mysqli_error($conn); } else { $sql = "SELECT * FROM 4000Series"; $results = mysqli_query($conn, $sql); while($row = mysqli_fetch_assoc($results)) { $debug_message.= "test"; $debug_message.= $row['gpu']. " " .$row['khs']. " " .$row['usd']; $debug_message.= "<br>"; } mysqli_close($conn); } echo $debug_message; ?> </body> </html> Ok, so I created a test database on my server, created 1 table with 1 row in it and the row is populated with values.I can connect to the database without error, but when I try to put in a while loop to print the results I get nothing but a blank screen.I tried placing some debug code in the while loop to see where its getting hung up but the while loop doesn't want to run at all. I placed an echo directly outside of the loop and the debug text is displayed fine but anything I put in the loop doesn't work.Can anyone help me out?Here is my code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $dbuser = "webestat_testdb"; $dbpass = "password"; $dbname = "webestat_testdb"; $server = "localhost"; $conn = mysql_connect($server, $dbuser, $dbpass); if (!$conn) { echo "Failed to connect to db" . mysql_error(); } $selectdb = mysql_select_db('webestat_testdb', $conn); $sql = "SELECT * FROM 4000Series"; $results = mysql_query($conn, $sql); while($row = mysql_fetch_assoc($results)) { echo "test"; echo $row['gpu']. " " .$row['khs']. " " .$row['usd']; echo "<br>"; } mysql_close($conn); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/286021-sql-db-while-loop-error/#findComment-1468213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.