Jump to content

SQL DB While loop error


jasonxxx102

Recommended Posts

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>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.