How to Print MySql table contents
Posted by: sylverock (---.dllstx.fios.verizon.net)
Date: February 13, 2008 05:34AM

Hellow Wamper's:

How do I display MySql table contents in the browser. - I AM SEARCHING THE MySql PRINT/ECHO COMMAND TO DISPLAY THE SELECT QUERY CONTENTS

This is how my code looks like:

<html>
<body>
<?php


//include configuration file
include('config.php');

// open database connection
$connection = mysql_connect($host, $user, $pass) or die('ERROR: Unable to
connect!');

// select database
mysql_select_db($db) or die('ERROR: Unable to select database!');

// generate and execute query


$query = "SELECT name, owner, species FROM pet";

$NumRows = mysql_query($query) or die("ERROR: $query.".mysql_error());

echo "Total number of rows in the table are ". mysql_num_rows($NumRows);

//The total number of rows in the table are displayed correctly- now I want to display the results of the Select statement. WHAT CAN I DO - Your suggestions will be much appreciated

// close connection
mysql_close($connection);



?>
</body>
</html>

Options: ReplyQuote
Re: How to Print MySql table contents
Posted by: bloody velvet (---.nyc.res.rr.com)
Date: February 13, 2008 07:47AM

You mean something like:

<?php

//setup db connection
$usr = "username";
$pwd = "yourpass";
$db = "yourdatabase";
$host = "localhost";

$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"winking smiley; }

$SQL = "SELECT name, owner, species FROM pet";
$retid = mysql_db_query($db, $SQL, $cid);
if (!$retid) { echo( mysql_error()); }

$n = "1"; //count
while ($data = mysql_fetch_array($retid))
{
$data[$n] = $data;
$n++;
}

var_dump($data);

?php>

This puts everything into an array, then dumps it all. You could easily just put an eco inside the WHILE instead. You can also do like:
echo $data["name"];
inside the WHILE, which will only echo the name column on each loop.

If you need more help, let me know.

Options: ReplyQuote


Sorry, only registered users may post in this forum.