-4

I am trying to figure out a solution on how I am going to display my data from the database. However I want to to show one specific entry. Here is my database design: MySQL database example.

I am using PHP to connect it with a website. On the website I would like it to display from the story table: "This is a book about the internet!" Only that nothing else only that specific entry. The book_number is a primary key and is auto incremented.

Here is my current code:

<?php
// Change variables if going to a server.
$username = "root";
$password = "";
$hostname = "localhost";

//connect
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//Select book database.
$selected = mysql_select_db("book",$dbhandle)
  or die("Could not select examples");

  //select all the records from the book database
$result = mysql_query("SELECT book_name, story, time_track FROM book");
//fetch data
while ($row = mysql_fetch_array($result)) {
   echo "ID:".$row{'book_name'}." Name:".$row{'story'}."
   ".$row{'time_track'}."<br>";
}

//close
mysql_close($dbhandle);

?>

My current code connects and displays all entry's I would like it to display as specified above.

Also I would like it to display one entry at a time from story table since I will be making more then one books and the story will be divided for multiple pages.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Edgar Axe
  • 130
  • 1
  • 12
  • Use the [WHERE](http://www.w3schools.com/sql/sql_where.asp) parameter – Crecket Sep 23 '15 at 10:28
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 23 '15 at 12:15

3 Answers3

1

You need to filter your records through SQL Like this:

$book_number = $_GET['book_number'];
$result = mysql_query("SELECT book_name, story, time_track FROM book
WHERE book_number = '$book_number'
");

You can pass this book_number variable through URL (GET) with/without encryption.

It is better to encrypt variables while sending them on URLs.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • You should pass `$book_number` through URL. Otherwise. just change `$book_number = $_GET['book_number'];` to `$book_number = 1;` and it will work. – Pupil Sep 23 '15 at 10:36
  • Sorry for that, please change to `$book_number = 1` and it must work. – Pupil Sep 23 '15 at 10:41
0

USE THIS

SELECT book_name, story, time_track FROM book WHERE book_name='your book'

This query will help to select details of that particular book name.

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
-1

Try This

<?php
// Change variables if going to a server.
$username = "root";
$password = "";
$hostname = "localhost";

//connect
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//Select book database.
  mysql_select_db("book",$dbhandle)
  or die("Could not select examples");

  //select single record from the book table which book_number is 1. 
$result = mysql_query("SELECT book_name, story, time_track FROM book WHERE book_number='1'");
//fetch data
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  echo "ID:".$row['book_name']." Name:". $row['story'].$row['time_track']."<br>";  
}

//close
mysql_close($dbhandle);

?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
gsanthosh91
  • 371
  • 4
  • 7
  • Notice: Undefined index: book_name in D:\Program\xamp\htdocs\c\story\c.php on line 20 Notice: Undefined index: story in D:\Program\xamp\htdocs\c\story\c.php on line 20 Notice: Undefined index: time_track in D:\Program\xamp\htdocs\c\story\c.php on line 20 ID: Name: – Edgar Axe Sep 23 '15 at 10:49
  • Thank you. Use square brackets for array – gsanthosh91 Sep 23 '15 at 10:57
  • 2
    Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Sep 23 '15 at 12:14