thugsr Posted August 21, 2012 Share Posted August 21, 2012 Hi everyone, i have problem with printing something from database in title tag. This is code <title><?php $id_brenda = $_GET['id']; $query="SELECT * FROM brend WHERE id_brenda='$id_brenda'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo $row['naziv_brenda']; } ?></title> And i get this warning Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in brands.php on line 9 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in brands.php on line 9 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) line 9 is $result = mysql_query($query) or die(mysql_error()); Does anyone know on what is meant with this warning? My connection on database is in separate file, and when i insert it in file with this code i get other error: Fatal error: Cannot redeclare connecttodb() (previously declared in docroot/dblib.php:9) in docroot/dblib.php on line 22 Please help me, this error driving me crazy. Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/ Share on other sites More sharing options...
MMDE Posted August 21, 2012 Share Posted August 21, 2012 You must connect to the MySQL server first and then also choose the database. Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371037 Share on other sites More sharing options...
ManiacDan Posted August 21, 2012 Share Posted August 21, 2012 Your first error is "there is no database." Your second error is "your database connection file is wrong." Read the error. Look at it: Fatal error: Cannot redeclare connecttodb() (previously declared in docroot/dblib.php:9) in docroot/dblib.php on line 22 What happened? "Fatal error" Why? "Cannot redeclare connecttodb()" Why not? "(Previously declared in docroot/dblib.php:9)" Where did this error happen? "in docroot/dblib.php on line 22" the error message is as exact as it can possibly be without spelling it out in 3 paragraphs like I'm doing. Your connecttodb function occurs twice. You can't have two copies of the same function. You must fix your dblib.php so it only has one function, and make sure that function is correct. Then you must include your dblib in any script which wishes to use the database, and ensure the database connection is actually established before you try to use it. Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371101 Share on other sites More sharing options...
thugsr Posted August 22, 2012 Author Share Posted August 22, 2012 I know that, but look at this... include('dblib.php'); connectToDB(); this is where he (php) thinks that is redeclared conectToDB... global $connection; function connectToDB() { global $connection; // this is line 9 $dbhost = 'localhost'; $dbuser = 'fitnes_5653'; $dbpass = 'test'; $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); //$dbname = 'fitnes'; $dbname = 'fitnes_5653'; mysql_select_db($dbname); //return $connection; } // this is line 22 there shouldn't be error in here... Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371351 Share on other sites More sharing options...
ManiacDan Posted August 22, 2012 Share Posted August 22, 2012 There's nothing else in this file, and you're sure this is the file being included? Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371409 Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2012 Share Posted August 23, 2012 You are either including the file twice or you are including it inside of a loop. Also, don't use the global keyword (ever.) Using it outside of a function doesn't do anything, except to waste processing time. Using it inside of a function for your connection variable makes your code is hard-coded to only be able to use that one connection. You should pass variables into a function as a call-time parameter and return any result from your function to the calling code, to make your function general purpose. Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371704 Share on other sites More sharing options...
thugsr Posted August 23, 2012 Author Share Posted August 23, 2012 @ManiacDan that is whole file, nothing else is in there...and that file is included... @PFMaBiSmAd i am not including it twice... Can you tell me how to print product name from database into title tag? is there other way? I don't know what to do...my way obviously doesn't work... Edit: I solved it...just puted if(!function_exists('connectToDB')) before function and it works...thank you all for helping me! Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371727 Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2012 Share Posted August 23, 2012 Both the error message and the work-around that you used to stop the error indicate that you are including the file twice. Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371776 Share on other sites More sharing options...
ManiacDan Posted August 23, 2012 Share Posted August 23, 2012 Yeah, you're definitely including this file twice. If you're unsure of which files are being included or why, you should look through your code until you figure it out. And 'global' is definitely wrong. Quote Link to comment https://forums.phpfreaks.com/topic/267364-help-with-error-about-printing-name-from-database-in-title-tag/#findComment-1371785 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.