Forum Moderators: coopster
I created two php files one for database connection string dbconfig.php and another one for all functions related to database processing libdb.php. In another php file I want to call a readDB() to read the database and returning a set of value. THis is what i put:
<?php
require "lib/libdb.php";
$values = readDB(values);
echo "here is the value:".$values;
?>
when I do this one, the page wont load (document not found) on php engine ver 4.3.10..... but works on ver 5.0.......My hosting site still using ver 4.........But when I commented that function calling, the page loads.... of course with error that $values doesnt exist......Am i missing anything? this thing has bugged me for the last 3 days.....I read some article that suggested that require should be put on top of every php php... I did that but it doesnt help.....
My previous attempts works ok... but this one doesnt... Is it because on dbconfig.php i put two different db connection strings as:
$db1 and $db2 as arrays?
Thanks
KapalSelam
it should be $values = readDB($value)
anyway these are my codes:
db_config.php
<?php
//db configurations
$db_times = array(
db_host => "host",
db_name => "dbname",
db_user => "user",
db_passwd => "passwd;
?>
lib_db.php
<?php
require "db_config.php";
//specific function to get latesttenders
function latest($db_times)
{
global $db_times;
$query = "SELECT * FROM LIST ORDER BY project_id DESC LIMIT 5";
$results = readDB($query, $db_ctimes);
$DBrows = mysql_num_rows($results);
for($i=0; $i<$DBrows; $i++)
{
$row = mysql_fetch_array($results);
$str = <p><b>".$row['project']."</b></p><p>".$row['street'].",".$row['suburb'].",".$row['postcode']."</p><p>".$row['details']." Value: $ ".$row['value']."</p>";
//these lines are needed to clean mysql data from \n and \r
//that cause error in javascript
$str = str_replace("\r"," ",$str);
$str = str_replace("\n"," ",$str);
$str = str_replace("\"",""",$str);
$str = str_replace("&","&",$str);
$array = $array."tickercontents[".$i."] =".$str."\"\n";
}
return $array;
}
//generic function to get mysql error details
function showError()
{
die("Error ".mysql_errno()." : ".mysql_error());
}
//generic read DB function
function readDB($query, $db_config)
{
$db_host = $db_config["db_host"];
$db_name = $db_config["db_name"];
$db_user = $db_config["db_user"];
$db_passwd = $db_config["db_passwd"];
//open connection
if(!($connection = @ mysql_connect($db_host, $db_user, $db_passwd)))
{
die("Fatal error! Could not connect to database.");
}
//select a database
if(!(@ mysql_select_db($db_name,$connection)))
showError();
//query database
if(!($results = @ mysql_query($query, $connection)))
showError();
//close connection
mysql_close($connection);
return $results;
}
?>
I try to call them in this list.php
<?php
//to get latest function
require "lib/lib_db.php";
?>
<html>
........HTML code in here.....
.............................
.............................
............................
...........................
then i want that function to load js vars:
<Script......................>
<?php
echo latest();
?>
</script>
.............
.............
..........
</html>
Thanks
KapalSelam