Posts: 401
Threads: 141
Joined: Oct 2009
Reputation:
5
I've been learning SQL through phpmyadmin, but I also want to see what it's like without phpmyadmin. However, I'm having some problems.
I have
Code: -- phpMyAdmin SQL Dump
-- version 3.1.2deb1ubuntu0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 24, 2009 at 01:58 AM
-- Server version: 5.0.75
-- PHP Version: 5.2.6-3ubuntu4.4
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `mydomain`
--
-- --------------------------------------------------------
--
-- Table structure for table `people`
--
CREATE TABLE IF NOT EXISTS `people` (
`id` int(6) NOT NULL,
`name` char(100) NOT NULL,
`telephone` char(50) NOT NULL,
`birthday` char(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `people`
--
INSERT INTO `people` (`id`, `name`, `telephone`, `birthday`) VALUES
(100100, 'ross', '222 222 222', '3 january'),
(200200, 'John', '999 999 999', '19 may'),
(300300, 'jane', '888 888 888', '19 july');
saved into Test.sql
I want to see my chart or some sort of result in my browser, how do I do this. I've already tried putting it in /var/www/. However, that didn't work, it only displayed Test.sql in plain text.
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
You'll need to write an PHP code for that, first you'll need to connect to the database and then fetch resuls... and show them.
PHP Code: <?php
$db = array( 'host' => 'localhost', // your host 'user' => 'root', //username 'pass' => '', //password 'base' => 'test' //database name not table );
// connecting to the database if($connect = mysql_connect($db['host'], $db['user'], $db['pass'])) { // selecting the database if(!$sel = mysql_select_db($db['base'])) { die("Connection failed..<br />".mysql_error()); } }
//sql query string $sql = "SELECT * FROM `people`"; //execute sql query if(!$query = mysql_query($sql)) { die(mysql_error()); // die and show error }
//if mysql_num_rows returns 0 there isn't anything in the table if(mysql_num_rows($query) <= 0) { die("There are no entries in the database"); }
//fetch result from the query while($rows = mysql_fetch_array($query)) { echo $rows['id']." : ".$rows['name']." : ".$rows['telephone']." : ".$rows['birthday']."<br />"; }
//close connection mysql_cl($connect);
?>
Posts: 401
Threads: 141
Joined: Oct 2009
Reputation:
5
(12-24-2009, 07:46 AM)Master of The Universe Wrote: You'll need to write an PHP code for that, first you'll need to connect to the database and then fetch resuls... and show them.
PHP Code: <?php
$db = array( 'host' => 'localhost', // your host 'user' => 'root', //username 'pass' => '', //password 'base' => 'test' //database name not table );
// connecting to the database if($connect = mysql_connect($db['host'], $db['user'], $db['pass'])) { // selecting the database if(!$sel = mysql_select_db($db['base'])) { die("Connection failed..<br />".mysql_error()); } }
//sql query string $sql = "SELECT * FROM `people`"; //execute sql query if(!$query = mysql_query($sql)) { die(mysql_error()); // die and show error }
//if mysql_num_rows returns 0 there isn't anything in the table if(mysql_num_rows($query) <= 0) { die("There are no entries in the database"); }
//fetch result from the query while($rows = mysql_fetch_array($query)) { echo $rows['id']." : ".$rows['name']." : ".$rows['telephone']." : ".$rows['birthday']."<br />"; }
//close connection mysql_cl($connect);
?>
Thanks, this worked great. Only thing is I think mysql_cl() should be mysql_close()
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
(12-24-2009, 12:28 PM)nevets04 Wrote: Thanks, this worked great. Only thing is I think mysql_cl() should be mysql_close()
Yeah you're right, sorry about that.
It is mysql_close();
Posts: 401
Threads: 141
Joined: Oct 2009
Reputation:
5
Can you make it write the names of each row on top like:
| id | name | telephone | birthday |
Posts: 1,351
Threads: 111
Joined: Oct 2009
Reputation:
40
(12-24-2009, 12:38 PM)nevets04 Wrote: Can you make it write the names of each row on top like:
| id | name | telephone | birthday |
Just put this right before the while function.
PHP Code: echo "id | name | telephone | birthday <br />";
But you could also put this before while.
PHP Code: echo <<<HTML <table> <tr><td>ID</td><td>NAME</td><td>TELEPHONE</td><td>BIRTHDAY</td></tr> HTML;
And then change the echo in the while to
PHP Code: echo "<tr><td>".$rows['id']."</td><td>".$rows['name']."</td><td>".$rows['telephone']."</td><td>".$rows['birthday']."</td></tr>";
And after the while function
Posts: 401
Threads: 141
Joined: Oct 2009
Reputation:
5
(12-24-2009, 12:44 PM)Master of The Universe Wrote: Just put this right before the while function.
PHP Code: echo "id | name | telephone | birthday <br />";
But you could also put this before while.
PHP Code: echo <<<HTML <table> <tr><td>ID</td><td>NAME</td><td>TELEPHONE</td><td>BIRTHDAY</td></tr> HTML;
And then change the echo in the while to
PHP Code: echo "<tr><td>".$rows['id']."</td><td>".$rows['name']."</td><td>".$rows['telephone']."</td><td>".$rows['birthday']."</td></tr>";
And after the while function
K, thanks
Posts: 43
Threads: 6
Joined: Oct 2009
Reputation:
0
nevets, you should also look into the mysql command line. It displays your tables in the fashion you described and it allows you to enter sql queries one-by-one.
|