PHP and MYSQL Web Development Problem

I am learning from Sams PHP and MYSQL Web Development book. I am following one of  the examples and cant seem to connect to the database. I have created the users and given it all of the rights to the DB, but it will not connect, I dont even show a connection attempt in MySQL Administrator. I have done everything that I can think of even creating a ODBC condit for it but nothing works. Below is the cod that I am using. Thanks

<html>
<head>
  <title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?
 // Report simple running errors
//error_reporting (E_ERROR | E_WARNING | E_PARSE);


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

  $searchtype = addslashes($searchtype);
  $searchterm = addslashes($searchterm);


  @ $db = mysql_pconnect("localhost", "bookorama", "bookorama");


if (!$db)
  {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
  }


  mysql_select_db("books");
  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = mysql_query($query);


  $num_results = mysql_num_rows($result);


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


  for ($i=0; $i <$num_results; $i++)
  {
     $row = mysql_fetch_array($result);
     echo "<p><strong>".($i+1).". Title: ";
     echo 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>";
  }


?>


</body>
</html>
LVL 3
trathAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gamebitsCommented:
Hi trath

I replaced:  @ $db = mysql_pconnect("localhost", "bookorama", "bookorama");

by:    $db = mysql_connect("localhost", "bookorama", "bookorama");

Since I didn't have the form to do a search I had to disable this part of the code:

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

I'm now getting this error report:

Warning: mysql_connect(): Access denied for user: 'bookorama@localhost' (Using password: YES) in /home/gamebit/public_html/COINS/DeBug/testforpoint.php on line 22
Error: Could not connect to database. Please try again later.

The sript try to access the db but I did not gave the right info (username, password)

So I replaced: ("localhost", "bookorama", "bookorama") with the proper info to access one of my db and I got this message:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/gamebit/public_html/COINS/DeBug/testforpoint.php on line 37

Number of books found:

Explanation: I did access the db (my db) but none of the field matches the query of course.

So replace the line I said at the beginning of the post and supply the right username and password, it should work.

Gamebits
trathAuthor Commented:
That didnt work. I am sure the problem is in how I have the DB setup. I have disabled all of the input checking, I have paseted the code I am using below. So that I can see the actual error in the attempt to connect to the DB. Here is the error...Thanks

Warning: mysql_select_db(): Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wwwroot\results.php on line 32

Warning: mysql_select_db(): A link to the server could not be established in C:\wwwroot\results.php on line 32

Warning: mysql_query(): Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wwwroot\results.php on line 34

Warning: mysql_query(): A link to the server could not be established in C:\wwwroot\results.php on line 34

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wwwroot\results.php on line 37

Number of books found:


Here is the code...


<html>
<head>
  <title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?
 // Report simple running errors
//error_reporting (E_ERROR | E_WARNING | E_PARSE);


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

  $searchtype = addslashes($searchtype);
  $searchterm = addslashes($searchterm);


  @ $db = mysql_connect("localhost", "root", "root");


//if (!$db)
//  {
//    echo "Error: Could not connect to database. Please try again later.";
//    exit;
//  }


  mysql_select_db("books");
  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = mysql_query($query);


  $num_results = mysql_num_rows($result);


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


  for ($i=0; $i <$num_results; $i++)
  {
     $row = mysql_fetch_array($result);
     echo "<p><strong>".($i+1).". Title: ";
     echo 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>";
  }


?>


</body>
</html>
 



kupra1Commented:
I think you need to check your mysql database to confirm the access from localhost or any host to the username "bookorama".
If you dont have the required record in the mysql database, you can give the same by executing this from the mysql command prompt:

GRANT ALL PRIVILEGES ON *.* TO 'bookorama'@'%'
IDENTIFIED BY 'bookorama' WITH GRANT OPTION;

Thanks
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

trathAuthor Commented:
Im sure that there is somthing simple wrong here but I tried the kupras suggestion by typing the command given, and the result was the same. Notice that in the response it is saying user ODBC@localhost using password:NO. In gamebits comments he did testing that said
 Access denied for user: 'bookorama@localhost' (Using password: YES). Why is it trying to connect using the correct user for him and not for me?
kupra1Commented:
hey trath,
  it should have worked. Are you using the username "bookorama" in your code or "root"? Whatever you use, execute the command for that username and password.
If you have the MySQL Control Center, go to the mysql database, then to the "user" table and confirm whether you have a record pertaining to the username with host as "%" and "localhost".

Try executing both of these:
GRANT ALL PRIVILEGES ON *.* TO 'bookorama'@'%'
IDENTIFIED BY 'bookorama' WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* TO 'bookorama'@'localhost'
IDENTIFIED BY 'bookorama' WITH GRANT OPTION;
gamebitsCommented:
Correct me if I'm wrong but you are not using a webhost at this point, do you?

To check your system (should run automatically on startup) type;

mysql -u root

to connect to it. You may need to supply a path to the mysql executable  
(c:\mysql\bin\mysql -u root on Windows (usually)).

You should get a welcome message ex. Welcome to MySQl monitor followed by a connection id and server version.

If you get an error message like

ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)

it means the MySQL server is not running. If you restart your system the server should start automatically, you can also restart it manually by typing;

mysql --standalone

at a command prompt.

To set the Root Password type;

set password for root@localhost=password('your password');

Log out ( \q ) and then log back in

mysql -u root -p

You should be prompt for your password -u means username and -p means log in with password.

Create a day-to-day account by typing;

grant create, create temporary tables, delete, execute, index, insert, lock tables, select, show databases, update
on *.*
to username identified by 'password';
trathAuthor Commented:
OK I think it is connecting even though I cannot prove it, is there some way I can see this. I am now getting the error. Why does it have to be so hard to get this to work. Learning Assembler wasnt this difficult...

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wwwroot\results.php on line 37

The section of code is like this

 mysql_select_db("books");
  $query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
  $result = mysql_query($query);


 $num_results = mysql_num_rows($result);     //THIS IS LINE 37


  echo "<p>Number of books found: ".$num_results."</p>";
kupra1Commented:
The error meant that your query wasn't executed and hence, the result set was empty. To confirm that, replace this:
$result = mysql_query($query);

by

$result = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");

I think you should confirm whether your database name is "books" and also the table name is "books" as you are doing that in your queries.

mysql_select_db("books");   // Is your database name "books"
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";  //is the tablename "books"


trathAuthor Commented:
here is what it kicks back...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like '%%'' at line 1
Couldn't execute query: select * from books where like '%%'
gamebitsCommented:
If you'd like you can try this, I know it works because I tried it on my system, make sure you replace DATABASE NAME  PASSWORD and TABLE NAME with the proper input.

I tried it with  a little form included in the script.

<html>
<head>
  <title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>Book-O-Rama Search Results</h1>
<?
 // Report simple running errors
//error_reporting (E_ERROR | E_WARNING | E_PARSE);


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

  $searchtype = addslashes($searchtype);
  $searchterm = addslashes($searchterm);


   $db = mysql_connect("localhost", "DATABASE NAME", "PASSWORD");


if (!$db)
  {
    echo "Error: Could not connect to database. Please try again later.";
    exit;
  }


  mysql_select_db("DATABASE NAME");
  $resultID = mysql_query ("select *
                              from TABLE NAME
                              where $searchtype
                              LIKE '%$searchterm%'");


  $num_results = mysql_num_rows($resultID);


  echo "<p>Number of books found: ".$num_results."</p>";
 
  $i=0;
while ($i < $num_results) {

$title=mysql_result($resultID,$i,"title");
$author=mysql_result($resultID,$i,"author");
$isbn=mysql_result($resultID,$i,"isbn");
$price=mysql_result($resultID,$i,"price");

echo "<table border=0 cellpadding=2 cellspacing=0>
<tr><td><strong>Title:</td>          <td>$title</strong></td></tr>
<tr><td>Author:</td>             <td>$author</td></tr>
<tr><td>ISBN:</td>     <td>$isbn</td></tr>
<tr><td>Price:</td>          <td>$price</td></tr>

</table>

";

$i++;
}


?>


<form action="<? $PHP_SELF ?>" method="POST">
<p><strong>Search: <select name="searchtype">
<option value='title'>Title</option>
<option value='author'>Author</option>
<option value='isbn'>ISBN</option>
<option value='price'>Price</option>
</select>

<strong>For:</strong> <input type="text" name="searchterm" size="30">


<input type="submit" name="submit" value="Search">

</form>
</body>
</html>

Gamebits

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trathAuthor Commented:
Ok, now the problem is that it didnt recognize the auth type so I had to upgrade my version of PHP. Now I get the error..

Fatal error: Call to undefined function mysql_pconnect() in C:\wwwroot\results.php on line 22

Before I upgraded PHP versions I was getting a connection request to the DB but it didnt recognize the authentication type.
Swarnika GuptaCommented:
$query = mysql_query("select * from student join college on student.college_id=college.id where '".$searchtype."' like '%".$searchterm."%' ");

what if i have join operation in my query and searchterm here is the name of the college which is present in college table ??
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.