whitecollarcoder Posted September 13, 2007 Share Posted September 13, 2007 I've been stuck with a project here at work to basically learn php as I go with this. So far I've come to the conclusion that I need to make sure and code using classes and functions. Here is my question. I'm starting simple with baby steps and I've created my database and tables already in mysql. without using any class/functions I made a simple page that connected to the db and reported everything in a table for me. <? include 'config.php'; echo "<title>base</title>" . "</head>" . "<body>"; echo "Testing the output. <br>" . "Get some data from the table<br>" . "<table>"; $sqlconn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Unable to Connect'); $db = mysql_select_db($dbname, $sqlconn) or die(mysql_error()); $sqlquery = "select first_name,last_name,email_address from pls_contacts"; $result = mysql_query($sqlquery, $sqlconn) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo "<tr>" . "<td>{$row['first_name']}</td><td>{$row['last_name']}</td><td>{$row['email_address']}</td>" . "</tr>"; } echo "</table>"; mysql_close($sqlconn); ?> What I tried to do was convert this into a more organized way of things, which would allow me to call a connection or query instead of having to retype it any time I needed it. CONFIG.PHP function sqlconnect($sqlconn) { $sqlconn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Unable to Connect'); return $sqlconn; } so I simply included it in my test_con file using include 'config.php'; and then added sqlconnect($sqlconn); in the test_con.php file to call the function for use. TEST_CON.PHP sqlconnect($sqlconn); $db = mysql_select_db($dbname, $sqlconn) or die(mysql_error()); $sqlquery = "select first_name,last_name,email_address from pls_contacts"; $result = mysql_query($sqlquery, $sqlconn) or die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo "<tr>" . "<td>{$row['first_name']}</td><td>{$row['last_name']}</td><td>{$row['email_address']}</td>" . "</tr>"; } echo "</table>"; mysql_close($sqlconn); This however just totally doesnt work. I'm sure I just brutally ripped apart anything close to right in class/function coding but I've yet to find anything that talks about using classes and functions with msyql. Suggestions and guidance toward some correction would be great. Quote Link to comment https://forums.phpfreaks.com/topic/69230-mysql-classfunction-organization-question/ Share on other sites More sharing options...
whitecollarcoder Posted September 14, 2007 Author Share Posted September 14, 2007 /bump Quote Link to comment https://forums.phpfreaks.com/topic/69230-mysql-classfunction-organization-question/#findComment-348377 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.