Store more than 1 piece of information in a single variable

Store more than 1 piece of information in a single variable

am 03.08.2007 17:44:18 von Arena Servers

------=_NextPart_000_001B_01C7D5ED.88DE6470
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

ok, firstly here's my script...


$db_host =3D '*****';

$db_user =3D '*****';

$db_pass =3D '*****';

$db_name =3D '*****';

$db_name2 =3D '*****';

$time =3D time();

$conn =3D mysql_connect($db_host, $db_user, $db_pass);

mysql_select_db($db_name);

$query =3D "SELECT * FROM `clanmembers`";

$result =3D mysql_query($query) or die('Query failed. ' . =
mysql_error());

while ($row =3D mysql_fetch_array($result)){

$user_loginname =3D $row['user_loginname'];

}

mysql_close_db;

mysql_select_db($db_name2);

$query =3D "SELECT user_id FROM `e107_user` WHERE `user_loginname` =3D =
'$user_loginname'";

$result =3D mysql_query($query) or die('Query 2 failed. ' . =
mysql_error());

while ($row =3D mysql_fetch_array($result)){

$user_id =3D ($row['user_id']);

$query =3D 'INSERT INTO e107_private_msg VALUES (NULL, \'1\', =
\''.$user_id.'\', \''.$time.'\', \'0\', \'Clan War 2\', \'Clan War =
Arranged\', \'0\', \'0\', \'\', \'+rr+\', \'0\');';

$result =3D mysql_query($query) or die('Query 3 failed. ' . =
mysql_error());

}

?>


Now, the script's task is to select loginnames from one database1, =
compare them to database2, select the user_id associated with the =
loginnames in database2 and send a private message to each of those =
loginnames.

Just now, the script selects only the last loginname from database1 and =
then sends a private message to that 1 person.

I need it to select all the loginnames which currently there are 25. So =
what do I need to change for this to work?

Thanks in advance.

Paul

Arena Servers - Web Hosting
http://www.arenasmithster.co.uk
------=_NextPart_000_001B_01C7D5ED.88DE6470--

Re: Store more than 1 piece of information in a single variable

am 03.08.2007 18:05:01 von Michael Preslar

First.. You're using mysql_fetch_array when you wanted
mysql_fetch_assoc.. .. select user_id from whatever .. and then $row =
mysql_fetch_array($res) .. youd have a $row[0].. mysql_fetch_assoc
would allow $row['user_id']

Secondly.. You can use one query to select all the info..

select e107.user_id FROM db2.e107_user as e107 left join
db1.clanmembers as clan on e107.user_loginname = clan.user_loginname

You'll still want to mysql_select_db($db2) for your insert query

Thirdly.. I'd change the insert query to:

$user_id = mysql_real_escape_string($user_id);
$query = "INSERT INTO e107_private_msg VALUES (NULL, '1', '$user_id',
NOW(), '0', 'Clan War 2', 'Clan War Arranged', '0', '0', '', '+rr+',
'0')";
mysql_query($query) or die('Query 3 failed. ' . mysql_error());

Where the entire query is surrounded by double quotes, so you dont
have to back slash singles.. $user_id is escaped, so its safe.. using
mysql's NOW() function instead of php's time()..

On 8/3/07, Arena Servers wrote:
> mysql_select_db($db_name2);
>
> $query = "SELECT user_id FROM `e107_user` WHERE `user_loginname` = '$user_loginname'";
>
> $result = mysql_query($query) or die('Query 2 failed. ' . mysql_error());
>
> while ($row = mysql_fetch_array($result)){
>
> $user_id = ($row['user_id']);
>
> $query = 'INSERT INTO e107_private_msg VALUES (NULL, \'1\', \''.$user_id.'\', \''.$time.'\', \'0\', \'Clan War 2\', \'Clan War Arranged\', \'0\', \'0\', \'\', \'+rr+\', \'0\');';
>
> $result = mysql_query($query) or die('Query 3 failed. ' . mysql_error());
>
> }

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Store more than 1 piece of information in a single variable

am 03.08.2007 18:06:42 von Uber Wannabe

(see below)

-----Original Message-----
From: Arena Servers [mailto:smithster@arenasmithster.co.uk]
Sent: Friday, August 03, 2007 10:44 AM
To: php-db@lists.php.net
Subject: [PHP-DB] Store more than 1 piece of information in a single
variable

ok, firstly here's my script...


$db_host = '*****';

$db_user = '*****';

$db_pass = '*****';

$db_name = '*****';

$db_name2 = '*****';

$time = time();

$conn = mysql_connect($db_host, $db_user, $db_pass);

//N/A - Add $ in front of mysql_select_db and (maybe not necessary) specify
connection
$mysql_select_db($db_name, $conn);

//N/A - No ' needed.
$query = "SELECT * FROM clanmembers";

//N/A - Specify connection in query (maybe not necessary)
$result = mysql_query($query, $conn) or die('Query failed. ' .
mysql_error());

//N/A - I think mysql_fetch_assoc brings the result in with keys;
mysql_fetch_array would let you reference by indices.
while ($row = mysql_fetch_assoc($result))
//N/A - Toss names in an array; otherwise, you're overwriting each username
every time you fetch.
$user_loginname[] = $row['user_loginname'];

//N/A - Maybe not necessary to close DB in between queries.
mysql_close_db;

mysql_select_db($db_name2);

//N/A - Again, no ' needed. Also, your query should recursively check each
username from above.
foreach($user_loginname As $msguser)
{
$query = "SELECT user_id FROM e107_user WHERE user_loginname =
'$msguser'";
$result = mysql_query($query, $conn) or die('Query 2 failed. ' .
mysql_error());

//N/A - We'll use mysql_fetch_assoc() instead, and an if statement.
if($row = mysql_fetch_assoc($result)
{
$user_id = ($row['user_id']);
//N/A - Put code to message user here
}
}
?>


Now, the script's task is to select loginnames from one database1, compare
them to database2, select the user_id associated with the loginnames in
database2 and send a private message to each of those loginnames.

Just now, the script selects only the last loginname from database1 and then
sends a private message to that 1 person.

I need it to select all the loginnames which currently there are 25. So what
do I need to change for this to work?

Thanks in advance.

Paul

Arena Servers - Web Hosting
http://www.arenasmithster.co.uk

-----End Original Message-----


Comments are in original message. I hope at least some of them help.

-- N/A

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Store more than 1 piece of information in a single variable

am 03.08.2007 20:13:58 von niel

> First.. You're using mysql_fetch_array when you wanted
> mysql_fetch_assoc.. .. select user_id from whatever .. and then $row =
> mysql_fetch_array($res) .. youd have a $row[0].. mysql_fetch_assoc
> would allow $row['user_id']

mysql_fetch_array($res) will fetch both associative and numerical
indexes, so it will have $row[0] and $row['user_id'] in it. It has an
optional second parameter to indicate associative, numerical or both,
defaulting to both. mysql_fetch_row only fetches numerically indexed
arrays

--
Niel Archer

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php