Matrix001
Matrix001

Reputation: 1272

No database found!! mysql error

     //require_once("../StoredProcedure/connect.php");
  $conn=mysql_connect("localhost","root","") or die(mysql_error);
  mysql_select_db("politicalforum",$conn); 

 function updateThread($threadID, $content) 
 {
     mysql_query("UPDATE threads
                  SET content='$content'  
                  WHERE thread_id=$threadID") ;
    // $res = mysql_query($sql) or trigger_error(mysql_error().$sql);
     mysql_close();
 }

I get this each time..what am I doing wrong?

No database selected.

I am calling that function from an outside file...like this..

if(isset($_GET['threadID']) && isset($_POST["reply"]) && isset($_POST['textareas']))
   {

        updateThread($_GET['threadID'], $_POST['textareas']); 

       $postValid=TRUE;
   }

Upvotes: 0

Views: 1183

Answers (1)

Emw
Emw

Reputation: 1665

Your connection is likely going out of scope, causing the mysql_query to run against the context's database object which doesn't exist by the time updateThread fires.

In this case you need to pass the connection into the mysql_query function inside updateThread. Architecturally there are two ways you can go about this:

1) Open and close the connection inside your updateThread function:

function updateThread($threadID, $content) 
{
   $conn=mysql_connect("localhost","root","") or die(mysql_error);
   mysql_select_db("politicalforum",$conn); 
   mysql_query("UPDATE threads
                SET content='$content'  
                WHERE thread_id=$threadID", $conn) ;
   mysql_close($conn);
   $conn = null;
}

2) Pass the connection as a variable to updateThread, in case you want to use the same connection in other functions. PHP will automatically close and dispose your MySQL connection when the script finishes:

function updateThread($threadID, $content, $conn) 
{
   mysql_query("UPDATE threads
                SET content='$content'  
                WHERE thread_id=$threadID", $conn) ;
}

Upvotes: 3

Related Questions