Hello friends from SO!
Alright, this time I have a little bit more complex problem. We have a web crawler running and functioning normally during most of the day and time.
However, from time to time, it just stops: the link which is supposed to be analyzed next, never change it's state (from pending to scanning), and of course this stops the whole cycle.
We're logging all PHP errors using:
//errores producción
#ini_set('error_reporting', -1);
#ini_set('log_errors','On');
#ini_set('display_errors','Off');
#ini_set('error_log','/var/www/vhosts/xxx.com/xxx.com/xxx');
There's no evidence of anything that could cause the problem described. 0 anomalies.
Therefore, I believe the problem might be related to some kind of MySQL issues?
Every single MySQL query we do, is done using MySQLi by custome made functions, so my question here is:
Is there any simple approach to record every single MySQL error on the same file where we are storing the PHP errors?
Here are some of the functions used to query the MySQL:
Function db_ob($db_link, $ask) {
$feedback = mysqli_fetch_object(mysqli_query($db_link, $ask));
return $feedback;
}
and:
Function db_ob_all($db_link, $ask) {
$feedback = mysqli_query($db_link, $ask);
while ($row = mysqli_fetch_object($feedback)) { $value[] = $row; }
return $value;
}
So what I'm looking for, is a one or two lines solution, that I could add into these functions, in order to store and track any issue or error in the same file where I'm currently storing the PHP errors.
Thanks in advance!
Chris;
Solved:
1) make a function to track the errors into the PHP error_log:
Function informar_error_db($db_link) {
error_log("Dreadful SQL error: ". mysqli_error($db_link)." in ".$_SERVER["SCRIPT_FILENAME"]);
}
2) If there's MySQLi issues, save em:
Function db_ask($db_link, $ask) {
$feedback = mysqli_query($db_link, $ask);
if (mysqli_error($db_link)) { informar_error_db($db_link); }
return $feedback;
}
Here:
if (mysqli_error($db_link)) { informar_error_db($db_link); }
Related
I have a code:
$tx = Yii::$app->db->beginTransaction();
try {
// CODE (a lot of active-record reads and writes)
$tx->commit();
echo "All good!";
} catch (\Throwable $ex) {
$tx->rollback();
echo "Error";
}
It executes and I get "All good!" as a result.
However, nothing gets actually stored in database.
The code has been working for several months so far and wasn't modieid.
It suddently stopped working yesterday.
After an hour of debugging, I can confirm operations go fine, I can "echo" intermidate results, including IDs of the records I'm inserting. But still nothing saves in DB after the final commit.
If I remove transaction, the code works, and everything gets stored in DB, as it used to with Transaction around.
I want to ensure wholeness of the changes, want to get in back inside the transaction.
Or, at least, I want to understand which code (or DB state or whatever else) prevents data from being stored and why I don't get any exceptions and my "Error" echoed, since transaction failed to commit.
I was sure that if transaction fails to commit (actually write to DB), commit() method would throw an exception, but it does not. Is there a way to get it?
Thank you very much in advance.
The problem turned out to be in the code.
Thanks, #Michal Hynčica, you were right indeed.
There was a part like:
foreach (..) {
$tx = Yii->$app->db->beginTransaction();
if ($oneRareCondition) {
continue;
}
...
$tx->commit();
}
Was solved by adding $tx->commit(); before continue; in that rarely-happening if:
foreach (..) {
$tx = Yii->$app->db->beginTransaction();
if ($oneRareCondition) {
$tx->commit();
continue;
}
...
$tx->commit();
}
I've been searching for a suitable PHP caching method for MSSQL results.
Most of the examples I can find suggest storing the results in an array, which would then get included to page. This seems great unless a request for the content was made at the same time as it being updated/rebuilt.
I was hoping to find something similar to ASP's application level variables, but far as I'm aware, PHP doesn't offer this functionality?
The problem I'm facing is I need to perform 6 queries on page to populate dropdown boxes. This happens on the vast majority of pages. It's also not an option to combine the queries. The cached data will also need to be rebuilt sporadically, when the system changes. This could be once a day, once a week or a month. Any advice will be greatly received, thanks!
You can use Redis server and phpredis PHP extension to cache results fetched from database:
$redis = new Redis();
$redis->connect('/tmp/redis.sock');
$sql = "SELECT something FROM sometable WHERE condition";
$sql_hash = md5($sql);
$redis_key = "dbcache:${sql_hash}";
$ttl = 3600; // values expire in 1 hour
if ($result = $redis->get($redis_key)) {
$result = json_decode($result, true);
} else {
$result = Db::fetchArray($sql);
$redis->setex($redis_key, $ttl, json_encode($result));
}
(Error checks skipped for clarity)
i have a kind of problems here and i don't know how to solve it... google said nothing and the search function here didn't shown me anything...
i use this code
$GAME_DB = new mysqli($__CONFIG['MySQL']['HOST'], $__CONFIG['MySQL']['USER'], $__CONFIG['MySQL']['PASS'], $__CONFIG['MySQL']['DB']['GAME']);
if($GAME_DB->connect_errno) { echo $GAME_DB->connect_error; exit; }
$VILLAGE_DATA['villageID'] = $mysqli->real_escape_string($VILLAGE_DATA['villageID']);
$query = "SELECT name, level, time FROM actions WHERE type='build' AND villageID='".$VILLAGE_DATA['villageID']."'";
if($result = $GAME_DB->query($query))
{
// table header
while($row = $result->fetch_row())
{
// some rows in here
}
// table footer
}
there aren't any syntax errors in that query and the results are there!
sometimes the query is successful and i see my table but sometimes (for example: when i reload) i receive the error message Commands out of sync; you can't run this command now
how can this just work "sometimes"? where is my problem?
http://dev.mysql.com/doc/refman/5.6/en/commands-out-of-sync.html
If you get Commands out of sync; you can't run this command now in
your client code, you are calling client functions in the wrong order.
This can happen, for example, if you are using mysql_use_result() and
try to execute a new query before you have called mysql_free_result().
It can also happen if you try to execute two queries that return data
without calling mysql_use_result() or mysql_store_result() in between.
But from the code you've posted I don't see how this could happen. Either there's more code than what you've posted and something there is happening out of order, or your MySQL connections are being improperly established/pooled/persisted somewhere outside of PHP.
I am quite a noob in using LDAP (this is actually my first project) and I can't find an answer on google that can help me...
So my problem is: I'm trying to add a contact with php to an active directory on a windows 2003 server. I can connect to the server, and neither I have problems with ldap_bind.
But when I run the program I always get the Error:
Warning: ldap_add() [function.ldap-add]: Add: Operations error in (Blabla) on line bla
and the ldap_error also only says "Operations error"
which is pretty vague, so I don't even know if it's a problem with the server or with my code.
I saw some threads with similar problems where servers didn't allow anonymous access, but I even bind with an admin account and it still does not work.
My code looks a lot like this:
$ldapcon=ldap_connect("servername");
if($ldapcon) {
$bind=ldap_bind($ldapcon,"Admin#domain.com", "somePassword");
if($bind) {
// create data...
$info=array();
$info["cn"][0] = "Hans Mustermann";
$info["sn"][0] = "Mustermann";
$info["givenName"][0] = "Hans";
$info["mail"][0] = "MustermannH#firma.de";
$info["objectclass"][0] = "top";
$info["objectclass"][1] = "person";
$info["objectclass"][2] = "organizationalPerson";
$info["objectclass"][3] = "contact";
$info["ou"][0] = "Users";
$info["ou"][1] = "contact";
// add Data...
$r=ldap_add($ldapcon, "cn=Hans Mustermann, sn=Mustermann", $info)
or die(ldap_error($ldapcon)); //error: operations error
}
}
Are there some infos missing? is the code wrong? do i need some changes on the ad-settings? is it a problem with "remote-rights-setting" or whatever?
Am I just too stupid and blind to see the problem or is it a bigger thing that is not easy to fix?
Does someone of you have an idea?
Thanks a lot
Chillikarli
Instead of the or die() condition you might try this
// add Data...
if(!(ldap_add($ldapcon, "cn=Hans Mustermann, sn=Mustermann", $info))) {
echo "There is a problem to create the account\n";
echo "Please contact your administrator !\n";
echo "LDAP Error: ".ldap_error($ldapcon)."\n";
exit;
}
ldap_unbind($ldapcon);
I work with Workbench and phpMyAdmin with mySql, I use Procedures and Function etc...
Sometimes one procedure triggers another Function and so on... eventually something goes wrong somewhere.
Is there any tool known to anybody that can show all the queries mySql runs?
Like a command prompt that shows every command MySql runs
it would be a very helpful debugging tool
Run SET GLOBAL general_log = 'ON'; , all queries will be logged to a log-file. See here
I use on a very big site this function I made:
define(test, 3);
function my_query($SQL){//this is a layer over mysql_query to get interesting data while debugging
if(test == 3){ // paranoia
$query_start = get_microtime_ms();
$exp = mysql_query("EXPLAIN ".$SQL);
$query_end = get_microtime_ms();
$query_time = $query_end - $query_start;
$explain .="\n<table border=\"1\">";
$explain .="<tr><td>id</td><td>select_type</td><td>table</td><td>type</td><td>possible_keys</td><td>key</td><td>key_len</td><td>ref</td><td>rows</td><td>Extra</td></tr>";
while( $explain_r = mysql_fetch_array($exp)){
$explain .="\n\t<tr>";
for($i=0;$i<10;$i++){
$explain .= "\n\t\t<td>".$explain_r[$i]." </td>";
}
$explain .="\n\t</tr>";
}
$explain .="\n</table>";
$r = mysql_query($SQL)or die(mysql_error(),$SQL);
$returned = mysql_num_rows($r);
print("<pre>".trim($SQL)."\n".$explain."Time spent: ".$query_time."\nReturned rows: ".$returned."\n--------------\n</pre>");
return $r;
}
if(test == 2){ // hard debugging
print("<pre>\n\n".trim($SQL)."\n--------------\n</pre>");
$r = mysql_query($SQL)or die((mysql_error(),$SQL));
return $r;
}
elseif(test == 1){ // testing
$r = mysql_query($SQL)or die((mysql_error(),$SQL));
return $r;
}
elseif(test == 0){ //production
if($_SESSION['uid'] == 59){
$only_me = $SQL;
}
$r = mysql_query($SQL)or die(
mail("me#example.com",
"[Error http://".$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI']."] Error on ".$_SERVER['SCRIPT_NAME'],
"From: ".$_SERVER['REMOTE_ADDR'].
"\nReferer: ".$_SERVER['HTTP_REFERER']."\nDate: ".date()."\nError:\n".mysql_error()."\n".$SQL.
"\nUsername: ".$_SESSION['username']."\nhttp://".$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI'].
"\nRef: ".$_SERVER['HTTP_REFERER']).
" a query went wrong Please click here to go back to home page<br><br><pre>$only_me".mysql_error()."</pre>");
return $r;
}
}
Edit your MySQL configuration file (my.cnf or my.ini) and add a log="/path/to/yourlog.log" line in the [mysqld] section. Make sure you restart MySQL to have it pick up the changes in the config file.
This should log all queries that are executed (though I'm not sure it will log the queries executed inside stored procedures).
In your database abstraction class, the best way to do it is to add a debug option that logs queries and debug information to a file, or if there is enough overhead in your application, keep it all available in a variable you can inspect at runtime. Once you've got all this information there, you can output it to a hidden div with a control, or inspect it directly with something like xdebug and netbeans.
On a general note - if you have a large application which you wish to debug, it's really worth spending a bit of time investigating breakpoints, and interactive debugging - it's not so complex to setup and extremely useful. I asked about this recently, and it's really helped me with this kind of stuff!
If your database calls aren't abstracted (shame!) then you can override core PHP functions to include a debug call as demonstrated in this useful example on the PHP site, using the APD extension. Very useful! For example, you can override mysql_query() with your own code to log the query.