Jump to content

Querying a Database from the web


litosgonzalez

Recommended Posts

Hi guys,

I'm nuvee with php and mysql,

I'm writing the code to search and query data from Mysql,

but something is wrong with the connection or with the code....

 

This is my search code search.html

 

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

 

<body>

 

 

 

 

<form action="results.php" method="post">

 

Choose Search Type: <br/>

 

<select name="searchtype">

<option value="object_id">Object ID</option>

</select>

<br/>

 

Enter Search Term:<br/>

 

<input name="searchterm" type="text">

<br/>

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

</form>

 

 

 

</body>

</html>

 

========

 

and this is my results.php sript.

 

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

 

<body>

 

 

 

<?php

//create short variables names

$searchtype=$HTTP_POST_VARS['searchtype'];

$searchterm=$HTTP_POST_VARS['searchterm'];

 

$searchterm = trim($searchterm);

if (!searchtype || !$searchterm)

{

echo 'You have not entered search detail. Please go back and try again.';

exit;

}

$searchtype = addslashes($searchtype);

$searchterm = addslashes($searchterm);

 

@ $db = mysql_pconnect('localhost', 'root mysql');

 

if (!$db)

{

echo 'Error: Could not connect to database. Please try again later.';

exit;

}

 

 

mysql_select_db('products');

$query = "select * from objects where ".$searchtype." like '%".$searchterm."%'";

$result = mysql_query($query);

 

$num_results = mysql_num_row($result);

 

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

 

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

 

{

$row = mysql_fetch_array($result);

echo '<p><strong>'.($i+1).'. Object ID: ';

echo htmlspecialchars(stripslashes($row['object_id']));

echo '</p>';

}

?>

 

</body>

</html>

 

 

thank for your help....!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.