madsovenielsen Posted September 15, 2010 Share Posted September 15, 2010 Hello.I am trying to get this code to work. <?php $host = "127.0.0.1"; $user = "root"; $password = "*******"; $database = "personal"; $connection = mysql_connect($host,$user,$password); mysql_select_db($database, $con); $sql = "SELECT * FROM files;"; $resultset = mysql_query($sql); while($row = mysql_fetch_array($resultset)) { echo $row['filename'] . "<br />"; } echo mysql_error();mysql_close($con); ?> mysql_error returns "No database selected"Although i have selected a database ??/mads Link to comment Share on other sites More sharing options...
thescientist Posted September 15, 2010 Share Posted September 15, 2010 try "localhost" for your $host Link to comment Share on other sites More sharing options...
jeffman Posted September 15, 2010 Share Posted September 15, 2010 mysql_error returns the most recent error. You should be checking the return value of all your mysql functions, starting from mysql_connect. You can't select a database if you don't have a connection, and you don't know yet if you have a connection. Link to comment Share on other sites More sharing options...
madsovenielsen Posted September 15, 2010 Author Share Posted September 15, 2010 try "localhost" for your $host120.0.0.1 is localhost ?Why does this help ?/mads Link to comment Share on other sites More sharing options...
madsovenielsen Posted September 15, 2010 Author Share Posted September 15, 2010 mysql_error returns the most recent error. You should be checking the return value of all your mysql functions, starting from mysql_connect. You can't select a database if you don't have a connection, and you don't know yet if you have a connection.No difference. 127.0.0.1 and localhost gives the same error. "No database selected"/mads Link to comment Share on other sites More sharing options...
madsovenielsen Posted September 15, 2010 Author Share Posted September 15, 2010 mysql_error returns the most recent error. You should be checking the return value of all your mysql functions, starting from mysql_connect. You can't select a database if you don't have a connection, and you don't know yet if you have a connection.This code works perfectly on the same database and computer<?php $con = mysql_connect("127.0.0.1","root","*********"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("users", $con); $result = mysql_query("SELECT * FROM active_users"); while($row = mysql_fetch_array($result)) { echo $row['id'] . $row['fname'] . "<br />"; } mysql_close($con);?> Link to comment Share on other sites More sharing options...
jeffman Posted September 15, 2010 Share Posted September 15, 2010 Oops. Just noticed this: $connection = mysql_connect($host,$user,$password);mysql_select_db($database, $con); You've used different variable names: $connection above, $con below. Link to comment Share on other sites More sharing options...
madsovenielsen Posted September 15, 2010 Author Share Posted September 15, 2010 Oops. Just noticed this:$connection = mysql_connect($host,$user,$password);mysql_select_db($database, $con); You've used different variable names: $connection above, $con below. That did the trick. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.