Jump to content

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


jbashir

Recommended Posts

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

[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);

$query=mysql_query('show tables') or die(mysql_error());[/code]


if you get no error then it is connected and if there is a problem then it should tell us it.
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 -
what about running this code

[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>";
}
?>[/code]

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
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:
[code=php:0]<?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>';[/code]

Now change [i]table_name[/i] 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.

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.