can't connect to database error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smithedison
Forum Newbie
Posts: 9
Joined: Fri Jul 10, 2015 4:51 am

can't connect to database error

Post by smithedison »

This is strange I return the first name and surname from a table. If I echo the 2 fields out I get the correct result. If however I try to use it in a html line I get an error Can't connect to database even though I have already done it and got the result set back.

I use a trigger error that sends me to an error page with this error, but it works fine if I don't try to use the result set in my page.

If I comment out the trigger error and use a standard connection it ALL works FINE.

Code: Select all

?  
    $qUserData = GetUser($ID); 
    $rs = mysql_fetch_array($qUserData); 
    //echo $rs['UserF'] . " " . $rs['UserS']; 
    //exit; 
?> 
    <h2>Edit User Shelves for: <?=$rs['UserF']?></h2><br> 

This works if I remove the commented out echo and exit. 

============= 


  if(!$link = @mysql_connect($host, $user, $pass)) 
    trigger_error('Can\'t connect to server: ('. $db . ')', E_USER_ERROR); 

  if(!$database = @mysql_select_db($data, $link)) 
    trigger_error('Can\'t select database on: (' . $db . ')', E_USER_ERROR); 
     
   
    //$link = @mysql_connect($host, $user, $pass); 
    //$database = @mysql_select_db($data, $link); 


If I remove the trigger errors and replace it with the normal connection it all works perfectly. The trigger error should be happening when I connect to the database not when I try to use 

<h2>Edit User Shelves for: <?=$rs['UserF']?></h2><br> 

This is strange...
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: can't connect to database error

Post by Pazuzu156 »

I don't see any connection in the first example, I don't know how that's laid out, but it looks like there's no connection.

In the second example, the reason the trigger_error may/may not get called how you want is because in your calls to mysql_connect, you're using the @ suppressor.

This suppresses errors, so that could likely be the cause, remove them.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply