Archive for the ‘Scripting / Coding’ Category
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
The server failed to fulfill an apparently valid request.
Response status codes beginning with the digit “5″ indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and indicate whether it is a temporary or permanent condition. Likewise, user agents should display any included entity to the user. These response codes are applicable to any request method.
500 Internal Server Error
A generic error message, given when no more specific message is suitable.
Posted in Scripting / Coding, Web Hosting Questions
The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server should include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents should display any included entity to the user. These are typically the most common error codes encountered while online.
403 Forbidden
The request was a legal request, but the server is refusing to respond to it. Unlike a 401 Unauthorized response, authenticating will make no difference.
Posted in Scripting / Coding, Web Hosting Questions