PHP / MYSQL / Apache

January 19, 2010 at 23:58:33
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...

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

than you for your help in advance...


See More: PHP / MYSQL / Apache

Report •


#1
January 20, 2010 at 07:06:30
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!

Windows Utilities


Report •

#2
January 20, 2010 at 08:36:42
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....


Report •

#3
January 20, 2010 at 10:56:58
Try replacing:

$result = $db->query($query);

with

$result=mysql_query($query);

or

$result = $db->query($query);

with

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

Windows Utilities


Report •

Related Solutions

#4
January 20, 2010 at 12:09:42
I tried both of your suggestion, but no luck....

Report •

#5
January 20, 2010 at 13:29:29
Can you post the completed script? Including what I suggested in it?

Windows Utilities


Report •

#6
January 20, 2010 at 18:40:43
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/


Report •

#7
January 20, 2010 at 21:59:58
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>


Report •

#8
January 20, 2010 at 22:15:11
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!


Report •


Ask Question