How to display certain info in a database table php and data

Codes here !

Moderators: macek, egami, gesf

bumpn
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Oct 04, 2012 8:33 am

How to display certain info in a database table php and data

Postby bumpn » Thu Oct 04, 2012 8:40 am

for use with this question lets say I have a database with 20 rows and 20 columns.columns have names and rows have numbers. I want to display on a certain area on page text from row 5 column 5. I can display the entire table using php but i only want certain info in certain places.
how do i do that plz help!!

Additional Details
the first column is id# and in php MyAdmin its set to ascending 1,2,3,4,5,6,7 ect.

is the php code were you just say col 5 row 5 using echo or print to display the info?

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: How to display certain info in a database table php and

Postby egami » Thu Oct 04, 2012 2:11 pm

Code: Select all

if ($schoolwork)
{
  if (
$show_me_stuff) { 
    if (
NULL)
    {
      die(
"Goodbye.");
    }
  } else { 
    
$will_help TRUE;
  }
}
 

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: How to display certain info in a database table php and

Postby seandisanti » Thu Oct 04, 2012 3:31 pm

egami wrote:

Code: Select all


if 
($schoolwork)
{
  if ($show_me_stuff) { 
    if 
(NULL)
    {
      die("Goodbye.");
    }
  } else { 
    $will_help 
= TRUE;
  }
}
  

:D Love it

***edit***
sorry, i guess i should post something too. maybe this would work...

Code: Select all

<?php
   echo (isset($proof_you_tried)) ? $helpful_output : $mockery;
?>

User avatar
engrmudasirmalik
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Jan 11, 2012 11:39 am
Location: Bahawalpur Pakistan
Contact:

Re: How to display certain info in a database table php and

Postby engrmudasirmalik » Thu Oct 04, 2012 10:06 pm

I have guess about what ever you want to !
Just simple is to run the following query !
This is how i understand your question

Code: Select all

$query   = mysql_query("SELECT * FROM tablename ORDER BY id ASC;");
while($row      = mysql_fetch_array($query))
{
echo   $row['name'];
}

bumpn
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Oct 04, 2012 8:33 am

Re: How to display certain info in a database table php and

Postby bumpn » Fri Oct 05, 2012 5:21 am

egami wrote:

Code: Select all


if 
($schoolwork)
{
  if ($show_me_stuff) { 
    if 
(NULL)
    {
      die("Goodbye.");
    }
  } else { 
    $will_help 
= TRUE;
  }
}
 

came here asking for help. if you want to be funny go to a comedy forum, otherwise go FUCK yourself

bumpn
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Oct 04, 2012 8:33 am

Re: How to display certain info in a database table php and

Postby bumpn » Fri Oct 05, 2012 5:56 am

I know I can use the code to post to the table and I have figured that out. but I cant figure out how to pull specific data from the table to display on the site. from the code the last guy sent I can see how it would pull from a specific column but how would it tell what row to pull from?

I have the code to connect to the database separate from the code ill be using to display the database data. using the php code

Code: Select all

<?php
include ('css/connect.php');
?>

here is my connect code and i think i need an array but im not sure

Code: Select all

<?php
// connect to server
$con = mysql_connect("localhost","database","pass");
if (!$con)
  {
  die('ARE YOU LOST: ' . mysql_error());
  }
echo "Connected to MySQL<br />";
// connect to database
mysql_select_db("database") or die(mysql_error());
echo "Connected to Database<br />";
// connect to table
$result = mysql_query( "SELECT * FROM table" ) or die("SELECT Error: ".mysql_error());
echo "Connected to table<br />";
// Variables

$variable=mysql_result($result,$i,"fieldname");


?>

here is the code I use to display the whole table. I can move the code to each div to display the table there.

Code: Select all

   <?php                                                                       
 $num_rows = mysql_num_rows($result);
print "<table width=800 border=5>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=7/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";                                           
?>.

as a side note I know html,css but i just started learning php. I had a question and went to a "popular" php forum and instead of getting help I got ridiculed and made fun of?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: How to display certain info in a database table php and

Postby seandisanti » Fri Oct 05, 2012 8:00 am

The ridicule was because of asking for help and not showing any evidence at first that you'd put any effort into solving the problem yourself. Also telling a mod to fuck off in response to levity is not a way to improve on that first impression. Posting code (like you have now) helps. Ok, that said... when you're grabbing data from a database, you select your columns in the SELECT statement, and you isolate particular rows with your WHERE statement.

So your query for the example given would be:


Code: Select all

SELECT column5 FROM tablename WHERE id=5


A couple other things to look at though, that may make your life a little easier in the long run. Check out mysqli_ functions, they're pretty much the same as mysql_ functions, but faster and more reliable. Also PDO is an even better direction once you get your confidence up to try it.

Instead of doing fetch_row and working line by line, you can fetch_array($result) and then step through the array.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: How to display certain info in a database table php and

Postby egami » Fri Oct 05, 2012 11:30 am

A Basic Loop:

Code: Select all

<?php 
$link 
mysql_connect('localhost','username','password'); // Change this to your credentials
$db mysql_select_db('Your_DB'); // Change this to your DB..

$table          'MyTableName'// change this to your table name..
$order_field  'FieldNameToOrderBy';

$query "SELECT * FROM $table ORDER BY $order_field ";
$result mysql_query($query);
if (
mysql_num_rows($result) > 0) { 
  while (
$row mysql_fetch_assoc($result)) { 
    
$array[] = $row;
  }
  echo 
'
  <div>
  <table>
  <thead>
    <tr>
       <th>Field1..</th>
        ... repeat for the fields..
    </tr>
   </thead>
   <tbody>
   '
;
  foreach(
$array as $k => $v) { 
    echo 
'
    <tr>
       <td>'
.$v['field1'].'</td>
       ...repeat the fields...
    </tr>
    '
;
  }
  echo 
'
  </tbody>
   </table>
   '
;
}
 

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: How to display certain info in a database table php and

Postby egami » Tue Oct 09, 2012 7:06 am

Well, I guess either he figured it out, or is too but hurt. Either way, good luck.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: How to display certain info in a database table php and

Postby seandisanti » Tue Oct 09, 2012 8:29 am

Yeah, it seems like a lot of people just disappear as soon as they get their solution, no feedback on if it worked etc. I know that happens on all forums, but I don't think i've seen more than a couple threads total where the OP says "that worked, thanks"

bumpn
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Oct 04, 2012 8:33 am

Re: How to display certain info in a database table php and

Postby bumpn » Tue Oct 23, 2012 1:30 pm

no ive been busy with android app this has been put on hold

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: How to display certain info in a database table php and

Postby seandisanti » Tue Oct 23, 2012 2:40 pm

does your posting mean that its been revived and you're ready to try suggestions?


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest

cron