pcw Posted March 15, 2011 Share Posted March 15, 2011 Hi, I have this script that returns the entire table including column names and the row data. However I only want certain column names and the corresponding data to be returned. <?php include_once("data/mysql.php"); $mysqlPassword = (base64_decode($mysqlpword)); $db = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die ("Error connecting to database"); mysql_select_db("$dbname", $db) or die ("An error occured when connecting to database"); // sending query $result = mysql_query("SELECT * FROM members"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>Table: members</h1>"; echo "<table border='1'><tr>"; for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>\n"; while($row = mysql_fetch_row($result)) { echo "<tr>"; foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_query($result); ?> Any help is always appreciated Quote Link to comment https://forums.phpfreaks.com/topic/230713-only-show-certain-columns-and-column-data/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 15, 2011 Share Posted March 15, 2011 Just SELECT the columns you want in your query statement. Quote Link to comment https://forums.phpfreaks.com/topic/230713-only-show-certain-columns-and-column-data/#findComment-1187812 Share on other sites More sharing options...
pcw Posted March 15, 2011 Author Share Posted March 15, 2011 Hi, thanks for your reply. I have however made a change to the code as I needed to add two links to the end of the table row on the page. <?php include_once("data/mysql.php"); $mysqlPassword = (base64_decode($mysqlpword)); $db = mysql_connect("$localhost", "$mysqlusername", "$mysqlPassword") or die ("Error connecting to database"); mysql_select_db("$dbname", $db) or die ("An error occured when connecting to database"); # $result = mysql_query("SELECT * FROM members"); # while($row = mysql_fetch_assoc($result)){ # echo "<tr><td>".$row['username']."</td>, <td> ".$row['firstname']"</td>, <td> ".$row['register']."</td>, <td>".$row['approved'].", <td>'<a href=userlogin.php?username=$username&password=$password>Reset</a>'</td>, <td>'<a href=userdelete.php?username=$username&password=$password>Delete</a>'</td> <br/>"; # } ?> But now I get this error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/tropicsb/public_html/MemberSiteMaker/templates/admin/manage.php on line 33 how should I have written this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/230713-only-show-certain-columns-and-column-data/#findComment-1187818 Share on other sites More sharing options...
fenway Posted March 15, 2011 Share Posted March 15, 2011 This really isn't the correct place for php parsing errors. Quote Link to comment https://forums.phpfreaks.com/topic/230713-only-show-certain-columns-and-column-data/#findComment-1187910 Share on other sites More sharing options...
JamieB Posted March 15, 2011 Share Posted March 15, 2011 You're missing a . after firstname .$row['firstname'] should be .$row['firstname']. Quote Link to comment https://forums.phpfreaks.com/topic/230713-only-show-certain-columns-and-column-data/#findComment-1187917 Share on other sites More sharing options...
pcw Posted March 15, 2011 Author Share Posted March 15, 2011 Many thanks JamieB :-) Quote Link to comment https://forums.phpfreaks.com/topic/230713-only-show-certain-columns-and-column-data/#findComment-1187926 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.