Βάση Δεδομένων : mysql
- Κώδικας: Επιλογή όλων
<?php
/**
* @author : exarhis@hotmail.com
* @copyright : open source 2010
* @description : Its a file of four functions , 3 can be executed directly and the arguments are SQL code;
* Bellow are the samples .
*/
$one = get_data("SELECT field1,field2,field3 FROM table");
echo $one['field1'][0] ; // This array has this structure ;
$one = get_data_1("SELECT field FROM table1 WHERE `id` = `1`"); // returns just one variable
echo $one; // return one variable from the db
$one = get_data_1line("SELECT * FROM table2 WHERE `id` = '1'");
echo $one['id']; // return one row of the db
$one = get_data_1line("SELECT field1,field2 FROM table3 WHERE `field1` = 'dasdsa'");
function get_data($sql)
{
include '/home/domain/public_html/config.php';
$fields = get_fields($sql);
$result = mysql_query($sql) or die(mysql_error());
$rows_num = mysql_num_rows($result);
$i=1;
while ($row = mysql_fetch_array($result))
{
for($fi=0;$fi<count($fields);$fi++)
{
if(is_array($fields)) $current_field = $fields[$fi];
else $current_field = $fields;
$begin[$current_field][$i]=$row[$current_field];
}
$i++;
}
mysql_close;
return $begin;
}
function get_data_1line($sql)
{
include '/home/leftagr/public_html/config.php';
$fields = get_fields($sql);
$result = mysql_query($sql) or die(mysql_error());
$rows_num = mysql_num_rows($result);
$i=1;
while ($row = mysql_fetch_array($result))
{
for($fi=0;$fi<count($fields);$fi++)
{
$current_field = $fields[$fi];
$begin[$current_field]=$row[$current_field];
}
$i++;
}
mysql_close;
return $begin;
}
function get_data_1($sql)
{
include '/home/leftagr/public_html/config.php';
$fields = get_fields($sql);
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) return $row[$fields];
}
function get_fields($sql)
{
include '/home/leftagr/public_html/config.php';
$temp = explode(" ",$sql );
$fieldsi = $temp['1'];
$table = $temp['3'];
if($fieldsi == '*')
{
$fields_con = mysql_list_fields($config['db'],$table);
for($i=0;$i<mysql_num_fields($fields_con);$i++) $fields[$i]=mysql_field_name($fields_con,$i);
mysql_close;
}
else {
$fieldsi = str_replace(' ','',$fieldsi);
$fields = explode (',',$fieldsi);
if(count($fields)==1) $fields = $fields[0];
}
return $fields;
}
?>