Posts Tagged ‘MySQL’

How to order the results of MySQL query?

Usually most of the work done with MySQL involves pulling down data from a MySQL database. In MySQL, data is retrieved with the “SELECT” keyword. Think of SELECT as working the same way as it does on your computer. If you wanted to copy some information in a document, you would first select the desired information, then copy and paste.

How to limit the number of rows returned in MySQL?

MySQL supports the LIMIT keyword, which allows you to control how many rows are returned; ideal when displaying data over many pages. You can use LIMIT in your sql query like this:

SELECT name FROM table LIMIT 10

if you want to get the rows between 10 and 20 do the following:

SELECT name FROM table LIMIT 9, 10

How to insert the date into MySQL table?

Specifying the dates on which the content is entered is of prime importance for the structuring and the chronological arrangement of articles, posts and replies in a dynamic website. MySQL comes with several data types for storing dates in its database system: DATE, TIMESTAMP, DATETIME and YEAR.

The default way to store a date in a MySQL database is by using DATE. The proper format of a DATE is: YYYY-MM-DD. If you try to enter a date in a format other than the Year-Month-Day format, it might work but it won’t be storing the dates as you expect.

In order to run a MySQL Insert command and add the current date into your table you can use MySQL’s built-in function CURDATE() in your query.

How to include a conditional in my MySQL query?

You will usually need to apply some logic to the query that you apply to a table, and this is easily done with the WHERE keyword.
This will be followed by a fieldname, or list of them, and then the condition you want to be met.
Thus if you want to select only rows from a table WHERE the user is called Quentin you would do the follow:

$peeps = mysql_query("SELECT * FROM users WHERE firstname = 'Quentin'");
?>

How to group the results of a MySQL query?

If you want to group the results by a particular field value rather than listing all results individually, then you use the GROUP BY keyword.
Here is how the query might look:
SELECT columns FROM Table WHERE clause GROUP BY column ORDER BY column
Common mistakes are to apply the GROUP BY either before the WHERE clause or after the ORDER BY – make sure you get it right.
Also remember that you are returning aggregate data rather than every result as an individual row, therefore it can take a little bit of careful thinking as to when you should use GROUP BY and a realisation of what will be returned when you do.

How to find the number of rows affected by a query?

With mysql it is usually just a case of learning which function to use, and this is no exception.

The function you will want is mysql_affected_rows which you call after the query has run, and it will tell you how many rows were affected.

How do I connect to MySQL from PHP?

The first thing you will need to do when using PHP and mySQL is to connect to the mySQL database from PHP.
Luckily there are a couple of functions in PHP that make it easy. Once you get it working you can just copy and paste at the top of each script that requires the database or use a template or include file of some sort to include the details.

And that’s all there is to it – use your username, host, password and database name and this will establish the connection.
The most common problem is that you get the password wrong so double check it’s right if you get errors.

How do I access the current time with MySQL?

Another common query is to get to the current time in addition to the date within mysql.
When you want to log when something happens – for instance a post to your message board, this will be a particularly useful thing to know.
With the current time and date you will need to use NOW() like so:

$update = mysql_query("UPDATE my_users SET lastlogin = NOW() WHERE username = 'user'");
?>

How can I show an image from MySQL?

You can store a image in a database making the database table field of BLOB type. Later you can query the database fecth the content of image as usual and display this fetched-image on a webpage (or whatever the way like) using PHP headers.

What is MySQL hostname?

The hostname of your MySQL database is ‘localhost’. You will find also this information when you create any database in DirectAdmin server control panel.