Erorr getting Info from MySQL DB<

liunx

Guest
hiya guys; I just bought a book called
"PHP and MySQL Web Development, Second Edition"
and so far its been great; now I'm into the MySQL parts...

Using modified versions of their example script for
creating a search form, to search where it displays
the book title, ISBN number, price, author(s), etc.

I've used the basic table/colum SQL command line
to create a table called "users" and in it, I've put

userid - not null, primary key, auto increment values.
name - used to store brief user names
email - used to store user emails
division - used to store the division the gamer is in

Now, in it I've added two rows so far;
korn <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e --> GR
test <!-- e --><a href="mailto:[email protected]">[email protected]</a><!-- e --> NA

When I execute a simple script to query the table,
and try and display the info relating to this, I get
an error.... Here's the coding:

search.php - the code
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="results.php" method="post">
<select name="searchtype">
<option value="User">User Name</option>
<option value="Email">User's Email</option>
<option value="Division">User's Division</option>
</select>
<br>Enter Search Term:<br>
<input type="text" name="searchterm"><br>
<input type="submit" value="Search"></form>
</body></html>

result.php - the code
<html>
<head>
<title>Search Results</title>
</head>
<body>
<?php
// create short variable names
$searchtype=$HTT_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details';
eixt;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $db = mysql_pconnect('localhost', 'user_name', 'password');

if (!$db)
{
echo 'Error: could not connect to database. Please try again later.';
exit;
}
mysql_select_db('db_name');
$query = "select * from users where ".$searchtype." like '%".searchterm."%'";
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo '<p>Number of users found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><b>'.($i+1.'. User: ';
echo htmlspecialchars(stripslashes($row['name']));
echo '</b><br>Email Address: ';
echo stripslashes($row['email']));
echo '<br>Division: ';
echo stripslashes($row['dvision']));
echo '</p>';
}
?>
</body></html>

I'm getting this error when I run it:
Parse error: parse error in /home/korn/public_html/test/results.php on line 38

which would be this line:
echo '<p><b>'.($i+1.'. User: ';


Can someone help me? I'm really stuck...echo '<p><b>'.($i+1.'. User: ');


Does the added ) make the difference?Nope, still saying an error on line38...eixt; /// typo
$searchtype=$HTT_POST_VARS['searchtype']; /// Typo
echo '<p><b>'.($i+1.'. User: '; /// Typo
echo htmlspecialchars(stripslashes($row'name'])); /// Typoecho stripslashes($row['email'])); /// Typo
echo stripslashes($row['dvision'])); /// TypoOriginally posted by Bica
echo '<p><b>'.($i+1.'. User: ');


Does the added ) make the difference?

i'll say
'<p><b>'.($i+1).' User: ';alright, here's the code I got:

<html>
<head>
<title>Search Results</title>
</head>
<body>
<?php
// create short variable names
$searchtype=$HTT_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details';
eixt;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $db = mysql_pconnect('localhost', 'user_pass', 'user_pass');

if (!$db)
{
echo 'Error: could not connect to database. Please try again later.';
exit;
}
mysql_select_db('db_name');
$query = "select * from users where ".$searchtype." like '%".searchterm."%'";
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo '<p>Number of users found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><b>'.($i+1).' User: ';
echo htmlspecialchars(stripslashes($row['User']));
echo '</b><br>Email Address: ';
echo stripslashes($row['Email']);
echo '<br>Division: ';
echo stripslashes($row['Division']);
echo '</p>';
}
?>
</body></html>

And I get this error:
You have not entered search details
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/korn/public_html/test/results.php on line 31

Number of users found:OK, ill make these changes, then do the same on the file
(they are marked)

Originally posted by Leonisknovo
<html>
<head>
<title>Search Results</title>
</head>
<body>
<?php
// create short variable names
$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm']; //HTT changed to HTTP
$searchterm= trim($searchterm);

if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details';
exit(); //eixt; to exit();
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $db = mysql_pconnect('localhost', 'user_pass', 'user_pass');

if (!$db)
{
echo 'Error: could not connect to database. Please try again later.';
exit;
}
mysql_select_db('db_name');
$query = "select * from users where ".$searchtype." like '%".searchterm."%'";
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo '<p>Number of users found: '.$num_results.'</p>';

for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><b>'.($i+1).' User: ';
echo htmlspecialchars(stripslashes($row['User']));
echo '</b><br>Email Address: ';
echo stripslashes($row['Email']);
echo '<br>Division: ';
echo stripslashes($row['Division']);
echo '</p>';
}
?>
</body></html>Instead of $HTTP_POST_VARS['name'] why not use $_POST['name']instead of
$result = mysql_query($query);

do
$result = mysql_query($query) or exit(mysql_error());

will display if any error in your query...thnkx guys; I shalfed this project for now;
but I'm using all the stuff I learned for future projects :)
which, actually, was the point of this project :rolleyes:
 
Back
Top