SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert with mysqli will not work

    I am trying to insert a new row into my table, and everything I try simply will not work. I have verified that my sql string is correct by running it in both phpmyadmin and mysql monitor. No matter what I try, it just prints the sql string like I told it to, and the exception I set up for when the insert query fails.
    Here's my code:
    Code:
    function insertNewUser($sn, $email, $pword)
    {
    	$link = new mysqli($db_host, $db_username, $db_password, $db_name);
    	if(!$link)
    		throw new exception("Error init mysqli_init.");
    	if($link->connect_error)
    		throw new exception("Error connecting to database in the insertNewUser function.", mysqli_connect_error());
    	$queryString = "INSERT INTO users (user_sn,user_email,user_password) VALUES ('$sn','$email','$pword')";
    	print $queryString . "<br>";//print string to make sure sql is right
    	
    	if($link->real_query($queryString)===FALSE)
    		throw new exception("Error inserting new user into table users.",$php_errormsg);
    	else
    		print "Successful insert.";
    		
    	$link->close();
    }
    What am I doing wrong?

    Thanks!
    --David Reagan
    DavidReagan.net

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does $link->error say? (Put it in your throw statement to debug)

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    What does $link->error say? (Put it in your throw statement to debug)
    Thanks, I wasn't sure how to get the errors out.
    Code:
    if($link->real_query($queryString)===FALSE)
    		throw new exception("Error inserting new user into table users. $link->error");
    Now it is telling me "Caught exception: Error inserting new user into table users. No database selected"

    But shouldn't the new mysqli have told me that it failed selecting the database?

    I also just tried:
    Code:
    $link = new mysqli($db_host, $db_username, $db_password, $db_name);
    	$link->select_db($db_name);
    And it still didn't select the database...
    --David Reagan
    DavidReagan.net

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does $db_name have the correct database name?
    What do you get from this:
    PHP Code:
    if(!$link->select_db($db_name)) {
      exit(
    $link->error);


  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, tried what you said. It still said "No database selected." So I tried hard coding the name in, instead of a variable. And now it says. "Access denied for user ''@'localhost' to database 'ficstorm'"

    So I tried printing the database variables from the config file I included, and nothing prints... But if nothing is there, why didn't creating the $link variable just fail? Am I missing something in how you include variables from another file? I just used include_once.
    --David Reagan
    DavidReagan.net

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    USA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for putting me on the right track! I just figured out how to use the define function in my config file. Now it works. I knew it was something simple.
    --David Reagan
    DavidReagan.net


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •