Jump to content

[SOLVED] Functions Again


Daney11

Recommended Posts

Hey Guys,

   Troubles with functions again... Basically i would like to know how to extract a username from a database using functions.

[b]Connect_Database.php[/b]
[code]
<?php
function ConnectDatabase()

{

global $db;
$db = mysql_connect("localhost","root","") or die("Unable To Connect To The MySQL Database");
mysql_select_db("database", $db) or die("Unable To Select The Database");

}
?>
[/code]

Above is my database connect script.

How would i go about extracting all usernames from the `members` table in the database?

Something like this?

[code]
<?php

function GetUsername($username) {

$strQuery = "SELECT member_id, member_username FROM members WHERE member_id > '0'";
$result = mysql_query($strQuery,$db) or die(mysql_error());

$myrow = mysql_fetch_array($result)

$username = $myrow['member_username'];

}

echo GetUsername("$username");
[/code]
.............

Where am i going wrong? And how would i go about doing it correctly?

Thanks again guys,

Dane
Link to comment
Share on other sites

Yes, i really did read the link.

I am getting an error.

The function is...

[code]
function GetUsername($username) {

$strQuery = "SELECT manager_id, manager_first_name FROM managers WHERE manager_id > '0'";
$result = mysql_query($strQuery,$db) or die(mysql_error());

$myrow = mysql_fetch_array($result);

$username = $myrow['manager_first_name'];
return $username;

}
[/code]

ive included it in my script and used...

<?php echo GetUsername("$username"); ?>

and im getting these errors

Notice: Undefined variable: username in c:\program files\www\premiership.php on line 43

Notice: Undefined variable: db in c:\program files\www\included_files\total_goals.php on line 35

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in c:\program files\www\included_files\total_goals.php on line 35

and line 35 is

$result = mysql_query($strQuery,$db) or die(mysql_error());
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.