RE: [PHP] Re: catch the error
| From: | Boyd, Todd M. | Date: | Thu, 26 Feb 2009 21:58:22 +0000 |
| Subject: | RE: [PHP] Re: catch the error | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-288879@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: PJ [mailto:af.gourmet@videotron.ca]
> Sent: Thursday, February 26, 2009 1:16 PM
> To: ash@ashleysheridan.co.uk
> Cc: Darryle Steplight; Ricardo Dias Marques;
php-general@lists.php.net;
> MySql
> Subject: Re: [PHP] Re: catch the error
>
> Ashley Sheridan wrote:
> > On Thu, 2009-02-26 at 13:56 -0500, PJ wrote:
> >
> >> Ashley Sheridan wrote:
> >>
> >>> On Thu, 2009-02-26 at 13:34 -0500, Darryle Steplight wrote:
> >>>
> >>>
> >>>> Hi PJ,
> >>>> $db_host = 'biggie';
> >>>> $db_user = 'root';
> >>>> $db_pass = 'gugus@#$';
> >>>> $db_name = 'biblane';
> >>>>
> >>>>
> >>>>
> >>>> Everyone here is trying to help you and that's cool, but EVERYONE
> on
> >>>> this list may not be so nice. The above credentials is definitely
> the
> >>>> type of information you want to keep private, unless you don't
> mind
> >>>> people potentially accessing your database tables and doing
> whatever
> >>>> they like with them.
> >>>>
> >>>> I suggest doing something like
> >>>> $db_host = 'localhost;
> >>>> $db_user = 'foo';
> >>>> $db_pass= ''bar;
> >>>> $db_name =''xxxxxx;
> >>>>
> >>>> if you are going to post it on the list.
> >>>>
> >>>> On Thu, Feb 26, 2009 at 1:22 PM, PJ <af.gourmet@videotron.ca>
> wrote:
> >>>>
> >>>>
> >>>>> Ricardo Dias Marques wrote:
> >>>>>
> >>>>>
> >>>>>> Hi PJ,
> >>>>>>
> >>>>>> On Thu, Feb 26, 2009 at 17:28, PJ <af.gourmet@videotron.ca>
> wrote:
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>> What is wrond with this file? same identical insert works from
> console
> >>>>>>> but not from this file :-(
> >>>>>>>
> >>>>>>> [snip]
> >>>>>>>
> >>>>>>> <?
> >>>>>>> //include ("lib/db1.php"); // Connect to database
> >>>>>>> mysql_connect('biggie', 'user',
> >>>>>>> 'password', 'test');
> >>>>>>> $sql1 = "INSERT INTO example (name, age) VALUES ('Joe
> >>>>>>> Blow',
> '69')";
> >>>>>>> $result1 = mysql_query($sql1,$db);
> >>>>>>> if (!$result1) {
> >>>>>>> echo("<P>Error performing 1st query: " .
> >>>>>>> mysql_error() . "</P>");
> >>>>>>> exit();
> >>>>>>> }
> >>>>>>> ?>
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>> I haven't coded in PHP for a long time, but I think that your
> problem
> >>>>>> is in this line:
> >>>>>>
> >>>>>> $result1 = mysql_query($sql1,$db);
> >>>>>>
> >>>>>> Up to that point, $db (that should point to a database link
> >>>>>> identifier) is not defined. You probably want to assign the
> >>>>>> "mysql_connect" result to that $db variable.
> >>>>>>
> >>>>>>
> >>>>>> So, I think that you will solve your problem by changing your
> >>>>>> "mysql_connect" line FROM the current form:
> >>>>>>
> >>>>>> mysql_connect('biggie', 'user',
> >>>>>> 'password', 'test');
> >>>>>>
> >>>>>> .. TO this one:
> >>>>>>
> >>>>>> $db = mysql_connect('biggie', 'user',
> >>>>>> 'password', 'test');
> >>>>>>
> >>>>>>
> >>>>>> Am I right?
> >>>>>>
> >>>>>>
> >>>>> Partly. I had an error in the location of the include. Ashley
> corrected
> >>>>> the rest but it only works with the include. Not as whown below
> >>>>> <?
> >>>>> //include ("../lib/db1.php"); // Connect to database
> >>>>>
> >>>>> $db_host = 'biggie';
> >>>>> $db_user = 'root';
> >>>>> $db_pass = 'gugus@#$';
> >>>>> $db_name = 'biblane';
> >>>>>
> >>>>> $db_connect = mysql_connect($db_host, $db_user, $db_pass);
> >>>>> $db_select = mysql_select_db($db_name, $db_connect);
> >>>>>
> >>>>> $sql1 = "INSERT INTO test (name, age) VALUES ('Arnie
> >>>>> Shwartz',
> '75')";
> >>>>> $result1 = mysql_query($sql1,$db);
> >>>>> if (!$result1) {
> >>>>> echo("<P>Error performing 1st query: " .
> >>>>> mysql_error() . "</P>");
> >>>>> exit();
> >>>>> }
> >>>>> ?>
> >>>>>
> >>>>> --
> >>>>>
> >>>>> Phil Jourdan --- pj@ptahhotep.com
> >>>>> http://www.ptahhotep.com
> >>>>> http://www.chiccantine.com
> >>>>>
> >>>>>
> >>>>> --
> >>>>> MySQL General Mailing List
> >>>>> For list archives:
> >>>>> http://lists.mysql.com/mysql
> >>>>> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=dsteplight@gmail.com
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>> I agree. I wouldn't trust me at all! ;)
> >>>
> >>>
> >>> Ash
> >>> www.ashleysheridan.co.uk
> >>>
> >>>
> >>>
> >>>
> >> Yeah.... very stupid of me...but I found the error: see if you can
> catch it:
> >> <?
> >> //include ("../lib/db1.php"); // Connect to database
> >>
> >> $db_host = 'xxx';
> >> $db_user = 'xxx;
> >> $db_pass = 'xxx';
> >> $db_name = 'xxx';
> >>
> >>
> >> $db = mysql_connect($db_host, $db_user, $db_pass);
> >> mysql_select_db($db_name,$db);
> >>
> >> $sql1 = "INSERT INTO test (name, age) VALUES ('Arnie Shwartz',
> '75')";
> >> $result1 = mysql_query($sql1,$db);
> >> if (!$result1) {
> >> echo("<P>Error performing 3st query: " .
> >> mysql_error() . "</P>");
> >>
> >> }
> >> echo $sql1;
> >> echo "<br />";
> >> echo $db_select;
> >> exit();
> >> ?>
> >>
> >> --
> >>
> >> Phil Jourdan --- pj@ptahhotep.com
> >> http://www.ptahhotep.com
> >> http://www.chiccantine.com
> >>
> >>
> >>
> > $db_user has not had the string terminated. pray tell was that the
> > answer you were looking for?!
> >
> >
> > Ash
> > www.ashleysheridan.co.uk
> >
> >
> >
> No. Damn those typos!
>
> What seems to have made it work is just
>
> $db = mysql_connect($db_host, $db_user, $db_pass);
> mysql_select_db($db_name,$db);
>
> not using mysql_select in a string....
> but would you use it in a string? how & why?
Jesus Christ... everyone on this list must've had a long week, because
you guys are going blind. :D
In examples sent to you, people foolishly replaced your $db var with
$db_connect ONLY FOR PART OF THE SCRIPT. You've defined your database
connection as $db_connect in some versions of the source, but then you
reference $db (without _connect) in your mysql_select call in that same
source.
$db = mysql_connect([option list here]); # <-- this code instantiates a
connection
mysql_select_db([some name], $db); # notice how $db is here?
$result = mysql_query([some query], $db); # it's here, too!
$db becomes your resource link when you use mysql_connect. That resource
link must then be passed to your mysql functions. Otherwise, they have
no idea which database connection you are attempting to use.
# BAD:
$db = mysql_connect([options]);
$result = mysql_query([some query], $db_connect); # what's $db_connect?
who knows!
# GOOD:
$db = mysql_connect([options]);
$result = mysql_query([some query], $db); # same resource link from
mysql_connect
See?
HTH,
// Todd