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... <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);
$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>MY OUT PUT ON THE BROWSER STOPS HERE..
Book-O-Rama Search Results
Starting connectionthan you for your help in advance...
To connect, I would suggest using code like what I have below. Now the code I am showing you is working and actually in use. I named my variables as such:
$DBhost = "localhost";
$DBuser = "dbusername";
$DBpass = "dbpassword";
$DBName = "nameofdatabase";$link = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
mysql_select_db("$DBName") or die("Unable to select
database $DBName");I hope this helps!
Sorry no luck, I also tried to connect as root...as soon as the code hits the link statement
$link = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");it just goes into some far far galaxy....I don't even get Unable to connect or any error message....
Try replacing: $result = $db->query($query);
with
$result=mysql_query($query);
or
$result = $db->query($query);
with
$result = $link->query($query);
I tried both of your suggestion, but no luck....
Post the script in the Programming forum which is where it should be and you will probably have more success. Home Page http://ewen.uuuq.com/
I am thinking it might be a MySQL setting???
But here are the two files .html and .phpsearch.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>
I posted my problem on the Programming forum also..
This is the post name..MySQL Connection problem form PHP script
Thanx You guys in advance!