Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question regarding mySql and select line continue

Status
Not open for further replies.

rileypetty

Programmer
Jan 20, 2007
43
0
0
US
I'm attempting to rewrite all my asp/mssql in php/mysql and I have a problem with continuing the select statement string on a separate/second line.
In asp my Select statement would look partially like this -

strSQL = "SELECT books.recid AS recid, books.date_created AS cdate, "
strSQL = strSQL & "books.au_fname AS fname, books.au_mname AS mname, "
strSQL = strSQL & "books.au_lname AS lname, books.title AS title "


and it worked just fine but in mySQl/php I cannot get the darn thing to work. It works fine when I keep the select statement line to a single line but when I go to a second line...well. I've tried different combinations of syntax but no luck.

My working mysql/php looks like this - $sql = "select blog-recid as recid, picdesc as pdesc from blogpic order by blog-recid; "

I would like it to look like this ----

$sql = "SELECT blog_recid as recid, picdesc as pdesc, picowner as powner; "
"FROM blogpic; "
"ORDER BY blog_recid; "

I would appreciate any help someone may be willing to give me. Riley


 
Hi

First of all, this is pure PHP question and has nothing to do with any SQL dialect.

Then do not put semicolons ( ; ) in the middle of SQL statement.

Your options would be :
PHP:
[gray]// concantenate strings[/gray]
[navy]$sql[/navy] [teal]=[/teal] [i][green]"SELECT blog_recid as recid, picdesc as pdesc, picowner as powner "[/green][/i] [teal].[/teal]
[i][green]"FROM blogpic "[/green][/i] [teal].[/teal]
[i][green]"ORDER BY blog_recid "[/green][/i][teal];[/teal]

[gray]// append strings[/gray]
[navy]$sql[/navy] [teal]=[/teal] [i][green]"SELECT blog_recid as recid, picdesc as pdesc, picowner as powner "[/green][/i][teal];[/teal]
[navy]$sql[/navy] [teal].=[/teal] [i][green]"FROM blogpic "[/green][/i][teal];[/teal]
[navy]$sql[/navy] [teal].=[/teal] [i][green]"ORDER BY blog_recid "[/green][/i][teal];[/teal]

[gray]// use a single multiline string[/gray]
[navy]$sql[/navy] [teal]=[/teal] [i][green]"SELECT blog_recid as recid, picdesc as pdesc, picowner as powner[/green][/i]
[i][green]FROM blogpic[/green][/i]
[i][green]ORDER BY blog_recid"[/green][/i][teal];[/teal]

[gray]// same as above with heredoc notation[/gray]
[navy]$sql[/navy] [teal]= <<<[/teal]END_OF_STRING
[i][green]SELECT blog_recid as recid, picdesc as pdesc, picowner as powner
FROM blogpic
ORDER BY blog_recid[/i][/green]
END_OF_STRING[teal];[/teal]

Feherke.
feherke.ga
 
Thank you very much for the help and I'll make sure I'm in the php forum next time. Riley
 
While Feherke has given you all the options, you could also do with your old way, just use the PHP way to concatenate strings with dot, not with ampersand. The whole rest of development langauges uses a plus as concatentaion operator, but that aside it's simply also possible to stay with your old way and simply replace & with .

Code:
strSQL = "SELECT books.recid AS recid, books.date_created AS cdate, ";
 strSQL = strSQL . "books.au_fname AS fname, books.au_mname AS mname, ";
 strSQL = strSQL . "books.au_lname AS lname, books.title AS title; ";

As you see you also need semicolons at the end of each line, but outside the strings, each PHP command needs to end with a semicolon, you execute three assigment commands here in this code version. The only semicolon needed inside the strings is at the very end of the sql query, that's optional with a single query and is the only thing related to MySQL in your code.

PHP like most any langauge differs form VB in not needing a command continuation character like VBs underline. Continuation of code needs no specific character, even in Feherkes "use a single multiline string" example. Instead you end every command with a semicolon. Outside of any string.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top