Jump to content

[SOLVED] Help php/mysql / table / and nothing shows!!


Recommended Posts

Can someone take a look at my code and tell me why nothing shows in the table... and why the code at the bottom displays everything twice... ?!!?!?!

 

How do I show individual variables from my database?1?!

 

Right now I'm not trying to do anything except show my records from my database!!!!

 

Many thanks in advance.

Excel209

 


include 'db.php';  //This connects to my server & database


$mysql = mysql_query("SELECT * FROM users"); 
$result = mysql_query($mysql);
$numofrows = mysql_num_rows($result);

echo "<TABLE BORDER=\"1\" width=\"24%\">\n";
echo "<TR bgcolor=\"lightblue\">
<TD>Username</TD>
<TD>first</TD>
<TD>last</TD></TR>\n";
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); 
if($i % 2) {
	echo "<TR bgcolor=\"#CCFFCC\">\n";
} else { 
	echo "<TR bgcolor=\"#BFD8BC\">\n";
}
echo "<TD>".$row['username']."</TD>
<TD>".$row['first_name']."</TD>
<TD>".$row['last_name']."</TD>
\n";
echo "</TR>\n";
}
echo "</TABLE>\n";	

// This is me trying to get any type of result to show...

$query = "SELECT * FROM users";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result)){
foreach ($line as $value){
    	echo $value;
    }
}

try something like this

<?php
include 'db.php';  //This connects to my server & database


$mysql = mysql_query("SELECT * FROM users"); 
$result = mysql_query($mysql) or die ("Error in query" . mysql_error());
//always check for errors in the query just in case
echo "<TABLE BORDER=\"1\" width=\"24%\">\n";
echo "<TR bgcolor=\"lightblue\">
<TD>Username</TD>
<TD>first</TD>
<TD>last</TD></TR>\n";
$count = 1; //start a different counter set to 1
while($row = mysql_fetch_array($result))
{
if($count % 2) { //check to see if its directly divisible by 2
	echo "<TR bgcolor=\"#CCFFCC\">\n";
} else { 
	echo "<TR bgcolor=\"#BFD8BC\">\n";
}
echo "<TD>".$row['username']."</TD>
<TD>".$row['first_name']."</TD>
<TD>".$row['last_name']."</TD>
\n";
echo "</TR>\n";
$count++;	//increment the counter
}
echo "</TABLE>\n";	

// This is me trying to get any type of result to show...

$query = "SELECT * FROM users";
$result = mysql_query($query) or die ("Error in query" . mysql_error());
//again check for any errors , if there was one above it will be repeated here too - same query
while ($line = mysql_fetch_array($result)){
foreach ($line as $value){
    	echo $value;
    }
}
?>

If I use this code I get an error message... "Error in query".. "blah..blah...blah.."

 

$mysql = mysql_query("SELECT * FROM users"); 
$result = mysql_query($mysql) or die ("Error in query" . mysql_error());

 

But if I use this code it works :-

 

$query = "SELECT * FROM users";
$result = mysql_query($query) or die ("Error in query" . mysql_error());

 

Is this strange of what?!  But thanks for the code... the table now works great!!

 

Excl209.

It's because in this code:

 

<?php

$mysql = mysql_query("SELECT * FROM users"); 
$result = mysql_query($mysql) or die ("Error in query" . mysql_error());

?>

 

You are putting two mysql_query()'s around one query. You would have to take the mysql_query out of the $mysql line, then it would work...but it would also be identical to the working code you posted above...so that is the only difference.

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.