Alex-Grim Posted December 14, 2007 Share Posted December 14, 2007 As you can see from the code, i've declared all the variables in the class already, becasue they'll not change, but for some reason, when i try to connect and execute a query, i get : Access denied for user 'apache'@'localhost' (using password: NO) But i've sent the password and the user name in my script? <?php echo "<h1>MySql.php is now included</h1>"; class MySql{ var $d="AlexGrim"; var $h="127.0.0.1"; var $u="webuser"; var $p="666"; var $connection; var $query; var $num; var $array; var $rows; // function MySql($h,$u,$p,$d){ // $this->d = $d; // $this->h = $h; // $this->u = $u; // $this->p = $p; // } function Connect(){ $connection = mysql_connect("$h","$u","$p"); mysql_select_db("$d", $connection); } function Close(){ mysql_close($this->connection); } function Query($sql){ $query = mysql_query($sql) or die(mysql_error()); return $query; } function FetchRow($query){ $rows = mysql_fetch_row($query); return $rows; } function FetchArray($query){ $array = mysql_fetch_array($query); return $array; } function FetchNum($query){ $num = mysql_num_rows($query); return $num; } } ?> And here's the calling page, ignore the testing crap: <?php $br = "<br/><br/>"; $e = "<h1>ErRoR</h1>"; $myfile = "includes/classes/MySql.php"; if (file_exists($myfile)){ echo "The file DOES exist. $br"; include_once($myfile); echo "Page Loaded, and MySql.php included. $br"; }else{ echo ($e."<h1>ErRoR! Content not available!</h1>"); } $db = new MySql(); $con = $db->Connect(); $q = $db->Query("Select * from Comments"); ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2007 Share Posted December 14, 2007 any variable in the class should be referenced with $this->[variable name] so the statement below $connection = mysql_connect("$h","$u","$p"); should be $connection = mysql_connect($this->h,$this->u,$this->p); Quote Link to comment Share on other sites More sharing options...
Alex-Grim Posted December 14, 2007 Author Share Posted December 14, 2007 Thanx, that fixed it. I got another error afterwards about not being able to connect on 127.0.0.1(13), but i changed it from 127.0.0.1 to "localhost" and it's fine now, but i wonder what the difference really is. Thanx again. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2007 Share Posted December 14, 2007 its with the mysql permission table there are entries for the host table in the mysql database so in that table localhost must be specified Quote Link to comment 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.