pmi Posted April 7, 2013 Share Posted April 7, 2013 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 https://forums.phpfreaks.com/topic/276634-database-connection-issues/ Share on other sites More sharing options...
pmi Posted April 7, 2013 Author Share Posted April 7, 2013 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. Link to comment https://forums.phpfreaks.com/topic/276634-database-connection-issues/#findComment-1423365 Share on other sites More sharing options...
codefossa Posted April 7, 2013 Share Posted April 7, 2013 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 https://forums.phpfreaks.com/topic/276634-database-connection-issues/#findComment-1423377 Share on other sites More sharing options...
pmi Posted April 8, 2013 Author Share Posted April 8, 2013 Thanks much for the help. I got it working! Appreciate it! Link to comment https://forums.phpfreaks.com/topic/276634-database-connection-issues/#findComment-1423497 Share on other sites More sharing options...
sjvmv Posted April 25, 2013 Share Posted April 25, 2013 Hi dude, That's really good. I hope, always we need that mysql optimization. whenever u r writing query, it should be optimized. thats really help DB hits. for more details with examples. Please have a look http://rightern.com/index.php?/projects/view_project/MySQL%2520optimization/30 I hope this will help you lot Thanks, VIJAY Link to comment https://forums.phpfreaks.com/topic/276634-database-connection-issues/#findComment-1426555 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.