Feeding SQL error into variable for use later on in code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
struggling_student
Forum Newbie
Posts: 15
Joined: Mon Feb 23, 2004 3:51 pm

Feeding SQL error into variable for use later on in code

Post by struggling_student »

In my code I have to run a query to add a database at the beginning of the code so that the table will be used in my sidebar which is included in the code.

The problem is that if the query fails due to an incorrect database name or a duplicated table name i can't output the error in the correct place on the page.

Is there anyway i can feed any error into a variable and call it at the appropriate place in the code?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

You probably want to use [php_man]mysql_errno[/php_man] or [php_man]mysql_error[/php_man]. The former is better if you don't want to give out information about database table names etc which might assist hackers.
Last edited by McGruff on Tue Aug 09, 2005 11:56 am, edited 1 time in total.
penguinboy
Forum Contributor
Posts: 171
Joined: Thu Nov 07, 2002 11:25 am

Post by penguinboy »

To set it to a variable:

Code: Select all

mysql_query() or ($mysql_error = mysql_error());
Or, if you have multiple querys, set it to an array element:

Code: Select all

mysql_query() or ($mysql_error[] = mysql_error());
struggling_student
Forum Newbie
Posts: 15
Joined: Mon Feb 23, 2004 3:51 pm

Post by struggling_student »

Excellent! Thanks alot!
Post Reply