PDA

View Full Version : Few Perl/PHP/mySQL questions


ilya
02-22-2000, 02:05 PM
I got few more questions about working with mySQL and some Perl/PHP stuff.. :)
1) In Perl, we use subroutines, right? I.e.:

if ($one==1) {&one;}
else {&two; }

Is there something similar to that in PHP?

2) How do I add columns to mySQL table through Perl? Say, i have a table that contains the following fields: FirstName, LastName, Email. I also want to add a new one - PhoneNumber. How do I do it?

3) In SELECT statements I can specify what some field is equal to (ie: SELECT * FROM table WHERE FirstName=ilya). Can I do the same but if the field contains specific query?

Thanks in advance.
------------------
Thanks for reading.
Webmaster of ScriptBox.NET Forums (http://www.scriptbox.net).
Webmaster of Ubb Code Hackers Hideout (http://www.ubbcodehacking.com)

urban
02-22-2000, 02:24 PM
They are called Functions in PHP and they are quite similar in appearance to C functions.[nbsp][nbsp]In PHP3, functions MUST be declared before they are used and they are simply called by name (no &) , this is different than Perl.


function my_function ($arg1) {
[nbsp][nbsp]echo "this function doesn't do much\n";
[nbsp][nbsp]echo "it just prints the first argument: $arg1\n";
[nbsp][nbsp]echo "and then returns arg1 as the function result.\n";
[nbsp][nbsp]return $arg1;
}


You probably want to check out the PHP manual, this stuff is covered pretty well -- http://www.php.net

You can add columns through Perl using ALTER TABLE, but it's probably not a good idea.[nbsp][nbsp]If you want to add a column to the table, you should [probably] use the MySQL monitor (via telnet) or a MySQL GUI utility like urSQL (Windows/ODBC) or mysqladmin (Linux) or PHPMyAdmin (Web).


alter table table_name add PhoneNumber varchar(16);


I'm not sure what you're asking with the third question...
<!-- NO_AUTO_LINK -->
[This message has been edited by urban (edited 02-22-00@1:24 pm)]

ilya
02-22-2000, 02:43 PM
Thanks for the quick responce!
1) I didn't really get the first one. Is this how I'd do it in PHP?
if ($abc==&quot;abc&quot;) {abc_function;}
function abc_function {
echo &quot;Just prints out ABC\n&quot;;
}

2) The reson I need this, is i'm writing a script, and for the next versions I might add new fileds to tables. If i can just add fields, the users won't have to create a whole new table and start fresh.

3) I already figured that out :). What I meant by that, was this: Say I have a table with those 3 fields. I have 10 records in it. I can search by specifying SLECT * FROM table WHERE FirstName=Mike. In that case, the script will look only for FirstName fields whose value is EXACTLY Mike. I need to display those, that contain Mike. I got it already:
SELECT * FROM table WHERE FirstName REGEXP &quot;[Mike]&quot;;

urban
02-22-2000, 03:03 PM
PHP3 requires that the function be declared BEFORE it is called.

So, first define the function/subroutine:

function abc_function {
[nbsp][nbsp]echo &quot;Just prints out ABC\n&quot;;
}

And then after this declaration/definition, you can call the function at will:

if ($abc == &quot;abc&quot;) {
[nbsp][nbsp]abc_function;
}

Shalazar
02-22-2000, 03:35 PM
ilya -

The wildcard % will allow you to represent any character in a MySQL query.[nbsp][nbsp]Therefore, if you'd like to query records containing &quot;mike,&quot; then you could use:


mysql_query (&quot;SELECT * FROM table_name WHERE (FirstName LIKE &quot;%mike%&quot;)&quot;);


And instead of using (FirstName = &quot;Mike&quot;) you rather use LIKE, which matches patterns.[nbsp][nbsp]And when compounded with the wildcard before and after, should find all instances of Mike in your entries.

That statement will allow you to query the FirstName column for all instances of records containing &quot;mike.&quot;[nbsp][nbsp]So it would successfully query entries like:

Mike
Mikelle
Bromike
Jomikech

I dunno who would be named Jomikech, but you get the idea.

And if you needed Mike to fall in a particular order...say you need only those entries where Mike is the beginning, you change the order of your wildcards:

(FirstName LIKE &quot;mike%&quot;)

Or if you wanted to see where mike was at the end:

(FirstName LIKE &quot;%mike&quot;)

And so on.

[This message has been edited by Shalazar (edited 02-22-00@2:42 pm)]

Justin
02-22-2000, 04:08 PM
In Perl, the &amp; is no longer needed (since Perl 5 I believe)... It used to be that if you were calling a function that has not yet been declared you needed the &amp;, but this no longer applies in my experience. I find it cleaner to use it sometimes only because it helps to determine my own subs from built-in subs at a glance...

For creating/altering tables, you could pass the query just as you would any other query - have it alter the table and check the result (for success or failure)...

By definition, a sub or subroutine is a routine that is called but returns nothing. A function is a routine that has a return value. However, in Perl a sub does both, as does a function in PHP. I'm not sure why they both took different routes but they did... Basic is the only language I know of that actually differentiates between the two, but it is a long standing standard that is pretty much no longer adhered to in most languages now...

At any rate, one thing to keep in mind with functions in PHP is that variables are localized to the function - for example:
</font><font face="Courier" size="3">
$name = &quot;Justin&quot;;

function print_name {
[nbsp][nbsp] print &quot;The name is $name.\n&quot;;
}

print_name();
</font><font face="Verdana, Arial" size="2">
The output would simply be:
</font><font face="Courier" size="3">
The name is .
</font><font face="Verdana, Arial" size="2">
You would need to declare the variable as a global, in one of two ways:
</font><font face="Courier" size="3">
$name = &quot;Justin&quot;;

function print_name {
[nbsp][nbsp] global $name;
[nbsp][nbsp] print &quot;The name is $name.\n&quot;;
}

print_name();
</font><font face="Verdana, Arial" size="2">
Or:
</font><font face="Courier" size="3">
$name = &quot;Justin&quot;;

function print_name {
[nbsp][nbsp] print &quot;The name is $GLOBALS[name].\n&quot;;
}

print_name();
</font><font face="Verdana, Arial" size="2">
Both of the above examples would yield:
</font><font face="Courier" size="3">
The name is Justin.
</font><font face="Verdana, Arial" size="2">
Hope this helps.

------------------
Justin Nelson
FutureQuest (http://www.FutureQuest.net/index.php) Support

ilya
02-22-2000, 04:38 PM
How come this doesn't work?

<?
$name = &quot;Justin&quot;;

function print_name {
[nbsp][nbsp] global $name;
[nbsp][nbsp] print &quot;The name is $name.\n&quot;;
}

print_name();

?>

The error I get is this:

&quot;Parse error: parse error, expecting `'('' in C:\Inetpub\cgi-bin\test.php3 on line 4&quot;

Any suggestions?

ilya
02-22-2000, 04:40 PM
Got it fixed already :)
Just needed to add () in function name, ie:
function print_name() {
}

Justin
02-22-2000, 05:18 PM
Oops - you caught me :)[nbsp][nbsp]Yes, the code was untested - I frequently post untested code, so usually it will require some tweaking before it will work - glad you figured it out :)

------------------
Justin Nelson
FutureQuest (http://www.FutureQuest.net/index.php) Support

urban
02-22-2000, 05:21 PM
My only point was that in PHP, subroutines are called functions.


By definition, a sub or subroutine is a routine that is called but returns nothing. A function is a routine that has a return value.[nbsp][nbsp]However, in Perl a sub does both, as does a function in PHP. I'm not sure why they both took different routes but they did... Basic is the only language I know of that actually differentiates between the two, but it is a long standing standard that is pretty much no longer adhered to in most languages now...

Actually, By definition a subroutine is a collection of statements that can be used repeatedly.[nbsp][nbsp]Most modern languages have subroutines.[nbsp][nbsp]However, each language implements these using different concepts.[nbsp][nbsp]

A function is a subroutine.[nbsp][nbsp]The word subroutine is used to describe a high-level programming concept (a group of commonly used code).[nbsp][nbsp]Function, Procedure, Sub are used to implement the concept of a subroutine in each respective language.

Pascal, for example, uses two different keywords to differentiate subroutines that return a result (functions) and subroutines that do not (procedures), but they are both subroutines.[nbsp][nbsp]In C and PHP (as well as several other languages), all subroutines are functions, regardless of whether or not a result is returned.[nbsp][nbsp]

If you look closely at C and PHP documentation you'll find that there is no such thing as a subroutine that isn't a function (avoiding classes).[nbsp][nbsp]<!-- NO_AUTO_LINK -->