MySQL

MySQL errors into PHP?

Here are some examples of how to utilize mysql_error ()

mysql_connect("your.hostaddress.com",
"username", "password") or die(mysql_error()) 

This will return an error if there is a problem connecting to your MySQL database

 $value = mysql_query($your_query)
 or die("A MySQL error has occurred.<br />Your Query: " .
 $your_query . "<br /> Error:
(" . mysql_errno() . ") " . mysql_error())

When you encounter an error, this will return your custom message (A MySQL error has occurred.<br />) followed by a line number, and the actual

thanks http://php.about.com/od/phpwithmysql/f/mysql_error.htm

Quotes in mysql queries – security issue

Remember to check numeric data as well. If an application generates a query such as SELECT * FROM table WHERE ID=234 when a user enters the value 234,the user can enter the value 234 OR 1=1 to cause the application to generate the query SELECT * FROM table WHERE ID=234 OR 1=1.As a result, the server retrieves every row in the table. This exposes every row and causes excessive server load. The simplest way to protect from this type of attack is to use single quotes around the numeric constants: SELECT * FROM table WHERE ID='234'.  If the user enters extra information, it all becomes part of the string. In a numeric context, MySQL automatically converts this string to a number and strips any trailing nonnumeric characters from it. It means that if the user enters 234myname the value remains 234. Another option is to do a check before the mysql query if the value is numeric.

Source http://dev.mysql.com/doc/refman/5.0/en/security-guidelines.html

Mysql auto Datetime timestamp

On default valeu of timestamp add this  CURRENT_TIMESTAMP

Mysql Select within Select (sel into sel)

SELECT
clients.id,

(SELECT Count(clients_job.id)

FROM
clients_job

WHERE
clients_job.client_id =  clients.id   ) AS JobCounter

FROM 
clients

ORDER
BY EPONIMIA ASC

PHP mysqli::set_charset

In connection file add the following lines

$mysqli = new mysqli($hostname_###,$username_###,$password_###,$database_###);
$mysqli->query(“SET NAMES ‘utf8′”);

ORDER BY using specific order from IN

You can use FIND_IN_SET to do that:
SELECT * FROM tablename WHERE id IN (1,10,8,5) ORDER BY FIND_IN_SET(id, ’1,10,8,5′)

Php, MySql, .htaccess: friendly urls

Php, MySql, .htaccess: friendly urls

There are a lot of tutorials that show how to change an address like:

  • www.mysite.com/products.php?product_id=1234

into others like:

  • www.mysite.com/products/1234
  • www.mysite.com/products/1234.html

In this article I’d like to go one step forward and to create something like:

  • www.mysite.com/easy-to-remember-product-name
  • www.mysite.com/easy-to-remember-product-name.html More >

mysql console backup restore

Question: How to backup the whole database?

Answer: Just omit the “table_name” argument. For example, mysqldump -u root -pwordpress > D:\wordpress.sql

Question: How to backup all the databases in MySQL?

USE –force to continue on errors

Answer: Use “–all-databases”. For example, mysqldump -u root -p –all-databases > D:\db.sql

linux mysqldump -u admin -p –all-databases > db.sql

Restoring

Now let’s to restore it. Enter the following command in the command line window (or shell):

mysql -u root -p wordpress < D:\wordpress_posts.sql

This will restore the “wp_posts” table. You need to specify the database name. Here, it is “wordpress“. The following command restore the whole database:

mysql -u root -p wordpress < D:\wordpress.sql

If you want to restore all the databases (you used “–all-databases” argument to generate a backup file before), you don’t need to specify the database name.

mysql -u root -p < D:\db.sql

mysql: “not cleanly closed and upgrade needing tables” problem

I have used mysql server on my laptop with gutsy on xfs file system. I have tested on hardy version and have same output as following

Quote:
sudo /etc/init.d/mysql start
* Starting MySQL database server mysqld [ OK ]
* Checking for corrupt, not cleanly closed and upgrade needing tables.
I google and couldn’t find a clean solution. I have already do

Quote:
myisamchk *.MYI
myisamchk –recover *.MYI
myisamchk –safe-recover *.MYI
But unfortunately problem still there. I need your help about why and how this can be solved. Thank a lot for your help.