Jump to content

Database Connection Issues


pmi
Go to solution Solved by codefossa,

Recommended Posts

Hello All,

 

I am relatively new to PHP and this site so I apologize in advance if I am asking this in the wrong place. I am trying to very simply connect a PHP script to my localhost database. I am using WAMP server 2. The PHP version is 5.3.13, Apache version 2.2.22, and MySQL version 5.5.24

 

I believe with my current code I am now able to connect, but I am unable to output the query result.

 

I have tried a lot of things, and I know this is a simple task, but I'm stuck. The error I seem to be getting often is:

 

"mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\DB_connect_5_04052013\index.php on line 29"

 

Any suggestions would be much appreciated.

 

 

<?php

// Create connection

$con=mysql_connect("localhost","root");



// Check connection

if ($con == FALSE)

  {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

  }

 

// Check connection again

$link = mysql_connect("localhost","root", "", "autosite_db");

if (!$link)

    {

    die("Can't connect to database server");

    }



// Select the Database

mysql_select_db ("autosite_db", $con)



          or die ("Unable to select database");

          

// Execute Sample Query

      

$SQL = "SELECT * FROM vehicle";

$result = mysql_query($SQL);





while ( $db_field = mysql_fetch_assoc($result) ) {







print $db_field['vin'] . "<BR>";

print $db_field['year'] . "<BR>";

print $db_field['make'] . "<BR>";

print $db_field['model'] . "<BR>";



}
 
Link to comment
Share on other sites

Sorry all,  just noticed I should have posted this in the "PHP coding section" of PHP Freaks. I don't want to double post and I don't think there is any way to move it now.

Edited by pmi
Link to comment
Share on other sites

  • Solution

Here's a little example.  Untested, so sorry if I mistyped or something.

 

 

<?php

// Not necessary, but easier to change.  Defining SQL connection info.
define("HOSTNAME", "localhost");
define("USERNAME", "root");
define("PASSWORD", "********");
define("DATABASE", "mysql_db");

// Connect or Die
$link = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die("Failed to connect to MySQL. ):");
mysql_select_db(DATABASE, $link); // Select Database

// Query the database.
$sql = "SELECT * FROM `table` WHERE `this` = 'that';";
$result = mysql_query($sql);

// We're done with it, so close the connection.
mysql_close($link);

// Loop through and do whatever with the returned data.
while ($row = mysql_fetch_assoc($result))
{
    echo $row['this'] . " is that." . "\n";
}

?>
Link to comment
Share on other sites

  • 3 weeks later...
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.