php-general Digest 6 Jul 2008 09:47:13 -0000 Issue 5553

2008-07-06 Thread php-general-digest-help

php-general Digest 6 Jul 2008 09:47:13 - Issue 5553

Topics (messages 276321 through 276329):

Re: class_is_loadable?
276321 by: Aschwin Wesselius
276323 by: Eric Butera

Re: odbc msaccess php5
276322 by: Peter Jackson

Re: Trying to keep a dropdown selection sticky
276324 by: Warren Vail
276325 by: Michael S. Dunsavage
276326 by: Michael S. Dunsavage

Re: Asynchronous PHP Execution
276327 by: Waynn Lue
276328 by: Waynn Lue
276329 by: Richard Heyes

Administrivia:

To subscribe to the digest, e-mail:
[EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]

To post to the list, e-mail:
[EMAIL PROTECTED]


--
---BeginMessage---

Pulni4kiya wrote:

Well reimplementing autoloading doesn't seem such a bad idea.
With the integrated autoload ...there is one very stupid way of doing 
what you want. Something like this (I suppose you know which class is 
the parent of the one that is 'missing'):

eval(class $class_name extends THE_PARENT {});
You can put a field with the actual class name and fill it in the 
constructor so you would know if it's the actual class B or just A 
with a different name.


(What I just wrote looks very stupid... Don't laugh at me very much 
please. :D)


I'll think of something smarter...this is the first thing that came 
into my mind.


Btw why is it so important to use autoloading anyway?


Hi,

Has anybody used the PECL extension automap yet and if so, are there 
issues with using that to autoload or not?


Greetings,

Aschwin Wesselius
---End Message---
---BeginMessage---
On Sat, Jul 5, 2008 at 1:36 PM, Larry Garfield [EMAIL PROTECTED] wrote:
 Greetings, all.

 I am trying to figure out a way to implement the following logic, but I am not
 sure if it is possible to do so without a lot of additional side work:

 I have a class, A, and another class B that extends A.  They live in separate
 files.  The logic I need to implement is as follows:

 if (class_exists('B')) {
  $foo = new B();
 }
 else {
  $foo = new A();
 }

 That is all well and good if both A and B are already loaded and parsed, but I
 am using spl_autoload to lazy-load classes as needed.  That means the
 class_exists() call will return false if B exists but hasn't been included
 yet.  What I would like to happen is for PHP to include B if it exists or
 give a non-fatal error if it doesn't so that I can instantiate A instead.

 Ideally, the logic would be something like the following:

 try {
  $foo = new B();  // Try to autoload B, throw exception if it can't.
 }
 catch (ClassDoesntExistEvenAfterRunningThroughAutoloadException $e) {
  $foo = new A(); // May autoload A at this point, too.
 }
 // do stuff with $foo

 However, as far as I am aware $foo = new B(); will cause a fatal exception if
 autoload doesn't find a B.

 Does anyone know of a way to achieve the above effect?  This is specifically
 for PHP 5.2 and later.  Thanks.

 --
 Larry Garfield
 [EMAIL PROTECTED]

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



Perhaps this might do it:
spl_autoload_call('someclass');
if (!class_exists('someclass', false)) { load class A }
---End Message---
---BeginMessage---

Bastien Koert wrote:

On Sat, Jul 5, 2008 at 11:04 AM, Bastien Koert [EMAIL PROTECTED] wrote:



On Sat, Jul 5, 2008 at 6:51 AM, Peter Jackson [EMAIL PROTECTED]
wrote:




$conn=odbc_connect(Database,,);  works
$a = abcd; (this value exists in db)
$stat = Select * FROM  . 'Table Name';
$qry = odbc_exec($conn,$stat);
$res = odbc_result_all($qry) or die(Error: );

The above works as I expect it to.(Returns 70 rows)

If I now want to add a where clause
$stat = SELECT * FROM  . 'Table Name' .  Where  . 'Column Name =
  . $a;  (This works)

Now the place I fall into the abyss.
if I change WHERE clause in $stat to
  WHERE  . 'Column Name' LIKE abc*  (or other variations like abc%
abc% abc* 'abc%' 'abc*')
All I end up with is a blank page or Warning odbc_result_all No tuples
available at this result index.

Also I'm having trouble working out how to use a date in the WHERE clause.
 I've tried #yy-mm-dd# yy-mm-dd* dd/mm/yy etc etc (oh and yy/mm/dd 00:00:00
etc
 I realize this is probably more odbc/sql related but after a lot of
goggling and reading I havent found the answer (about 5 days so far)





As the data seems to be text based, you need to quote it

WHERE  . 'Column Name' LIKE 'abc%'

--

Bastien

Cat, the other other white meat




sorry, missed the access dates...

try mm/dd/ as the format


Unfortunately thats the first thing I thought of.  I've tried every 
variation of quote I could think of.  Can anyone tell me how to log what 
 the odbc connection is actually sending/receiving?  (as opposed to 
just echoing the sql statement I 'think' its sending.





---End Message---
---BeginMessage---
The selection 

Re: [PHP] Asynchronous PHP Execution

2008-07-06 Thread Waynn Lue
On Sat, Jul 5, 2008 at 12:28 PM, Daniel Brown [EMAIL PROTECTED] wrote:

 On Sat, Jul 5, 2008 at 6:01 AM, Waynn Lue [EMAIL PROTECTED] wrote:
  I have a system where a user clicks on a button which causes rows to
  be inserted in to the database.  I'd also like to run some lengthier
  post-processing on those rows, but don't want to put it in the
  critical path of the rows being inserted and returning to the user.
  What's the best way to either batch up these other actions, or pass
  them to a thread or other asynchronous process to do the second part
  of the action?

 Can you just run this via a cron or Scheduled Task?  Just have a
 boolean column that distinguishes new rows as unprocessed, and flip
 the flag when the cron script processes the row.

The problem with that is it requires another column to a table, which across
all our databases will take a really long time.  The other way is to create
another table, which means we're inserting across multiple tables.

Waynn


[PHP] Re: Creating XML files

2008-07-06 Thread Fabrice VIGNALS

Not so hard to find : http://php.net/manual/en/book.xml.php



It flance [EMAIL PROTECTED] a écrit dans le message de 
news:[EMAIL PROTECTED]

Hi all,

Some months ago i worked with XML. And i remember that i was able to 
create xml files quite easily. Now i don`t have the book i used by the 
time. I made many searches in google and i don't find something 
interesting. So i'm wondering if somebody can give a good link with 
examples. If i remeber well, i can create axml file without writing the 
hole file to a string before writing the string to the file.


Thanks a lot







--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: No Database Connection possible (mySQL)

2008-07-06 Thread Fabrice VIGNALS

First problem : there are not return in the function as it said
Second problem : php make a real connection with a unique id to a server 
database


$server1 = array(localhost,user,pass)
$server2 = array(localhost2,user2,pass2)

Function mysqlConnectServer($server)
{
$uid = mysql_connect($server) or die(mysql_error());
return $uid;
}

$uidServer1 = mysqlConnectServer( $server1);
$uidServer2 = mysqlConnectServer( $server2);

$db1_1 = mysql_select_db(tava, $uidServer1);
$db1_2 = mysql_select_db(tava2, $uidServer1);
$db2_1 = mysql_select_db(tava, $uidServer2);
$db2_2 = mysql_select_db(tava2, $uidServer2);

In this exemple we used, only 2 mysql connections to create 4 databases 
connections, on 2 mysql servers.


Depending of your application, you must manage server and database 
connection, to manage error and problem connection, but also for performance 
and don't create a server connection each time you connect to a different 
database on the same mysql server.

Also look at PDO:mysql and mysqli on php.net
Mysqli is simple to use, for exemple mysqli_connect(), use a mysql_connect 
or a mysql_pconnect depending of your configuration. Also mysqli check all 
server connection, if already exist it dont create a new connection to the 
server, but use an allready connection even your code isn't really 
optimised.
PDO is more complex and, in my mind, have some problem of performance at 
this time.




Aviation Coding [EMAIL PROTECTED] a écrit dans le message 
de news:[EMAIL PROTECTED]

Hi all,

I am having problems with a connection to a mysql database.

I am using


function con()
{
mysql_connect(localhost,user,pass) or die(mysql_error());
mysql_select_db(tava) or die(mysql_error());
}


Now, when I call the _function_ (!)

con() or die(no con);

I get the no con output.

When I call the mysql_connect and mysql_select directly before executing a
query, I get some DB output. But that won't work when I am using the
function...

Any ideas would be greatly appreciated.

Cheers!

Chris




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Asynchronous PHP Execution

2008-07-06 Thread Richard Heyes

Waynn Lue wrote:

and exec/shell (but that
doesn't seem to be asynchronous), but neither seems optimal.


It can be if you redirect the output streams and put an ampersand after it:

?php
   exec('sleep 5  /dev/null 2/dev/null ');
   echo 'Script ended';
?

This tiny sample should end immediately, and the sleep command should run
on regardless.

Thanks so much for the suggestion, that's what I ended up doing and it

worked, after some fiddling.  Just as a side note, does it execute from the
current directory of the file?


I think so.


Previously, I tried calling exec('php
scripts/foo.php'), but it seemed like there was some weird interaction
between different required files.  E.g., this was the layout:

orig.php
scripts/foo.php
incl.php


orig.php had the exec line, and foo.php had require_once(../incl.php).  But
it seemed like the exec call caused foo.php to execute from the scripts
directory while the require_once caused incl.php to also execute from the
scripts directory.

How does php determine what the working directory is?



I believe it's the directory of the original script. So if you had:

require_once('scripts/foo.php');

...in orig.php, that would be correct.

--
Richard Heyes

Employ me:
http://www.phpguru.org/cv

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: class_is_loadable?

2008-07-06 Thread Fabrice VIGNALS

Hi,

The problem is not the autoload but the implementation of such function.
class_is_loadable mean, hey php look at my class somewhere in my files. 
PHP should inspect some files, in some directories and list classes.

Which files, which extensions files, in which directories ? ...
In my mind you must replan your autoload, for exemple make a link beetween 
classes and files name, ie : if file_exists( A.class.php ) include_once( 
B.class.php) else include_once( A.class.php );
Check the factory method at Zend site, that explain how to work with class 
method, without to know the exact name of class (ex : load an specific class 
depending of the database available)




Larry Garfield [EMAIL PROTECTED] a écrit dans le message de 
news:[EMAIL PROTECTED]

Greetings, all.

I am trying to figure out a way to implement the following logic, but I am 
not

sure if it is possible to do so without a lot of additional side work:

I have a class, A, and another class B that extends A.  They live in 
separate

files.  The logic I need to implement is as follows:

if (class_exists('B')) {
 $foo = new B();
}
else {
 $foo = new A();
}

That is all well and good if both A and B are already loaded and parsed, 
but I

am using spl_autoload to lazy-load classes as needed.  That means the
class_exists() call will return false if B exists but hasn't been included
yet.  What I would like to happen is for PHP to include B if it exists or
give a non-fatal error if it doesn't so that I can instantiate A instead.

Ideally, the logic would be something like the following:

try {
 $foo = new B();  // Try to autoload B, throw exception if it can't.
}
catch (ClassDoesntExistEvenAfterRunningThroughAutoloadException $e) {
 $foo = new A(); // May autoload A at this point, too.
}
// do stuff with $foo

However, as far as I am aware $foo = new B(); will cause a fatal exception 
if

autoload doesn't find a B.

Does anyone know of a way to achieve the above effect?  This is 
specifically

for PHP 5.2 and later.  Thanks.

--
Larry Garfield
[EMAIL PROTECTED] 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Asynchronous PHP Execution

2008-07-06 Thread Waynn Lue

  and exec/shell (but that

 doesn't seem to be asynchronous), but neither seems optimal.


 It can be if you redirect the output streams and put an ampersand after it:

 ?php
exec('sleep 5  /dev/null 2/dev/null ');
echo 'Script ended';
 ?

 This tiny sample should end immediately, and the sleep command should run
 on regardless.

 Thanks so much for the suggestion, that's what I ended up doing and it
worked, after some fiddling.  Just as a side note, does it execute from the
current directory of the file?  Previously, I tried calling exec('php
scripts/foo.php'), but it seemed like there was some weird interaction
between different required files.  E.g., this was the layout:

orig.php
scripts/foo.php
incl.php


orig.php had the exec line, and foo.php had require_once(../incl.php).  But
it seemed like the exec call caused foo.php to execute from the scripts
directory while the require_once caused incl.php to also execute from the
scripts directory.

How does php determine what the working directory is?


Re: [PHP] URL Rewrite

2008-07-06 Thread Fabrice VIGNALS

Look at http://framework.zend.com/manual/en/zend.controller.router.html
Apache configuration et framework methods to rout your files are there.


Subhranil [EMAIL PROTECTED] a écrit dans le message de 
news:[EMAIL PROTECTED]


Hi All,

I want to show one URL at browser and content of different URL.

Like user can see the URL at address bar like http://localhost/test/home/
or http://localhost/test/tech/php/but content of page will be
http://localhost/test/index.php
The URL of the address bar will never change to
http://localhost/test/index.php

Still a  newbie!

Thanks,
Subhranild.

--
View this message in context: 
http://www.nabble.com/URL-Rewrite-tp18233803p18233803.html

Sent from the PHP - General mailing list archive at Nabble.com.




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Session variables disappear (some of them only)

2008-07-06 Thread Fabrice VIGNALS

Difficult to help you because there are many method of session :
- where do you store the sessions_variables : in local file, db or cookie ?
- how you transmit the session id, beetween pages(runtimes)  : cookie, $GET 
link, database ?



Did you check the availability of user cookie if you use it ?
Because if in each page of your application you define a session variable 
it's sure it will be every time here.
But the problem of session it's to transmit its ID between different pages, 
or session will be reset.
If a user don't authorised cookie you must transmit the session id by db 
storage or $Get link.


Also I don't see, a php modification during the last upgrades to explain 
that's kind of session problem.





karma [EMAIL PROTECTED] a écrit dans le message de 
news:[EMAIL PROTECTED]


Hi !

I have a very weird issue since the last Apache upgrade (- 2.2.8-r3, a 
month ago), but I'm not sure it is related (well, I'm pretty sure it's 
not).


Like many people, I've written an application that use PHP session 
variables, like $_SESSION[my_variable].


Sometimes (it doesn't happen all the time), _some_ of these variables are 
not written in the session file and they are lost after a simple 
header(Location:...); (same domain). The session file is in the right 
directory (permissions are fine), but some of my variables are missing.


The facts :
- Apache 2.2.9 + PHP 5.2.6_rc4 running on a Gentoo (up-to-date)
- all my scripts begin with session_start(). I've tried to add 
session_write_close() before every header(Location:...) call, it doesn't 
help.
- I didn't change anything in my program (it has been running just fine 
for 2 years), it just began to fail from time to time (I would say 10 
times a day). There is no hidden unset() function : it would fail for 
everyone.
- these variables are all set correctly, and they don't have reserved 
names.
- only a few variables disappear, but they are always the same ones (could 
it depend on their position in the session file ?!?)

- the session files are very small (max 100ko)
- it seems that it doesn't depend on the browser, but IE6 and IE7 seem to 
be the most affected ones (it may be because my users mostly use these 
browsers).
- I can't reproduce this issue from my local network (any OS/browser - it 
would be too easy :)
- reverting to the previous stable Apache and/or PHP versions doesn't 
help.

- I didn't change any php.ini directive.

Any idea ?

Thanks !


PS: if you need more details, just ask. The only thing I can't do is 
pasting the code : the scripts are quite huge. 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Session variables disappear (some of them only)

2008-07-06 Thread tedd

At 1:48 PM +0200 7/6/08, Fabrice VIGNALS wrote:

Difficult to help you because there are many method of session :
- where do you store the sessions_variables : in local file, db or cookie ?
- how you transmit the session id, beetween 
pages(runtimes)  : cookie, $GET link, database ?



Did you check the availability of user cookie if you use it ?
Because if in each page of your application you 
define a session variable it's sure it will be 
every time here.
But the problem of session it's to transmit its 
ID between different pages, or session will be 
reset.
If a user don't authorised cookie you must 
transmit the session id by db storage or $Get 
link.


Also I don't see, a php modification during the 
last upgrades to explain that's kind of session 
problem.





karma [EMAIL PROTECTED] a écrit dans 
le message de 
news:[EMAIL PROTECTED]


Hi !

I have a very weird issue since the last Apache 
upgrade (- 2.2.8-r3, a month ago), but I'm not 
sure it is related (well, I'm pretty sure it's 
not).


Like many people, I've written an application 
that use PHP session variables, like 
$_SESSION[my_variable].


Sometimes (it doesn't happen all the time), 
_some_ of these variables are not written in 
the session file and they are lost after a 
simple header(Location:...); (same domain). 
The session file is in the right directory 
(permissions are fine), but some of my 
variables are missing.


The facts :
- Apache 2.2.9 + PHP 5.2.6_rc4 running on a Gentoo (up-to-date)
- all my scripts begin with session_start(). 
I've tried to add session_write_close() before 
every header(Location:...) call, it doesn't 
help.
- I didn't change anything in my program (it 
has been running just fine for 2 years), it 
just began to fail from time to time (I would 
say 10 times a day). There is no hidden unset() 
function : it would fail for everyone.

- these variables are all set correctly, and they don't have reserved names.
- only a few variables disappear, but they are 
always the same ones (could it depend on their 
position in the session file ?!?)

- the session files are very small (max 100ko)
- it seems that it doesn't depend on the 
browser, but IE6 and IE7 seem to be the most 
affected ones (it may be because my users 
mostly use these browsers).
- I can't reproduce this issue from my local 
network (any OS/browser - it would be too easy 
:)

- reverting to the previous stable Apache and/or PHP versions doesn't help.
- I didn't change any php.ini directive.

Any idea ?

Thanks !


If it's any comfort, I had a similar problem 
sending session variables from a script in a 
httpdocs directory to a script in a httpsdocs. 
Some of the variables made it and some didn't. It 
was very confusing. The client had php 4.3.1 
installed, if that's any help.


I never did find out what the problem was and I 
finally passed everything via a POST.


Cheers,

tedd

--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] odbc msaccess php5 [Giving Up]

2008-07-06 Thread Peter Jackson

Peter Jackson wrote:

 well thats it Ive come to the conclusion that its a driver/lib issue. 
 From what I can see mdbtools lib only reads and only does basic select.
(eg Select * from table where col =thistext But not tex* % or date. 
Also looks like the project has died (think the last release was 2004 or 
so.)  Thought I would give odbtp ago but cant get that to make. aargghh 
I give up.



Bastien Koert wrote:

On Sat, Jul 5, 2008 at 11:04 AM, Bastien Koert [EMAIL PROTECTED] wrote:



On Sat, Jul 5, 2008 at 6:51 AM, Peter Jackson [EMAIL PROTECTED]
wrote:




$conn=odbc_connect(Database,,);  works
$a = abcd; (this value exists in db)
$stat = Select * FROM  . 'Table Name';
$qry = odbc_exec($conn,$stat);
$res = odbc_result_all($qry) or die(Error: );

The above works as I expect it to.(Returns 70 rows)

If I now want to add a where clause
$stat = SELECT * FROM  . 'Table Name' .  Where  . 'Column 
Name =

  . $a;  (This works)

Now the place I fall into the abyss.
if I change WHERE clause in $stat to
  WHERE  . 'Column Name' LIKE abc*  (or other variations like abc%
abc% abc* 'abc%' 'abc*')
All I end up with is a blank page or Warning odbc_result_all No tuples
available at this result index.

Also I'm having trouble working out how to use a date in the WHERE 
clause.
 I've tried #yy-mm-dd# yy-mm-dd* dd/mm/yy etc etc (oh and yy/mm/dd 
00:00:00

etc
 I realize this is probably more odbc/sql related but after a lot of
goggling and reading I havent found the answer (about 5 days so far)





As the data seems to be text based, you need to quote it

WHERE  . 'Column Name' LIKE 'abc%'

--

Bastien

Cat, the other other white meat




sorry, missed the access dates...

try mm/dd/ as the format


Unfortunately thats the first thing I thought of.  I've tried every 
variation of quote I could think of.  Can anyone tell me how to log what 
 the odbc connection is actually sending/receiving?  (as opposed to just 
echoing the sql statement I 'think' its sending.







--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Multiple words str_shuffle

2008-07-06 Thread Ron Piggott

I am trying to scramble individual words and/or phrases.

When it is a phrase I would like to keep the letters of each word
together, with a space between each one.  The code I have so far is
below.  I use PHP 4.4.7.  The code below is fine for a single word; it
is phrases that I am now trying to accommodate.


An example:

rise and shine

Desired output:

I S R E  N A D   E H I S N

Thanks for your help, 

Ron



$keyword might be 

$keyword = str_shuffle(strtoupper($keyword));

$buffer = ;

for ($count = 0; ($count  strlen($keyword)); $count++) $buffer .=
$keyword{$count}. ;

$keyword = trim($buffer);

unset($buffer);



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Multiple words str_shuffle

2008-07-06 Thread David Giragosian
On 7/6/08, Ron Piggott [EMAIL PROTECTED] wrote:


 I am trying to scramble individual words and/or phrases.

 When it is a phrase I would like to keep the letters of each word
 together, with a space between each one.  The code I have so far is
 below.  I use PHP 4.4.7.  The code below is fine for a single word; it
 is phrases that I am now trying to accommodate.


 An example:

 rise and shine

 Desired output:

 I S R E  N A D   E H I S N

 Thanks for your help,

 Ron



 $keyword might be

 $keyword = str_shuffle(strtoupper($keyword));

 $buffer = ;

 for ($count = 0; ($count  strlen($keyword)); $count++) $buffer .=
 $keyword{$count}. ;

 $keyword = trim($buffer);

 unset($buffer);


Once the individual words have had their letters shuffled, explode the
sentence on a space, then use the shuffle function (
http://us3.php.net/manual/en/function.shuffle.php) to, um, shuffle the
array.

David


Re: [PHP] Multiple words str_shuffle

2008-07-06 Thread Brady Mitchell

On Jul 6, 2008, at 305PM, Ron Piggott wrote:



I am trying to scramble individual words and/or phrases.

When it is a phrase I would like to keep the letters of each word
together, with a space between each one.  The code I have so far is
below.  I use PHP 4.4.7.  The code below is fine for a single word; it
is phrases that I am now trying to accommodate.


$orig_phrase = 'rise and shine';

// Split the phrase into an array with each word as an element
$array_phrase = explode(' ',$orig_phrase);

// Cycle through the array processing one word at a tie
foreach($array_phrase as $key = $value)
{
	// $orig_value is used in the do while loop to ensure that the  
shuffled string is not the original string.

$orig_value = $value;

	// Shuffle the string, and continue to do so until the returned  
string is not the original string

do{
$value = str_shuffle($value);   
} while($value == $orig_value);

// Uppercase value
$value = strtoupper($value);

// Insert a space after every letter
$value = chunk_split($value,1,' ');

// Set array value to newly formatted version
$array_phrase[$key] = $value;
}

// I'm using nbsp; so it will echo and be obvious that there are two  
spaces between words.

$scramble_phrase = implode('nbsp;nbsp;',$array_phrase);

echo $orig_phrase;
echo 'br /';
echo $scramble_phrase;

Everything after the do...while loop can be easily combined into one  
line; I left it as separate lines for clarity.


Brady

RE: [PHP] Re: No Database Connection possible (mySQL)

2008-07-06 Thread Chris Haensel

-Original Message-
From: M. Sokolewicz [mailto:[EMAIL PROTECTED] 
Sent: Friday, July 04, 2008 10:18 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]; php-general@lists.php.net
Subject: [PHP] Re: No Database Connection possible (mySQL)

David Robley wrote:
 Aviation Coding wrote:
 
 Hi all,

 I am having problems with a connection to a mysql database.

 I am using

 
 function con()
 {
 mysql_connect(localhost,user,pass) or die(mysql_error());
 mysql_select_db(tava) or die(mysql_error());
 }
 

 Now, when I call the _function_ (!)
 
 con() or die(no con);
 
 I get the no con output.

 When I call the mysql_connect and mysql_select directly before executing
a
 query, I get some DB output. But that won't work when I am using the
 function...

 Any ideas would be greatly appreciated.

 Cheers!

 Chris
 
 I think you need to return something from the function, like true if the
 connection/select worked, false if not.
 
 
 
 Cheers
You are correct.

function foo() {
  // does something
}

var_dump(foo()); // returns NULL

why? because you don't explicitly return anything. If you did, that'd be 
the return value. So if you did:
function bar() {
// does something
return true;
}

var_dump(bar()); // return true

Now, your script assumes a return-value:
baz() or somethingElse();
is an expression. This basically says:
if(!baz()) {
somethingElse();
}

Now, return (implicitly) null will result in (trough lazy comparison) a 
false value (*null == false*, null !== false), which then triggers your 
die() condition. Returning a meaningful value will get rid of that.

Other than that, choose 1 of 2 techniques:
1. error out inside the function itself (like you do now)
OR
2. error out based on the return-value of the function (like you do now 
ASWELL)
don't combine 1 and 2, stick with either, but not both.

function con()
{
mysql_connect(localhost,user,pass) or die(mysql_error());
mysql_select_db(tava) or die(mysql_error());
}
con();

works. The following would work too:
function con()
{
$r = mysql_connect(localhost,user,pass);
if(!$r) {
return false;
}
$r2 = mysql_select_db(tava);
if(!$r2) {
return false;
}
return true;
}

con() or die('no conn');

would also work correctly. So, stick with either, don't mix em.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

That worked like a charm. Thanks a lot. That one really had me stuck for
quite some hours. Thank God for this list *g*

Wish you all a great start into the week!

Chris


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php