I need to know how to get the maximum number of a column using PHP and SQL Server 7. I have a scenario as shown below. I have heard to use the max command in sql and didn't seem to work because I got the following error: "wrong parameter count mssql_result()"
<head><title>get Data Via PHP To SQL</title></head>
<body>
<?php
$hostname = "voyager"; //machine name where sql is
$user = "admins"; // password and user make per DB
$password = "test123";
$db = "gradient";
$link = mssql_connect($hostname, $user, $password);
if (!link) die("couldnt open file");
mssql_select_db($db) or die("couldn't open db");
print "databse \"$db\" is opened";
$result = mssql_query("select user_id from employee compute max(user_id)"); //select statement here
$mydata = mssql_result($result);
print $mydata;
?>
</body>
the query works in the ms query analyzer with the following results
1
2
3
3
4
max
========
4
It seems mssql_result() need only 1 item and there is many above is this correct?
if so is there another way?
BACKGROUND INFO
+++++++++++++++
I have 2 tables
Table 1 called employee
Table 2 called pc_hardware
In the Employee table the Primary Key is user_id which its value is
automatically generated and seeded.
In table 2 the Primary Key is pc_id which its value is automatically
generated and seeded.
The foreign key for table 2 is user_id but not seeded.
user_id properties are 'int' length of 4, precision of 10 and no nulls
allowed, for both tables.
scenerio: I filled the employee table first with a scripting language
called PHP with no problem. minutes later I went to fill the
pc_hardware table. I get the following message
Cannot inset null unto column 'user_id'...... column does not allow
nulls insert fails.
It seems to me 2 things are going on, it doesn't know to finish off
that record without referencing the original table employee and get
the user_id to place into it. And secondly I told the table no nulls
are accepted. (by the way if I accept nulls on table 2 the query works
but user_id is null, and there would be no way of me referencing other
tables correctly).
Is this a correct assumption that I need to get the user_id from
table 1 and place it in table 2 along with other values?
if so is there a way to find out the last user_id in a query, because
then I can simply querry that first and get the value and pass it
along onto my new query.
Sorry if this sounds dum but very new to SQL
Thanks in advance
Vin
<head><title>get Data Via PHP To SQL</title></head>
<body>
<?php
$hostname = "voyager"; //machine name where sql is
$user = "admins"; // password and user make per DB
$password = "test123";
$db = "gradient";
$link = mssql_connect($hostname, $user, $password);
if (!link) die("couldnt open file");
mssql_select_db($db) or die("couldn't open db");
print "databse \"$db\" is opened";
$result = mssql_query("select user_id from employee compute max(user_id)"); //select statement here
$mydata = mssql_result($result);
print $mydata;
?>
</body>
the query works in the ms query analyzer with the following results
1
2
3
3
4
max
========
4
It seems mssql_result() need only 1 item and there is many above is this correct?
if so is there another way?
BACKGROUND INFO
+++++++++++++++
I have 2 tables
Table 1 called employee
Table 2 called pc_hardware
In the Employee table the Primary Key is user_id which its value is
automatically generated and seeded.
In table 2 the Primary Key is pc_id which its value is automatically
generated and seeded.
The foreign key for table 2 is user_id but not seeded.
user_id properties are 'int' length of 4, precision of 10 and no nulls
allowed, for both tables.
scenerio: I filled the employee table first with a scripting language
called PHP with no problem. minutes later I went to fill the
pc_hardware table. I get the following message
Cannot inset null unto column 'user_id'...... column does not allow
nulls insert fails.
It seems to me 2 things are going on, it doesn't know to finish off
that record without referencing the original table employee and get
the user_id to place into it. And secondly I told the table no nulls
are accepted. (by the way if I accept nulls on table 2 the query works
but user_id is null, and there would be no way of me referencing other
tables correctly).
Is this a correct assumption that I need to get the user_id from
table 1 and place it in table 2 along with other values?
if so is there a way to find out the last user_id in a query, because
then I can simply querry that first and get the value and pass it
along onto my new query.
Sorry if this sounds dum but very new to SQL
Thanks in advance
Vin