Jump to content

Connecting to MySQL database witin a PHP page


Maracles

Recommended Posts

I'm creating my first PHP page that connects to a MySQL database. The database is being hosted on my webserver as is my php page. I am however struggling to make a connection to the database. Using code found in a tutorial I am following I must use:

 

@ $db = new mysqli('localhost', 'bookorama', 'bookorama123', 'books');

 

I  altered the above to match my webserver, it therefore reads like below (with password remove obviously), am I right in changing local host to my database host name? I should point out that the information I am using for the Host name, Username and Password is the same as that found in my  config.inc.php file found in the domain.com/phpmyadmin folder.

 

Also I have my website under a protected directory to shield it from Google while it is being built, does this affect how the page connects to the database.

 

@ $db = new mysqli('db1345.oneandone.co.uk', 'dbo287828419', 'xxxxxx', 'books');

 

Full code for the page is below, I am using the book 'PHP and MySQL Web Development' to learn.

 

 

<html>
<head>
  <title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?php
  // create short variable names
  $searchtype=$_POST['searchtype'];
  $searchterm=trim($_POST['searchterm']);

  if (!$searchtype || !$searchterm) {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }

  if (!get_magic_quotes_gpc()){
    $searchtype = addslashes($searchtype);
    $searchterm = addslashes($searchterm);
  }
  
@ $db = new mysqli('db1345.oneandone.co.uk', 'dbo287828419', 'xxxxx', 'books');


  if (mysqli_connect_errno()) {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = $db->query($query);

  $num_results = $result->num_rows;

  echo "<p>Number of books found: ".$num_results."</p>";

  for ($i=0; $i <$num_results; $i++) {
     $row = $result->fetch_assoc();
     echo "<p><strong>".($i+1).". Title: ";
     echo htmlspecialchars(stripslashes($row['title']));
     echo "</strong><br />Author: ";
     echo stripslashes($row['author']);
     echo "<br />ISBN: ";
     echo stripslashes($row['isbn']);
     echo "<br />Price: ";
     echo stripslashes($row['price']);
     echo "</p>";
  }

  $result->free();
  $db->close();

?>
</body>
</html>

 

 

 

Link to comment
Share on other sites

Sorry. The database is an extremely simple one listing a few records of customers, books and prices. The page has one drop down box allowing users to search for either Author, Title or ISBN and then one text field allowing users to enter search terms. The code for the html page is below.

 

<html>
<head>
<title>Book-O-Rama Catalog Search</title>
</head>

<body>
  <h1>Book-O-Rama Catalog Search</h1>
  
  <form action="results.php" method="post">
   Choose Search Type:<br /> 
   <select name="searchtype">
     <option value="author">Author</option>
     <option value="title">Title</option>
     <option value="isbn">ISBN</option>
   </select>
   <br />
   Enter Search Term:<br />
   <input name="searchterm" type="text" size="40" />
   <br />
   <input type="submit" name="submit" value="search" />
   </form>  
   
</body>  
</html>

 

 

Link to comment
Share on other sites

The problem was that- 'I am however struggling to make a connection to the database.'

 

The aim of the web page is to retrieve from the database any records matching the users entered search terms. At the moment when I enter in a search time that I am 100% certain is in the database the page returned lists only the header, ' Book-O-Rama Search Results' but does not list any results, nor does it list a specific error.

 

I do not know therefore whether it is connecting to the database and not actually finding any matching records, or whether it is not succeeding in its connection to the database. I presumed the later because I had never done this before and thought I may have got the wrong database parameters.

 

Is there a way to find out the exact reason it is nor retrieving the requested database rows?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.