Jump to content

Recommended Posts

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.

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.