Page 1 of 2
Displaying specific table cells from MYSQL database
Posted: Sat May 19, 2012 9:45 pm
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>
Re: Displaying specific table cells from MYSQL database
Posted: Sun May 20, 2012 8:41 pm
by The450Man
no one? i would of thought this would be easy.
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 5:06 am
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.
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 12:28 pm
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
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 12:56 pm
by Benjamin
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 2:24 pm
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?
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 2:55 pm
by Benjamin
There's examples on the page I linked to.
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 3:34 pm
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.
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 3:53 pm
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));
Re: Displaying specific table cells from MYSQL database
Posted: Mon May 21, 2012 4:14 pm
by The450Man
thanks Celauran,
How can i set the row of which to extract the data from?
Re: Displaying specific table cells from MYSQL database
Posted: Tue May 22, 2012 10:08 pm
by The450Man
One script was using $cell.
Bump
Re: Displaying specific table cells from MYSQL database
Posted: Tue May 22, 2012 11:27 pm
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.
Re: Displaying specific table cells from MYSQL database
Posted: Wed May 23, 2012 12:07 pm
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
Re: Displaying specific table cells from MYSQL database
Posted: Wed May 23, 2012 1:46 pm
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).
Re: Displaying specific table cells from MYSQL database
Posted: Wed May 23, 2012 2:15 pm
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.