Hi,
I have many quires in my PHP pages web sites.
When i execute them from the PHP it will be very slow! But when i execute them from the phpMyAdmin it will be very fast !!!
I use this class to access to mySQL database:
class MySQL {
var $mysql_query_result;
var $mysql_query_is_doing;
var $mysql_error_number;
var $mysql_error_desc;
var $mysql_get_sql_query;
var $linkID;
function DB_Connect()
{
if ( !@$this->linkID = mysql_connect( _HOST, _USER, _PASS ) )
{
die('<center><strong><font color="#FF0000">Cannot connect to the MySQL server: '.mysql_error().'</font></center></strong>');
}
else
{
if ( !mysql_select_db( _DBNAME ) )
{
die('<center><strong><font color="#FF0000">Cannot select the "'._DBNAME.'" Databse. Please make sure that.</font></center></strong>');
}
}
if (strlen(_DB_SET_CHARS)>= 1)
{
$s1 = @mysql_query("SET character_set_client = "._DB_CHARS_TYPE);
$s2 = @mysql_query("SET character_set_connection = "._DB_CHARS_TYPE);
$s3 = @mysql_query("SET character_set_database = "._DB_CHARS_TYPE);
$s4 = @mysql_query("SET character_set_results = "._DB_CHARS_TYPE);
$s5 = @mysql_query("SET character_set_server = "._DB_CHARS_TYPE);
}
}
// mysql_query()
function do_query( $sql_query )
{
$this->mysql_get_sql_query = $sql_query;
$this->mysql_query_result = @mysql_query($sql_query);
$do_query = array('INSERT', 'UPDATE', 'DELETE');
if ( mysql_affected_rows() )
{
$this->mysql_query_is_doing = 1;
}
else
{
$this->mysql_query_is_doing = 0;
}
$this->mysql_error_number = mysql_errno();
$this->mysql_error_desc = mysql_error();
}
// mysql_fetch_array()
function get_rows_array( )
{
return @mysql_fetch_array($this->mysql_query_result);
}
// mysql_fetch_assoc()
function get_rows_names()
{
return @mysql_fetch_assoc($this->mysql_query_result);
}
// mysql_fetch_object()
function get_rows_object()
{
return @mysql_fetch_object($this->mysql_query_result);
}
// mysql_result()
function get_result( $row , $field )
{
return @mysql_result($this->mysql_query_result,$row, $field);
}
// Table Optimization
function table_optimize( $table_name )
{
$this->do_query('OPTIMIZE TABLE '.$table_name);
}
function db_optimize()
{
$this->do_query('SHOW TABLES FROM '._DBNAME);
while( $table_no = $this->get_rows_array() )
{
$this->do_query('OPTIMIZE TABLE '.$table_no[0]);
while( $get_result = $this->get_rows_names() )
{
if ( $get_result['msg_type'] != 'error' )
{
echo 'Table Name: '.$get_result['Table'].'<br />';
echo 'Stauts Is: <font color="green">'.$get_result['Op'].'</font><br />';
echo $get_result['Msg_text'].'<br />';
echo '<hr />';
}
else
{
echo 'Cannot optimize the '.$get_result['Table'].' Table!<br />';
echo 'Error is: '.$get_result['Msg_text'];
}
} // while
} // while
}
function get_num_rows()
{
if ( @mysql_num_rows($this->mysql_query_result) )
{
return @mysql_num_rows($this->mysql_query_result);
}
else
{
//echo $this->mysql_get_sql_query;
//echo $this->sql_error();
}
}
function sql_error( $header = 0 )
{
global $style_full_path;
if ( $header == 1 )
{
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="'._LANG_HTML_DIR.'">
<head>
<meta http-equiv="Content-Type" content="text/html; charset='._LANG_ENCODING.'" />
<title>SQL Error!</title>
</head>
<style type="text/css">
body {
font-family: Tahoma;
font-size: 12px;
}
.sql_error {
width: 90%;
padding: 3px;
background-color: #fff3f3;
border: solid 1px #ff0001;
direction: ltr;
margin: 0 auto;
text-align: justify;
margin-top: 10px;
font-weight: bold;
color: Red;
font-family: Tahoma;
}
</style>
<body>';
}
$html = '<div align="center"><img src="'.$style_full_path.'/images/error.gif" /><br /><br />'._LNG_ERR_SQL_DO.'</div><div class="sql_error">'. $this->mysql_error_desc.'<br />SQL ERROR Number is: '.$this->mysql_error_number.'</div>';
if ($header == 1) $html .='</body></html>';
return $html;
}
function getLangStatement( $str )
{
global $default_lang;
return ' '.$str." (lang = '".$default_lang."' || lang = 'all') ";
}
function closeConnection()
{
@mysql_close($this->linkID);
}
}
Example:
$MySQL = new MySQL();
$MySQL->DB_Connect();
$MySQL->do_query('Query 1 here');
//to get the result
while ($r = $MySQL->get_rows_names() )
{
//code here
}
//Query 2
$MySQL->do_query('Query 2 here');
//to get the result
while ($r = $MySQL->get_rows_names() )
{
//code here
}
Anyone can help me please?