Page 1 of 1

Select use from a MysQl database

Posted: Sat Mar 01, 2003 11:27 am
by jrts
:(
What shall I do so when I choose the word "all" from a "select" of a html form, the results that appear are all the values of a column of a data base Mysql. Now the search only works when finds a equivalent with the data base from the "select" html.
Please, find below the program:
Could you have a look at it.


<?php require_once('Connections/conexs.php'); ?>
<?php
$maxRows_consulta = 10;
$pageNum_consulta = 0;
if (isset($HTTP_GET_VARS['pageNum_consulta'])) {
$pageNum_consulta = $HTTP_GET_VARS['pageNum_consulta'];
}
$startRow_consulta = $pageNum_consulta * $maxRows_consulta;

$colname_consulta = "1";
if (isset($HTTP_GET_VARS['poblacio'])) {
$colname_consulta = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['poblacio'] : addslashes($HTTP_GET_VARS['poblacio']);
}
$varpob_consulta = "1";
if (isset($HTTP_GET_VARS['tipus'])) {
$varpob_consulta = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['tipus'] : addslashes($HTTP_GET_VARS['tipus']);
}
mysql_select_db($database_conexs, $conexs);
$query_consulta = sprintf("SELECT tipus, poblacio, zona, foto1, foto2, descrip, preu FROM bens WHERE poblacio= '%s' and tipus = '%s'", $colname_consulta,$varpob_consulta);
$query_limit_consulta = sprintf("%s LIMIT %d, %d", $query_consulta, $startRow_consulta, $maxRows_consulta);
$consulta = mysql_query($query_limit_consulta, $conexs) or die(mysql_error());
$row_consulta = mysql_fetch_assoc($consulta);

if (isset($HTTP_GET_VARS['totalRows_consulta'])) {
$totalRows_consulta = $HTTP_GET_VARS['totalRows_consulta'];
} else {
$all_consulta = mysql_query($query_consulta);
$totalRows_consulta = mysql_num_rows($all_consulta);
}
$totalPages_consulta = ceil($totalRows_consulta/$maxRows_consulta)-1;
?>

Posted: Sun Mar 02, 2003 1:51 pm
by Crashin
I would use an "if" statement to check the form's selection, and construct a query if "all" is chosen. For example:

Code: Select all

if($var == "all") &#123;
     $query = "SELECT column FROM table";
&#125;
else &#123;
     $query = "SELECT column FROM table WHERE column='$var'";
&#125;
Hope that helps!

sorry but I'm a learner

Posted: Mon Mar 03, 2003 8:39 am
by jrts
8O
Where I can put this exactly?

Re: Select use from a MysQl database

Posted: Mon Mar 03, 2003 9:46 am
by Crashin
First of all, which variable in your code is coming from the select element of the HTML form?

Posted: Mon Mar 03, 2003 11:04 am
by jrts
The variable is 'poblacio', this variable give the result especific from the html select, but I want to catch all the values when the select is "All"

In the before post there is the script¡

thanks for all

Posted: Mon Mar 03, 2003 3:58 pm
by puckeye
Hi jrts,

In your HTML SELECT substitute the Word All for the % character like so:

Code: Select all

<OPTION VALUE="%">All</OPTION>
That way when a user selects the ALL option but when your PHP receives the FORM result it'll receive the % character which will retreive everything in your query.

% is a pattern-matching wildcard which means that if you use WHERE id='%' this will retrieve all the rows where id is not NULL.