Is there any error logging when mysql query fails? - php

I'm curious if there is any error logging by the MySQL database when I execute a query. I know that I call mysql_error to retrieve an error but is there some other logging on database side?

By default mysql does not log queries. But you can increase the log level at the sql configuration files. Try the file: /etc/mysql/my.cnfand uncomment the line
general_log = 1
For performance, it is off by default.

Well, if it's properly set up, then it should be. It usually is by default. You can get more info in the MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/server-logs.html
Also, if you use *nix or BSD, the logs are usually stored in /var/log/ .

what do you mean like empty querys or bad querys?
$sql = mysql_query( select * from table );
// but theres no rows you would right a if statment with mysql_num_rows
if(mysql_num_rows($sql) > 0){
}else{
// error message here for empty
}
// or
mysql_query(......)or die(mysql_error());
// this will tell you why its returnig false!

Related

Why PDO connection sowing "SQLSTATE[HY000]: General error"?

I am using pdo connection. I am trying to run a delete query but it is showing this message in the browser
*SQLSTATE[HY000]: General error*
Here is my query:
$user_id = $_POST['user_id']; $result = query($conn, "DELETE FROM user WHERE user_id = '$user_id'");
I don't know why happening this. Any kind of help will be appreciated.Thanks
I think there is a query() function does not exists in PHP .. It should be mysql_query or mysqli_query
Using Mysql query is bad because it is depreciated in Updated version of php
$result = mysqli_query($conn, "DELETE FROM user WHERE user_id = '$user_id'");
//So using mysqli :)
$result = mysqli_query($conn, "DELETE FROM user WHERE user_id = '$user_id'");
Per MySQL 5.5.35 source code, sql/sql_prepare.cc:
bool
Reprepare_observer::report_error(THD *thd)
{
/*
This 'error' is purely internal to the server:
- No exception handler is invoked,
- No condition is added in the condition area (warn_list).
The diagnostics area is set to an error status to enforce
that this thread execution stops and returns to the caller,
backtracking all the way to Prepared_statement::execute_loop().
*/
thd->stmt_da->set_error_status(thd, ER_NEED_REPREPARE,
ER(ER_NEED_REPREPARE), "HY000");
m_invalidated= TRUE;
return TRUE;
}
It appears that your error (SQL state HY000) will happen when there is a wrong sequence of prepare/execute statements. Double-check our logic to make sure you are properly using prepared statements, e.g properly fetching all of the results after the call to query() before calling it again.
If you cannot figure it out, isolate the problem to a minimal, complete, and verifiable example (https://stackoverflow.com/help/mcve), and post the code here.
UPDATE:
Does the problem go away (or do you at least get a meaningful error message) if you do
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
prior to the query?

MySql Trigger when attempting to insert into the same table and selecting the last record of the same data

I have a table T1 and firing a trigger after insert on T1 and calling external PHP programm using UDF where the app is looking for a last inserted data and do a action on condition base but it is not working as expected.. Please help as I guess that we cannot select the data from the same table where we are firing a trigger?? Is it so?
TRIGGER
DELIMITER ##
CREATE TRIGGER CALL
AFTER INSERT ON call_test
FOR EACH ROW
BEGIN
DECLARE cmd CHAR(255);
DECLARE result int(10);
SET cmd=CONCAT('php /var/www/html/test/call.php');
SET result = sys_exec(cmd);
END;
##
DELIMITER ;
call.php
function connect_db() {
$db_connection = mysql_connect("localhost","root","test") or die (mysql_error());
$db_select = mysql_select_db('testdb') or die (mysql_error());
}
connect_db();
$sql2=mysql_query("SELECT * FROM call_test ORDER BY createtime desc limit 1") or die(mysql_error());
$res = mysql_fetch_array($sql2);
if(strstr($res['name'],'go')!=false)
{
echo "inserted";
//sleep(10);
$sql4=mysql_query("insert into call_test_auto (name,createtime) values ('from UDF automatic2','".$today."')") or die(mysql_error());
}
else
{
echo "not inserted";
}
yes you can Sam. It is in row aliased by NEW. See Trigger Syntax and Examples
Your task is to do whatever is necessary to get things into variables so that you can concat and call your UDF with them as command line parameters. As seen here, where that gentleman sent a command line argument of Sarbajit to his C program he had compiled.
In your case, you are just calling PHP and nothing is happening!
Enabling sys_exec
It is not as if your average Joe is likely to even going to survive the call to sys_exec() without receiving a Syntax Error. See This Question on the Stack and the link for github at top of that question.
Things Failing silently
Remember that mysql Triggers and Events (as in Create Event) run and succeed to your wishes or don't, but do so silently. You might not even know that the above Syntax Error occurred. That would not be the case for a Stored Procedure run by a user in a query that had error reporting.
Of course, a Trigger or Event could call a Stored Procedure, but there is no UX to that, so sys_exec() syntax errors would go unnoticed.

php mysql get row

I am trying to use the mysqli to access a mysql database and a table called phptest, with the code below. I am trying to check if row is = 0 or something else but it does not echo anything. What is wrong with the code?
$mysqli = new mysqli("localhost", "root", "", "phptest");
if (!$mysqli) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$query = "SELECT `id` FROM `test` WHERE `email`='example#hotmail.com' AND `password`='5f4dcc3b5aa765d61d8327deb882cf99'";
$query_run = $mysqli->query($query);
$row_cnt = $query_run->num_rows;
if ($row_cnt==0) {
echo 'wrong..';
} else {
echo 'correct!';
}
EDIT: edited the variable in the if statement. I have runned the query directly in the mysql panel in the tabel. I get the right row count then. Why doesnt it work in php ?
Although you are checking for an undefined variable $row_count (which should be $row_cnt, that isn't your immediate problem (you should still fix this). You are getting a fatal error either because:
Your query is failing. Your query may be failing because of a missing field on the table or indeed you are missing the actual table in the database. The query will return FALSE if it fails, but you aren't checking for that, instead you try to access a property of the result. If the query fails, there is no result object, it is a boolean FALSE and will produce a fatal error something like Fatal Error: Trying to access a property of a none object.
You don't have the MySQLi library configured for your PHP installation
To further debug you should turn on error reporting as below, or consult your server's error log.
error_reporting(E_ALL);
ini_set('display_errors', '1');
One thing is that you defined $row_cnt and then checked for $row_count, but that's just a quick look at your program, so there might be something else wrong too.

Database connection problems in PHP/MySQL

When I run the script below with the added line,
$count = 1;
I get a value of 1 on the screen, but when I take that line out I don't get get anything at all. tried moving it above the $count=mysql_num_rows($result); and I still didn't get a value.
$sql="SELECT EMAIL FROM CUSTOMER WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$count = 1;
echo $count;
What am I doing wrong here? I have never used PHP until now. The error is:
Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)
This means that your MySQL server socket (/var/run/mysqld/mysqld.sock) is either missing or corrupt.
It could also mean that MySQL service is not working right, try restarting in SSH using:
$ service mysqld restart
If it says the service is missing, then say:
$ service mysql restart
I would guess at an a error with your SQL query (var_dump($count); would return false in this case).
To check this, I would do 2 things:
After your query do if(!$result) echo mysql_error( ); - this will show you any errors that happen while talking to the database
To check your SQL is being formed correctly, create it in a variable (e.g. $sql = "SELECT email ...";) and echo it out.
EDIT: Ahh just seen the update - it's a connection issue. Check that your mysql_connect( ) has the right host, username & password. Otherwise it could be a problem on your system (e.g. firewall or similar)
EDIT 2: As has been rightly pointed out, mysql_connect details would cause a different connection error to the one you're seeing. I've had a quick Google for it, and this cropped up http://lists.mysql.com/mysql/204035. Not sure it'll be any use as I've not read it through, but it does describe some steps for the solving this problem on someone else's system.
When you can't connect successfully to the database, return value from mysql_query function ($result) would not be a valid value. so when you give it to mysql_num_rows function, it fails & returns FALSE value, which has no visual effect on output screen!

Executing SQL directly; no cursor in PHP when calling a stored procedure

I'm getting the following error when trying to execute a stored procedure from PHP using sqlsrv driver:
Executing SQL directly; no cursor
Add a cursor option to your connection:
$connection = odbc_connect('host','username','password', 1)
or die('Connection failed!');
After password insert cursor_option 1 <-- this option was missing.
I got the same error when I used code that basically did this:
$options = array( "Scrollable"=>SQLSRV_CURSOR_STATIC);
$sql = "exec dbs_webgetnextcount 'expense'";
sqlsrv_query($db,$sql,null,$options);
I fixed it by not setting the cursor.
$sql = "exec dbs_webgetnextcount 'expense'";
sqlsrv_query($db,$sql);
PS. I was doing the CURSOR_STATIC option on my select statements so that sqlsrv_num_rows() would give a correct number.
I had the same problem as Jon but I figured since it is just a warning generated by the sql data access component and the query still completes successfully you can disable it in sqlsrv.exe using sqlsrv_configure("WarningsReturnAsErrors", 0);
Could be a permissions issue:
http://griffo.info/tellme/2009/05/executing-sql-directly-no-cursor/

Categories