Jump to content


Photo

Problem in database connection (php+mysql+apache on windows)


  • Please log in to reply
9 replies to this topic

#1 jbashir

jbashir
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 August 2006 - 01:24 PM

I am having problem in making database connection with mysql database. I have searched the web and my code looks exactly the same as many have written. But still there is no success. Can someone look into it and guide me where I am wrong. Here is the code:

$dbhost = 'localhost:3306';
$dbuser = 'testuser';
$dbpass = 'testpass';
$conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error());
$dbname = 'testdb';
mysql_select_db($dbname);

I am using Apache 2.2.
Joseph Bashir
Ufnasoft
http://ufnasoft.com

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 01:28 PM

$dbhost = 'localhost:3306';
needs to just be

$dbhost = 'localhost';


regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 jbashir

jbashir
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 August 2006 - 01:37 PM

Liam

In the start I was doing it the you are telling:

$dbhost = 'localhost';

and I had this problem. And then someone suggested it this way (as now):

$dbhost = 'localhost:3306';

and still there is the same problem.

Joseph
Joseph Bashir
Ufnasoft
http://ufnasoft.com

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 01:40 PM

are you sure mysql is running? dont you even get an error?
that is the way to specify a port but should be needed to be honest..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 jbashir

jbashir
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 August 2006 - 01:53 PM

Yes mysql is running. And there are no errors even.
Joseph Bashir
Ufnasoft
http://ufnasoft.com

#6 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 02:03 PM

so it would seem to be connecting then.. what makes you think/know that it isnt? could just be somthign wrong with code you typed try this

$dbhost = 'localhost:3306'; 
$dbuser = 'testuser'; 
$dbpass = 'testpass'; 
$conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error()); 
$dbname = 'testdb'; 
mysql_select_db($dbname); 

$query=mysql_query('show tables') or die(mysql_error());


if you get no error then it is connected and if there is a problem then it should tell us it.

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 August 2006 - 02:19 PM

Remove the @ symbol from infront of mysql_connect to retrieve the PHP error, rather than the MySQL error being returned by mysql_error.

Also I have closed your other thread in the PHP SQL forum.

#8 jbashir

jbashir
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 August 2006 - 02:42 PM

Liam
I have used your code but still the same results.

I have also remove @ symbol as 'wildteen88' has just told, but still it is not showing any results, not even errors. All am getting is in the access.log file (Apache2.2/logs/access.log) is:

192.168.0.21 - - [26/Aug/2006:19:30:26 +0500] "GET /test.php HTTP/1.1" 200 -

Joseph Bashir
Ufnasoft
http://ufnasoft.com

#9 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 26 August 2006 - 02:54 PM

what about running this code

<?php
include('../opendb.php');
$result=mysql_query("SHOW TABLES") or die(mysql_error());
while ($query_data = mysql_fetch_row($result)) {
	echo $query_data[0]."<br>";
}
?>

hoiw come your looking at the apache access log? that wouldnt show a connection to Mysql server as the connection is from php
see if the code abobe lists ok.. if it does it should list all tables store in your database called testdb
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 August 2006 - 02:55 PM

If you are getting no errors then its working. Is that your full code. If its then that code wont display anythink. All it does is connect and select the database thats it. It wont return anythink. The only time it'll return something is when you start to query the database. Like so:
<?php
$dbhost = 'localhost:3306'; // this is fine, as '':3306 specifies the port number MySQL is running on
$dbuser = 'testuser';
$dbpass = 'testpass';
$conn = @mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql' . mysql_error());
$dbname = 'testdb';
mysql_select_db($dbname); 

// now query the database
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql, $conn) or die('Unable to perform query - ' . $sql . ' - ' . mysql_error());

// start the table:
echo '<table>';

// now display the results:
while($row = mysql_fetch_assoc($result))
{
    echo '<tr><td>';
    echo implode('</td><td>', $result);
    echo '</td></tr>';
}

// close the table:
echo '</table>';

Now change table_name from the SQL query ($sql) to the table that is in the database (testdb).

It'll now run the query and display everthing that is in that table.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users