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
https://forums.phpfreaks.com/topic/36221-solved-functions-again/
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());

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.