dennismonsewicz Posted October 1, 2008 Share Posted October 1, 2008 I am wanting to loop through a SQL DB and loop through the rows and where ever there is a 1 display something. Whats the best way of doing that? The DB table column names correspond to checkboxes... anyway to grab these and spit them out? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 1, 2008 Share Posted October 1, 2008 The point of databases and queries is to select just the data you are interested in. You would generally use a WHERE clause in a query that would match the rows that contain the value you want. What does your data look like and what queries have you tried? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 I reckon I am just trying to come up with a easier solution than having to write a million if statements for this... For example: if($results->pm1 == 1) { $checked = "checked" } pm1 is a DB column name Quote Link to comment Share on other sites More sharing options...
Maq Posted October 1, 2008 Share Posted October 1, 2008 //first you need to connect $host="localhost"; $user_name="username"; $password="password"; $db="your_db"; $connection = mysql_connect($host, $user_name, $password) or die ("Unable to connect!"); mysql_select_db($db) or die ("Unable to select database!"); $results = mysql_query("SELECT pm1 FROM table_name WHERE something = something;"); while ($data = mysql_fetch_array($results)) { echo $data['pm1']; } Does this help? Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 Here is something I am playing with: <?php include "includes/sql.php"; $query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'") or die(mysql_query()); $results = mysql_fetch_object($query); $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $results_qry = mysql_fetch_array($checkbox_qry); foreach($results_qry[] as $key => $val) { echo $key . "=" $val; } ?> I know how to do the suggestion by Maq... thanks though! Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 I found some errors in the code I was testing Updated Code: <?php include "includes/sql.php"; $id = "24"; $query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'") or die(mysql_error()); $results = mysql_fetch_object($query); $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $results_qry = mysql_fetch_array($checkbox_qry); foreach($results_qry[] as $key => $val) { echo $key . "=" $val; } Quote Link to comment Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 ok I now have all of the field names displaying, but I need to delete the first two out of the list how would I do this? updated code: <?php /*ini_set ("display_errors", "1"); error_reporting(E_ALL);*/ include "includes/sql.php"; $id = 24; $query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'")or die(mysql_error()); $results = mysql_fetch_object($query); $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error()); $field = mysql_num_fields($checkbox_qry); for($i = 0; $i < $field; $i++) { $names = mysql_field_name($checkbox_qry, $i); $title .= trim($names) . "<br />"; } echo $title; ?> Quote Link to comment 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.