MySQL Connection problem form PHP script

January 20, 2010 at 22:11:30
Specs: Windows XP
I am having trouble connecting to mysql using php script..as soon as my code reaches the connection point it doesn't connect and doesn't even give any error messages..here is my code...

I am thinking it might be a MySQL setting???
But here are the two files .html and .php

search.html

<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>

++++++++

result.php

<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);

}

echo 'Starting connection';

@ $db = new mysqli('localhost', 'tempuser', 'password', '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);

$result = mysql_query($query);

// $result = $link->query($query);

$num_results = $result->num_rows;


echo "

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


for ($i=0; $i <$num_results; $i++)
{

$row = $result->fetch_assoc();

echo "

<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>


See More: MySQL Connection problem form PHP script

Report •

#1
January 23, 2010 at 11:40:24
Are you able to connect from the command line client using the root user account?

Try something like below without the @

$db = new mysqli('localhost', 'tempuser', 'password', 'books') or
die("db error: " . mysqli_connect_error());

to see if you can get an error message of what might be going on.


Report •

#2
January 26, 2010 at 11:38:20
mysql> $db = new mysqli('localhost', 'tempuser', 'password', 'books');
ERROR 1064 (42000): 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 '$db =
new mysqli('localhost', 'tempuser', 'password', 'books')' at line 1

I also tried this command with root user and password...


Report •

#3
January 26, 2010 at 14:28:31
MySql wouldn't recognize php as a valid command. When you entered your password in the client, was it accepted for what you're trying to use in php code?

In the mysql client, type "password" (without quotes and what your user password actually is); if it's rejected, then the database server isn't recognizing the user name and/or password as being valid. Try logging in as the root user

mysql> create database if not exists books;
mysql> grant all privileges on books.* to 'tempuser'@'localhost' identified by 'password' with grant option;

If you're able to connect to tempuser from the mysql client, then try running your php script again. At the top of your php code, try adding error_reporting(E_ALL);

<?php

   error_reporting(E_ALL);
   $db = new mysqli('localhost', 'tempuser', 'password', 'books') or
   die("db error: " . mysqli_connect_error());

   // rest of your code
?>


Report •
Related Solutions


Ask Question