Entries Tagged 'MySQL' ↓

MySQL Case sensitive and Case insensitive search with Match and Like

This post teaches you how to set your MySQL search queries to be Case sensitive or case insensitive.

It's all in the database collation. MySQL by default search case insensitive. In phpMyAdmin or or Putty change the Collation of your fields. Collations that are binary search case-sensitive. Collations that end with _ci like latin1_swedish_ci are case insensitive (ci is an acrynm for case insensitive.

To change your Collations quickly run this code in a loop:

ALTER TABLE tablename CONVERT TO CHARACTER SET latin1 COLLATE latin1_swedish_ci;

Create a Javascript Array with PHP from a MySQL Database Table

Wrote this function to check email addresses already in a database, so I dont have to change pages, and check with javascript. Problem: it exposes your whole email list to the public in the source code, and the source code can become a monster length. So it isnt practical, but it works. (I'm not using it to check emails)

The script proves useful to do other things with javascript and spawn arrays to manipulate data... etc.

function jarray(){

$sql = "SELECT * FROM table_name LIMIT 0, 100000000";
$jarray = "var your_array = new Array();\n\t\t";
$i = 0;
$q = mysql_query($sql);
while($object = mysql_fetch_object($q)){
$jarray = $jarray."your_array[{$i}] = \"{$object->field_name}\";\n\t\t";
$i++;
}

print $jarray."\n";
}

Put your own info where the Text is bold

PHP MySQL Count Query Using Where Clause

Here is an example of a function that was created to count the number of book spreads attached to a certain book number. The problem was there were more than one book number attached to a whole table of bookspreads. By using SQL COUNT and WHERE it is possible to find the total count of entries with the same field input.

$book // the book number i wanted to count if a spread was attached to it

function spreadcount($book){
$query = "SELECT COUNT(*) FROM table WHERE book={$book} ";
$result = mysql_query($query) or die(mysql_error());
echo mysql_result($result,0); // 0 pulls the first result which is our magic number.

}