Displaying specific table cells from MYSQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Displaying specific table cells from MYSQL database

Post by The450Man »

I am trying to display specific table cells. More importantly a specific row. This row is dynamic and i must be able to easily change the row number. It would be great if i could tie it to a variable at the beginning of the script.

The row must then be displayed in a table that contains two rows. The top row is the lable for the content. The bottom row is the data pulled from the mysql database.

I ripped this from another place, it works, just dosnt do what i want.

Code: Select all

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'lptm42b';

$database = 'sphinx';
$table = 'spheres';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

no one? i would of thought this would be easy.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying specific table cells from MYSQL database

Post by Celauran »

The450Man wrote:no one? i would of thought this would be easy.
It is, but you've given us no information to work with. How is this specific row of yours to be identified? Can't build a query without that info.
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

Celauran wrote:
The450Man wrote:no one? i would of thought this would be easy.
It is, but you've given us no information to work with. How is this specific row of yours to be identified? Can't build a query without that info.
The specific row will be identified by the row count.

Example, i want the information in row number 3.

Set a variable to 3 (or 2 if the first row is considered row 0) so the script knows i want row 3.

Now that the script knows the row, i must identify what table cells i want from that row in some format.

Example,

name: cell1 lastname: cell2 age: cell3
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Displaying specific table cells from MYSQL database

Post by Benjamin »

You're looking for mysql_fetch_assoc()

http://us3.php.net/manual/en/function.m ... -assoc.php
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

thanks Benjamin.

I really dont know php so im not sure how to implement it. I understand logic, just not this language. Im used to autoit kind of stuff.

Im slowly starting to grasp it.

Anyone feel like giving me an example?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Displaying specific table cells from MYSQL database

Post by Benjamin »

There's examples on the page I linked to.
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

I did look at them and im very confused.

Regarding:

while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}

So this is showing the columns userid, fullname, userstatus for each row?

For my purposes id have to get rid of the "while", as i dont want it to loop; i just want the contents of a single row.

How can i do this? eliminating the "while would remove mysql_fetch_assoc($result). sorry im still grasping php.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying specific table cells from MYSQL database

Post by Celauran »

If you know you'll only be fetching a single row, there's no need for a while loop.

Code: Select all

$query = "SELECT foo, bar FROM table_name WHERE id = whatever";
list($foo, $bar) = mysql_fetch_row(mysql_query($query));
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

thanks Celauran,

How can i set the row of which to extract the data from?
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

One script was using $cell.

Bump
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Displaying specific table cells from MYSQL database

Post by califdon »

I realize this is going to sound academic, but in the relational database world, there is no significance to a particular row. You probably think that the 3rd row IS the 3rd row, but that's not necessarily true in a relational database. The point is that rows (by the way, NOT "cells"--databases do NOT have cells) must be identified by the data that is contained in them, normally by the value of their primary key. So it is essentially impossible to answer your question without fully understanding the application you are working on. But your understanding of how a relational database works needs to be corrected.
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

califdon wrote:I realize this is going to sound academic, but in the relational database world, there is no significance to a particular row. You probably think that the 3rd row IS the 3rd row, but that's not necessarily true in a relational database. The point is that rows (by the way, NOT "cells"--databases do NOT have cells) must be identified by the data that is contained in them, normally by the value of their primary key. So it is essentially impossible to answer your question without fully understanding the application you are working on. But your understanding of how a relational database works needs to be corrected.
I like academic responses, learning is good.

Im used to working with software that generates tables from csv files, so i work with coordinates like row/column.

To clear some confusion up. My mysql database table looks like this:

email-------------------clicks---------month to day clicks

11111@dfads.com----102------------897
22222@dfads.com-----87------------478
33333@dfads.com-----58-------------687

On one html page, id call 11111 for the email 11111@dfads.com

On that page i want to display (i dont want to display any other content):

Email: 11111@dfads.com Clicks: 102 Month to date clicks: 897

Id to the same thing for each row
Last edited by The450Man on Wed May 23, 2012 2:08 pm, edited 1 time in total.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Displaying specific table cells from MYSQL database

Post by mikosiko »

I ripped this from another place, it works, just dosnt do what i want.
well the code that you posted in your very first post has almost all the ingredients to do what you apparently want... is just a matter for you to start coding and experimenting with it and ask again if you have problems... what have you tried? (show your code and errors... btw: "It doesn't work" is not an error and doesnt help to help you).
The450Man
Forum Newbie
Posts: 14
Joined: Sat May 19, 2012 9:40 pm

Re: Displaying specific table cells from MYSQL database

Post by The450Man »

I have an idea, i just have no idea how to specify which row i want.

Regarding my database:

email-------------------clicks---------month to day clicks

11111@dfads.com----102------------897
22222@dfads.com-----87------------478
33333@dfads.com-----58-------------687

if i specify the "email" row as the primary key, would i call the row by the email value?

I also dont know how to specify the values.

Im reading up, but i cant find any examples of how im trying to do things. Its pretty sad, i took a course in php for my web design degree yet i seem to know very little about php.
Post Reply