• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Array messing around

Status
Not open for further replies.

Zisly

Intermediate OT User
Joined
Jun 9, 2008
Messages
7,338
Reaction score
120
Hello!

I've been sitting here for some hours trying to figure out whats going on but I just can't, I'm probably missing something simple but I just can't notice it >.<

My problem:
I'm trying to use the data of a global array to connect to a mysql database, which just doesn't work for some reason.
I made a test file to see if my way of doing it was wrong but nope, I was doing it right, at least judging from the example.
So, something has to be wrong with the main code, anyone who could give it a look?


Where I'm calling the function:

PHP:
<?PHP
include("footer.php");
include("config.php");
include("menus.php");

$topmenu = LoadTopMenu();

?>



config.php:
PHP:
// Database
$database_host = "localhost";
$database_user = "root";
$database_pass = "xxxx";
$database_db = "teamworker";

$CONFIG = array(
"db_host" => $database_host,
"db_user" => $database_user,
"db_pass" => $database_pass,
"db_db"   => $database_db);


And the function file:
PHP:
<?php
include("config.php");

	// Loads top menus
    function LoadTopMenu()
	{
		global $CONFIG;
        
		$top_menu = '';
        
		// Connect to the MYSQL database
		mysql_connect($CONFIG['db_host'], $CONFIG['db_user'], $CONFIG['db_pass']) or die(mysql_error());
		// Select the DB
		mysql_select_db($CONFIG['db_db']) or die(mysql_error());
				
		// Load top menu
		$query = mysql_query( 'SELECT * FROM `top_menu`;' )or die(mysql_error());
					
			while ( $row = mysql_fetch_array( $query ) )
		    {
				// load top menu, {destination, img, text*2}
				$tempVar = '<li><a href="'.$row['dest'].'" title="'.$row['text'].'">'.$row['text'].'</a></li>';
	                    
				// Append to variable
				$top_menu  = $top_menu.$tempVar;
		    }
		
		// return the variable with the menus
		return $top_menu;
	}
	
?>
 
Problem solved, it was so simple and stupid just as I suspected.
Sitting up at night doing this is not such a smart idea :p
 
just a tip: First use mysqli, Second supress error reporting or use 'die' in case it fails to connect otherway it will throw up your mysql password....
 
just a tip: First use mysqli, Second supress error reporting or use 'die' in case it fails to connect otherway it will throw up your mysql password....

I'm already using 'die'.
Check the code.
 
Like in my class:
Code:
	class Paste extends MySQLi
	{
		public function __construct()
		{
			parent::__construct("localhost", "root", "topsecret", "paste");

			if ($this->connect_error)
				die("Cannot connect database! [Error #".$this->connect_errno."]");
		}

oo ;d
 
PDO is better than mysqli!

not, PDO is slower than mysqli and lacks load of features which mysqli provide.... I can't really tell you my sources since it was some time ago i saw a benchmark about that and no longer got the link :(....

EDIT:
Just found a link to a debate about that, might be usefull http://www.sitepoint.com/forums/showthread.php?t=512273


@Down:
PDO isn't that hard, PLUS For the only thing i see PDO usefull is because is portable and cross-database but if you gonna use it for only mysql database whats the point of having PDO?
 
Last edited:
@averatec
But the PDO is more difficultly that MYSQL or MYSQLi That already they is OK for the beginners

@Migxxx
i like PDO, its very hard to use it and PDO is slower than mysqli? :eek:
 
@averatec
But the PDO is more difficultly that MYSQL or MYSQLi That already they is OK for the beginners

@Migxxx
i like PDO, its very hard to use it and PDO is slower than mysqli? :eek:

PDO slower than mysqli? fail...
 
Status
Not open for further replies.
Back
Top