Get an array in php mysql error like oci_error - php

Since some time ago I have been programming in php with oracle, and I've always liked the function oci_error() because the function can give me the byte position of the error in the query (offset) and then I can make a function to show the error with a flag indicating where is the error in the query.
Well I'm making a similar function but with mysql:
I have the mysql_error() which gives me the "message".
I have the mysql_errno() which gives me the "code".
The "sqltext" I can get it with the function itself.
But, How can I get the offset value of the error in the query?

MySQL errors are usually of the type
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near...
Following the "near" keyword is the part of the query where the error occurred. You could search for this string using strpos() in your query and use that as the offset.

Related

How to convert into Title Case in mysql query builder in laravel

I just want to ask how to convert the data I'm getting from database in Title case, its giving me an error:
Syntax error or access violation: 1064 You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use
I'm just using UCASE and LEFT, here's my code,
DB::raw('GROUP_CONCAT(DISTINCT " " ,
CONCAT(UCASE(LEFT(ru.firstname, " ", ru.lastname, 1)))) AS relates_to')
The output I want for example is "user name" into "User Name", oh and I'm using the latest MySQL Workbench just to inform you
Hope you can help me and notice me, thank you so much in advance.
The LEFT function only takes two parameters, so this alone would cause your current code to fail. You may try reworking it as follows:
DB::raw("GROUP_CONCAT(DISTINCT ' ' , LEFT(UCASE(CONCAT(ru.firstname, ' ', ru.lastname)), 1)) AS relates_to")
I don't know what your logic is exactly trying to achieve, but the above should at least run without error.

Which class they are telling about in the returned value of PDO::errorCode (void) in php?

As we know that PDO::errorCode ( void ) method returns the SQLSTATE where the returned value is the alphanumeric identifier of 5 characters, where the first two characters are the value of Class and the rest of the three characters are the value of the SubClass. My question is that about which Class & SubClass they are telling ? Either it is PDO class or anything else ?
Reference Link: PDO->errorCode(void)
Please can any one tell ?
Thanks !!!
This class and subclass has nothing to do with PDO or PHP classes, or object orientation at all. It just refers to the way that errors are categorized in SQL. As it says in the documentation, the error code is
a five characters alphanumeric identifier defined in the ANSI SQL-92 standard. Briefly, an SQLSTATE consists of a two characters class value followed by a three characters subclass value.... A class value of 01 indicates a warning...Class values other than 01, except for the class IM, indicate an error. The class IM is specific to warnings and errors that derive from the implementation of PDO
So except for class IM, these codes come from the database. They're the same codes you'll see if you query the database using command line tools and get an error, e.g.
mysql> select a from foo bar baz;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'baz' at line 1
42000 is the error code, its class is 42 and subclass is 000. If you made this same query using PDO, $pdo->errorCode() would return "42000", and $pdo->errorInfo would return an array like:
[ "42000",
1004,
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'baz' at line 1"
]

What is the Mysql query for displaying the code of a user defined function in mysql database for mysql version 5.0.45?

Please any body could suggest me as I am using this query
SHOW FUNCTION CODE 'function_name'.
But displaying error as given below:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Use SHOW FUNCTION CODE function_name
See MySQL docs https://dev.mysql.com/doc/refman/5.1/en/show-function-code.html

MYSQL IN Clause error

I have used the below code in mysql query:
$all_PIDs=array();
foreach($pID as $p)
{
$all_PIDs[]=$p->ID;
}
$AIDS=implode(',',$all_PIDs);
$table_tsk = new Timesheets_Table_Tasks();
$select_tsk = $table_tsk->select()
->from($table_tsk, array
(
'Total'=>'SUM(timesheets_tasks.Time)',
'Charged'=>'SUM(timesheets_tasks.Time_Charged)'
))
->where('timesheets_tasks.ProjectID IN ('.$AIDS.')')
;
But using the above code I am getting the following error:
"An error has occured
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1"
I have added a quotation mark(") for IN clause. But the problem is the query only displays for the first $AIDS number. Could someone help me to clear the error?
Thanks!
It should be specified as:
->where('timesheets_tasks.ProjectID IN (?)', $all_PIDs)
so you're passing an array of integers, not the comma-separated list of it
On your codes the quotes are not part of your MySQL query but only your PHP portion. DO this
$AIDS= "'".implode("','",$all_PIDs)."'";
And then
>where('timesheets_tasks.ProjectID IN ('.$AIDS.')'

Difference between mysql_errno and mysql_error

Can anybody explain the difference between mysql_errno and mysql_error?
mysql_errno returns the error code, while mysql_error returns the error text...
Are you saying the functions of php ?
mysql_errno returns the number of the error,
and mysql_error returns the text of the error.
You can easily find the difference in http://www.php.net/manual/en/function.mysql-errno.php
string mysql_error ([ resource $link_identifier ] )
Returns the error text from the last MySQL function. Errors coming back from the MySQL database backend no longer issue warnings. Instead, use mysql_error() to retrieve the error text. Note that this function only returns the error text from the most recently executed MySQL function (not including mysql_error() and mysql_errno()), so if you want to use it, make sure you check the value before calling another MySQL function.
mysql_errno is the number of the error
mysql_error — Returns the text of the error message from previous MySQL operation
mysql_errno — Returns the numerical value of the error message from previous MySQL operation
I would add to the topic that MySQL-specific error numbers returned by mysql_errno() are not the SQL error numbers you could expect, they differ from SQLSTATE values (returned by mysql_sqlstate() or sqlstate property inside a mysqli_sql_exception). You can find a complete list of error messages and error numbers for your MySQL distribution here (in addition to the Docs/mysqld_error.txt file).
Source: MySQL Reference Manual

Categories