unlishema.wolf Posted June 5, 2013 Share Posted June 5, 2013 Well it is the time of the year I refresh myself on php again. I am wanting to make a function to connect to the mysql database, run the query, and return the result. However, I also wish to add in a way to get the mysql error if something messes up (for debugging of course). Here is what I have so far: <?php $mysql_test_server="localhost"; $mysql_test_username="username"; $mysql_test_password="password"; $mysql_test_database="database"; function mysql_test_query($mysql_test_query, $mysql_test_close=true) { mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password); mysql_select_db($mysql_test_database); $mysql_test_result = mysql_query($mysql_test_query); if ($mysql_test_close) { mysql_close(); } return $mysql_test_result; } $result = mysql_test_query("SELECT * FROM people") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "First Name: ".$row['first_name']."</ br>"; echo "Last Name: ".$row['last_name']."</ br>"; } ?> Quote Link to comment Share on other sites More sharing options...
unlishema.wolf Posted June 5, 2013 Author Share Posted June 5, 2013 I forgot to mention I have attempted the following without any working results. <?php $mysql_test_server="localhost"; $mysql_test_username="username"; $mysql_test_password="password"; $mysql_test_database="database"; function usql_query($mysql_test_query, $mysql_test_close=true) { mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password) or return false; mysql_select_db($mysql_test_database) or return false; $mysql_test_result = mysql_query($mysql_test_query) or return false; if ($mysql_test_close) { mysql_close() or return false; } return $mysql_test_result; } $result = usql_query("SELECT * FROM people") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "First Name: ".$row['first_name']."</ br>"; echo "Last Name: ".$row['last_name']."</ br>"; } ?> However, after reading up on the php functions page I am going to test the following once I get my localhost back up and running. <?php $mysql_test_server="localhost"; $mysql_test_username="username"; $mysql_test_password="password"; $mysql_test_database="database"; function usql_query($mysql_test_query, $mysql_test_close=true) { $mysql_test_conected = mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password); if(!$mysql_test_conected) { return false; } if(!mysql_select_db($mysql_test_database)) { return false; } $mysql_test_result = mysql_query($mysql_test_query); if(!$mysql_test_result) { return false; } if ($mysql_test_close) { if(!mysql_close()) { return false; } } return $mysql_test_result; } $result = usql_query("SELECT * FROM people") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "First Name: ".$row['first_name']."</ br>"; echo "Last Name: ".$row['last_name']."</ br>"; } ?> Sorry for double post. I waited to long and can't edit the first post. Quote Link to comment Share on other sites More sharing options...
Solution unlishema.wolf Posted June 5, 2013 Author Solution Share Posted June 5, 2013 (edited) The following code allows you to call one function and run a mysql query without having to leave a mysql connection open or opening it every time. It will open the connection, select the database, run the query, and then close the connection if you wish so. Notice: If a mysql_error() occurs it will not close the connection. <?php $mysql_test_server="localhost"; $mysql_test_username="username"; $mysql_test_password="password"; $mysql_test_database="database"; function mysql_test_query($mysql_test_query, $mysql_test_close=true) { global $mysql_test_server,$mysql_test_username,$mysql_test_password,$mysql_test_database; $mysql_test_conected = mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password); if(!$mysql_test_conected) { return false; } if(!mysql_select_db($mysql_test_database)) { return false; } $mysql_test_result = mysql_query($mysql_test_query); if(!$mysql_test_result) { return false; } if ($mysql_test_close) { if(!mysql_close()) { return false; } } return $mysql_test_result; } $result = mysql_test_query("SELECT * FROM people") or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "First Name: ".$row['first_name']."</ br>"; echo "Last Name: ".$row['last_name']."</ br>"; } ?> Sorry for the third post just wanted to post the solution for my problem for anybody in the future. Edited June 5, 2013 by unlishema.wolf 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.