Jump to content

Only show certain columns and column data


pcw

Recommended Posts

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

Link to comment
Share on other sites

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

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.