Archive for the ‘MySQL’ Category
When you create your website database you always use DirectAdmin server control panel to create one. While creating the database you will be asked to enter databse name, username and password. The username with password requires to use in order to login to phpMyAdmin.
1. Enter URL http://yourdomain.com/phpmyadmin.
2. Use your username and password those you have entered while created the database.
Posted in DirectAdmin Control Panel, General Questions, MySQL, Web Hosting Questions
In order to create MySQL database which is essential thing for most CMS and blog systems you must:
1. Login to DirectAdmin server control panel.
2. Locate MySQL Management.
3. Click on Create new Database.
4. Enter Database Name, Database Username, Username Password, Confirm Password.
5. Click Create.
Posted in DirectAdmin Control Panel, MySQL, Scripting / Coding, Web Hosting Questions
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.
Posted in MySQL, Scripting / Coding
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
Posted in MySQL, Scripting / Coding
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.
Posted in MySQL, Scripting / Coding
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'");
?>
Posted in MySQL, Scripting / Coding
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.
Posted in MySQL, Scripting / Coding
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.
Posted in MySQL, Scripting / Coding
If you are adding data to a mySQL table then you will often want to know the ID of a row you have just inserted so that you can reference it elsewhere. Often you’ll add to a table with an auto_increment ID field and then want to use this to store other information on other tables.
Here’s how you do it:
$q = mysql_query("INSERT INTO mytable VALUES('test')");
$rownumber = mysql_insert_id();
//$rownumber contains the id of the new row
?>
Posted in MySQL, Scripting / Coding
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.
Posted in MySQL, Scripting / Coding