Jump to content

Trouble with mysql connection in PHP


tobiksowles

Recommended Posts

I'm attempting to use PHP to connect to an SQL database to use to store quotes from an IRC chat bot.  So far the bot seems to be running, but the sql connection isn't working and I'm not exactly sure why.  I've tried reading through all sorts of sites, and I modified code I found on the WC3 schools site just to be sure I wasn't missing some huge 'derp'.  Nothing seems to work, and I'm feeling my brain melt.

 

Relevant connection data for writing to the db:

	function addquote()
	{
		$con=mysqli_connect("localhost","BotBot","password","Quotes");
		// Check connection
			if (mysqli_connect_errno())
 			{
 			 echo "Failed to connect to MySQL: " . mysqli_connect_error();
  			}

		mysqli_query($con,"INSERT INTO `Quotes`.`quotes` (`ID_NUM`, `DATA`, `USER`) VALUES (NULL, \'$qd\', \'\');");
			$this->pm("Quote added to database.");

		mysqli_close($con);	
	}

Second connection for reading a random result from the db and messaging it:

function randomquote()
	{
		$con=mysqli_connect("localhost","BotBot","password","Quotes");
		// Check connection
			if (mysqli_connect_errno())
  			{
  				echo "Failed to connect to MySQL: " . mysqli_connect_error();
  			}

			$result = mysqli_query($con,"SELECT * FROM quotes ORDER BY RAND() LIMIT 1");
				$this->pm($result);
				
		mysqli_close($con);

        }

"password" in both is being replaced with the relevant password for that user account, and the account has full access to r/w the db.

 

I am getting the pm("Quote added to database") line returned to me through the IRC bot, and it doesn't disconnect the moment you try to use these functions (took about a week of pokes and prods to figure out what I was doing wrong!).  I'm not exactly sure how a response down past that sql connection could be working if it's not connecting, not without giving me a 'die'.

 

PHPMyAdmin isn't showing that 'BotBot' account even attempting a connection and dying for a bad password or anything, so... kinda running out of ideas here.

 

If anyone can give me a hand here, I'd be grateful for it.

Link to comment
Share on other sites

you should NOT create a database connection, run one query, close the database connection. you are figuratively killing your database server.

 

you need to create ONE database connection in your main code and use that throughout the code on any page. you would pass the $con variable into your functions as a call-time parameter.

 

at least your insert query is failing due to a sql syntax error (the escaped single-quotes that are part of the sql statement syntax, not part of the data in the statement) and you have no error checking logic to get your code to tell you if and why the query failed.

Link to comment
Share on other sites

Thank you for the help, Mac_G.  I moved the connection information to a seperate file and now have it require_once and don't close it, this will be a pretty commonly asked for database.  After a few tries, I at least managed to get it to attempt a connection, so this has gotten me MUCH further than I'd gotten on my own.

 

I'm still unable to actually connect to the db, but that seems to be something wrong with my sql setup, like the user that supposedly has privileges doesn't actually have them.  I'll keep at it though.

Link to comment
Share on other sites

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.