Jump to content

Output Data from MYSQL...HELP!!!!


kartikayg

Recommended Posts

Hi ..i m new to php and need some help. What i am trying here is to sort out the enteries in MySQL database by state. I also have alternate row color when the table prints. WHen i try this what i see is an empty table with no values..

Please someone help me...

Here is my code:

$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$state=$_POST['state'];
$sql="SELECT * FROM $tbl_name WHERE state='$state'";

//$sql = 'SELECT * '
//. ' FROM `nacontact` '
// . ' WHERE 1 AND `state` = \'$stat\'';
$result=mysql_query($sql);

// Define $color=1
$color="1";

while($rows=mysql_fetch_array($result)){
// If $color==1 table row color = #FFC600
if($color==1){ ?>
<tr class="tab_12">
<?php
echo "<td> ".$rows['fname']." ".$rows['lname']."</td>
<td>".$rows['city']."" ?> , <?php echo" ".$rows['state']."</td>
<td>".$rows['email']."</td>
<td>".$rows['phone']."</td>
<td>".$rows['country']."</td>" ?>
</tr>
<?php // Set $color==2, for switching to other color
$color="2";
}

// When $color not equal 1, use this table row color
else { ?>

<tr class="tab_13">

<?php echo "<td>".$rows['fname']." ".$rows['lname']."</td>
<td>".$rows['city']."" ?> , <?php echo" ".$rows['state']."</td>
<td>".$rows['email']."</td>
<td>".$rows['phone']."</td>
<td>".$rows['country']."</td> " ?>
</tr>
<?php
// Set $color back to 1
$color="1";
}

}
mysql_close();
?>
Link to comment
Share on other sites

You have passed the code within the <? and ?> right because i dont see it in your script before .

[code]mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$state=$_POST['state'];[/code]


Also,

change $result as follows to pinpoint errors.

[code]$result=mysql_query($sql) or die (mysql_error());[/code]


And finally when you copy codes on the forum wrap it the way i have done above - easier to read.
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.