Maracles Posted May 19, 2009 Share Posted May 19, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/158826-connecting-to-mysql-database-witin-a-php-page/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2009 Share Posted May 19, 2009 Umm. What is it doing? You have not exactly told us what you see in front of you that would help identify the problem. Quote Link to comment https://forums.phpfreaks.com/topic/158826-connecting-to-mysql-database-witin-a-php-page/#findComment-837693 Share on other sites More sharing options...
Maracles Posted May 19, 2009 Author Share Posted May 19, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/158826-connecting-to-mysql-database-witin-a-php-page/#findComment-837696 Share on other sites More sharing options...
PFMaBiSmAd Posted May 19, 2009 Share Posted May 19, 2009 You have made two posts without actually stating what the problem is. Do you have a specific problem, a specific error, or a specific question that you need help with? Quote Link to comment https://forums.phpfreaks.com/topic/158826-connecting-to-mysql-database-witin-a-php-page/#findComment-837698 Share on other sites More sharing options...
Maracles Posted May 19, 2009 Author Share Posted May 19, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/158826-connecting-to-mysql-database-witin-a-php-page/#findComment-837703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.