Posted October 03, 2011


When building a SQL string, I have to do something like this -
"Hi my name is " & "'" & [name] & "'" & " , how are you?" or "[name] = 'John Doe'"
Your line could be simplified to:
"Hi My name is \"" . [name] . "\", How are you?"
With single quotes you can even leave out the escape character
'Hi my name is"' . [name] . '", how are you?'
the . is the "standard" for combining string objects in PHP OOP - though the & character does come in handy when adding numeric values to the display.
The \ is an escape character telling PHP to literally parse the next character in line [allowing you to shorten lines of code that print out specialty characters - to do an escape character print out you escape it [eg \\].
So an example normal MySQL PHP query string would be something like:
$connect = mysql_connect($host, $username, $password);
mysql_select_db($dbname);
$query = 'SELECT * FROM Names'; // Obviously you would want to make it more specific to what name to retrieve
$result = mysql_query($connect, $query);
$row = mysql_fetch_array($result);
// add your loop structures here for more complex result parsing
$name = $row['name];
mysql_close();
echo 'Hello my name is "' . $name . '", how are you?';
I tend to work with custom functions and classes in my PHP scripts - that makes my work more modular and much easier to modify if an error should pop up.
[For example my db connection on the "display" page would look like $conn = $db->connect();]