Jump to content

Need help with broken variable?


Jax2

Recommended Posts

Hi all.

 

On a page inside my includes directory, I have shoutbox.php.

 

Shoutbox.php includes db.php which is in the same directory, hence: include("db.php");

 

Directly after that include statement, I can echo a variable ($prefix) that is stored inside of db.php, which correctly returns ts_

 

Now, further down in the code, I am trying to access a table in my database known as ts_shoutbox, so I am using ".$prefix."shoutbox  which should work fine, yet I am getting the error:

 

Table 'tsrecipe.shoutbox' doesn't exist

 

which is correct, because it's supposed to be tsrecipe.ts_shoutbox, not .shoutbox

 

Here is the code:

 

<?php
include("db.php");
echo "Prefix: ".$prefix.";

function getContent($link, $num){
$res = @mysql_query("SELECT ShoutBoxID, date, user, message FROM ".$prefix."shoutbox WHERE ShoutBoxID='$recipeID' ORDER BY date DESC LIMIT ".$num, $link);
if(!$res)
	die("Error: ".mysql_error());
else
	return $res;
}

 

I don't understand why it echos ts_ perfectly at the top but refuses to use it in the function.

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/197729-need-help-with-broken-variable/
Share on other sites

Found my problem.... I needed to define $prefix as a global variable inside of the function.

 

It now works.

 

function getContent($link, $num){
global $prefix;
$res = @mysql_query("SELECT date, user, message FROM ".$prefix."shoutbox ORDER BY date DESC LIMIT ".$num, $link);
if(!$res)
	die("Error: ".mysql_error());
else
	return $res;
}

Archived

This topic is now archived and is closed to further replies.

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