Jump to content

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


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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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