Jump to content

Help with error about printing name from database in title tag


Recommended Posts

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.

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.

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...

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.

@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!

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.