Professional Documents
Culture Documents
Una vez que nos hemos conectado con el servidor de bases de datos, ya podemos realizar
consultas a las tablas de la base de datos.
conex.phtml
<!-- Manual de PHP de WebEstilo.com -->
<?php
function Conectarse()
{
if (!($link=mysql_connect("localhost","usuario","Password")))
{
echo "Error conectando a la base de datos.";
exit();
}
if (!mysql_select_db("base_datos",$link))
{
echo "Error seleccionando la base de datos.";
exit();
}
return $link;
}
?>
while($row = mysql_fetch_array($result)) {
printf("<tr><td> %s</td><td> %s </td></tr>", $row["Nombre"],
$row["Apellidos"]);
}
mysql_free_result($result);
mysql_close($link);
?>
</table>
</body>
</html>
search.php:
<?
/*
* search.php
*
* Script for searching a database populated with keywords by the
* populate.php-script.
*/
if( $_POST['keyword'] )
{
/* Connect to the database: */
mysql_pconnect("localhost","construyasl","lzdttV6u0MoC")
or die("ERROR: Could not connect to database!");
mysql_select_db("construyasl");
/* Execute the query that performs the actual search in the DB: */
$result = mysql_query(" SELECT p.page_url AS url,
COUNT(*) AS occurrences
FROM page p, word w, occurrence o
WHERE p.page_id = o.page_id AND
w.word_id = o.word_id AND
w.word_word = \"$keyword\"
GROUP BY p.page_id
ORDER BY occurrences DESC
LIMIT $results" );
print "</body></html>\n";
?>
Y el otro es populate.php:
<?
/*
* populate.php
*
* Script for populating the search database with words,
* pages and word-occurences.
*/
mysql_select_db("test");
if( !$url )
{
die( "You need to define a URL to process." );
}
else if( substr($url,0,7) != "http://" )
{
$url = "http://$url";
}
if( $row['page_id'] )
{
/* If yes, use the old page_id: */
$page_id = $row['page_id'];
}
else
{
/* If not, create one: */
mysql_query("INSERT INTO page (page_url) VALUES (\"$url\")");
$page_id = mysql_insert_id();
}
/* Start parsing through the text, and build an index in the database: */
if( !($fd = fopen($url,"r")) )
die( "Could not open URL!" );
/* Extract all words matching the regexp from the current line: */
preg_match_all("/(\b[\w+]+\b)/",$buf,$words);
/* Loop through all words/occurrences and insert them into the database: */
for( $i = 0; $words[$i]; $i++ )
{
for( $j = 0; $words[$i][$j]; $j++ )
{
/* Does the current word already have a record in the word-table? */
$cur_word = addslashes( strtolower($words[$i][$j]) );
fclose($fd);
?>