Jump to content

PHP and MySQL connection problems


cootetom
 Share

Recommended Posts

Hey all, I'm learning how to use mySQL reading a book called 'PHP and MySQL Web Development' by Luke Welling & Laura Thomson. I'm working through an example but have become stuck! I've set up the database on my server hosted by streamline.net which looks like this;Customers(CustomerID, Name, Address, City)Orders(OrderID, CustomerID, Amount, Date)Books(ISBN, Author, Title, Price)Order_Items(OrderID, ISBN, Quantity)Book_Reviews(ISBN, Reviews)I then setp up a small form on a web page to query this database.

<form id="search" action="results.php" method="post">	<p>	<label for="searchtype">Search by: </label>	<select name="searchtype" id="searchtype">  <option value="author">Author</option>  <option value="title">Title</option>  <option value="isbn">ISBN</option>	</select>	</p>	<p>  <label for="searchterm">Search term: </label>  <input name="searchterm" id="searchterm" type="text" />	</p><p>  <input type="submit" id="submit" value="Search" />	</p></form>

But the code in my php file doesn't seem to be connecting to the database.

<?php	//create short variable names	$searchType = $_POST['searchtype'];	$searchTerm = $_POST['searchterm'];	$searchTerm = trim($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 = mysqli_connect('mysql8.streamline.net', 'tomcootec', 'mypassword', 'tomcootec');	if (mysqli_connect_errno())	{  echo 'Error: Could not connect to database. Please try again later.';  exit;	}		$query = "select * from books where ".$searchType." like '%".searchTerm."%'";	$results = mysqli_query($db, $query);		$num_results = mysqli_num_rows($results);		echo '<p>Number of books found: '.$num_results.'</p>';		for ($i=0; $i < $num_results; $i++)	{  $row = mysqli_fetch_assoc($results);  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>';	}		mysqli_free_result($results);	mysqli_close($db);?>

If I take away the error suppression operator '@' from the line when connecting i get the following error.

Fatal error: Cannot instantiate non-existent class: mysqli in /home/fhlinux188/t/tomcoote.co.uk/user/htdocs/bookStore/results.php on line 68
Can anyone give me some help on this one?Thanks- Tom
Link to comment
Share on other sites

It's entirely possible that the webserver doesn't have the correct version of PHP to use the mysqli functions. The version history for mysqli_connect appears to show that it starts in PHP5. If you aren't sure what version the server is running, or just to see configuration options (including which modules are loaded), you should set up a phpinfo page. Create a new page, and put this code in it (and nothing else):

<?phpphpinfo();?>

You will see the version of PHP that is running, environment information, configuration values, etc. If you scroll down, you should start seeing options for various modules or extensions that are loaded, like calendar, domxml, gd, mcrypt, etc. You should see mysql listed there, and presumably mysqli should be right next to it. If you don't see mysqli, then it isn't installed on the server. You might be able to ask the server admins to install support for the mysqli PHP extension if that's the case. But for the sake of instruction, you should be able to use mysql_connect and the mysql functions instead of mysqli.On a side note, if you're starting to learn about database design, which is a very good thing to learn, I'll give you a pointer that I've picked up. During my education, my database teachers were telling me that a certain table's primary key should be any unique value, like for a table full of people, social security number might be a good primary key. But in reality, it turns out that the best way is for all tables to have an autonumber ID, regardless of whether or not they have another unique field. So your schema above would change to something like this:Customers(CustomerID, Name, Address, City)Orders(OrderID, CustomerID, Amount, Date)Books(BookID, ISBN, Author, Title, Price)Order_Items(OrderID, BookID, Quantity)Book_Reviews(BookID, Reviews)ISBN sounds like a good choice for a primary key, but if you were to use this in a project, and deliver the project to the customer, sure enough you'll get a question about how do they change the ISBN because someone screwed up. Same with users, I have a field for user ID (or an email), but I still have an autonumbered primary key column that I use for referring to the user in all database tables, that way if the user ID ever has to change for whatever reason, it doesn't mess up the database.If you build an idiot-proof system, they will build a better idiot. Remember that.

Link to comment
Share on other sites

Ok, I have run the code you gave me and found that my server does not infact have the mysqli function support. It is running PHP version 4.However I have used the information from that handy link you gave me and got my code working. I have decided to read through the book I have and then change any examples I want to try to use the mysql functions instead. This will work out to my advantage as I will be learning about both sets of functions, plus it will make me think a little more!Thanks for the advice on my database designs I will certainly be using the idea. Also thanks for such a details response, now I can get on with learning MySQL.- Tom

Link to comment
Share on other sites

Yeah, good luck. There's not much of a difference between mysql and mysqli (mysqli is "improved mysql functions"). If you know how to use one, you should be able to use the other. Books often teach with the latest and greatest, so you may run into some more examples that you aren't able to use everywhere, but it shouldn't be a big deal.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...