tobiksowles Posted October 3, 2013 Share Posted October 3, 2013 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. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 3, 2013 Share Posted October 3, 2013 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. Quote Link to comment Share on other sites More sharing options...
tobiksowles Posted October 3, 2013 Author Share Posted October 3, 2013 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 3, 2013 Share Posted October 3, 2013 As well as passing the $con parameter to the function you will also need to pass the $qd value that you want to insert Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.