Solved

PHP3 MSSQL query

Posted on 2000-02-15
32
574 Views
Last Modified: 2008-03-17
Can anyone please inform me what am I doing wrong here:

<?
   mssql_connect("wap", "wwUser", "wwUser");
   mssql_select_db("Runtime");
   mssql_query("select dbo.AnalogLive.prodlevel from dbo.AnalogLive");
?>

or how exactly should I use (or any examples how) mssql_select_db and mssql_query functions as well as mssql_result...

Thanks in Progress,
Wizz.
0
Comment
Question by:wizzard
  • 24
  • 8
32 Comments
 
LVL 1

Accepted Solution

by:
aikon earned 90 total points
ID: 2523802
Hi wizzard,

Try this one:
<?php
   // AnalogLive is your target table
   // prodlevel is the field of Analog
   // table you want to query about

   $conn = mssql_connect("wap", "wwUser", "wwUser") or die ("Can't connect to server!");
   mssql_select_db("Runtime",$conn) or die ("Can't select database!");
 
   $res = mssql_query("select prodlevel from AnalogLive",$conn);

   while (mssql_fetch_row($res)) {
     //
     // Put here your code
     //
   }
   mssql_close($conn);
?>

Regards
0
 

Author Comment

by:wizzard
ID: 2524523
Adjusted points to 60
0
 

Author Comment

by:wizzard
ID: 2524524
i have the last question before GRADING you - mssql_fetch_row function - what does it and how can i now get the data out of that Table?

Is it all right that my base is Runtime.dbo.AnalogLive and 'dbo' wasn't meant in your source?

B.R.,

Wizzard.
0
 

Author Comment

by:wizzard
ID: 2526001
Adjusted points to 70
0
 

Author Comment

by:wizzard
ID: 2526002
this doesn't work a bit, can you please explain:

$res = mssql_query("select prodlevel from AnalogLive",$conn);
1> this line of code returns this:

Warning: MS SQL message: Invalid column name 'prodlevel'. (severity 16) in d:\wizzard\apache\www\home\mssql02.php3 on line 9

Warning: MS SQL: Query failed in d:\wizzard\apache\www\home\mssql02.php3 on line 9

> Maybe I should add .dbo prefix to the AnalogLive table?:
$res = mssql_query("select prodlevel from dbo.AnalogLive",$conn);

> Actually, the full path to the table is Runtime.dbo.AnalogLive

2> How can I write down data from that row i fetch? echo($res[0]) will work? is $res the same array echo can take row data from?

BR,

Wizz.
0
 
LVL 1

Expert Comment

by:aikon
ID: 2526150
Hi wizzard.

Ok, I will try ;)

a) The first error: Invalid column name
I suppose that you have a table called 'AnalogLive', and this table has a field called 'prodlevel'.
So what PHP is saying is that is incorrect! The AnalogLive table hasn't a field called 'prodlevel'

b) Second error: 'Query failed in ...'
This error was dropped because the mssql_query statement was failed!


> Maybe I should add .dbo prefix to the AnalogLive table?:
I don't need to use dbo prefix in my scripts when I worked with MS-SQL servers.
Try this query: "selec * from AnalogLive" instead

2> How can I write down data from that row i fetch? echo($res[0]) will work? is $res the same array echo can take row data from?

No it doesn't work fine, first you might fetch the row and then look at the fields.
I fill the while loop ...

<?php
 // At this point I supose all was gone ok
 // The loop show all the tuples
 $i=0;
 while (mssql_fetch_row($res)) {
   echo "<b>".mssql_result($res,$i,0)."</b><br>\n";
   $i++;
 }
?>

Regards.
0
 

Author Comment

by:wizzard
ID: 2526291
Adjusted points to 75
0
 

Author Comment

by:wizzard
ID: 2526292
khmmm that doesn't return anything... Maybe something is wrong with dbo.AnalogLive or simply AnalogLive...

BR,
Wizz.
0
 

Author Comment

by:wizzard
ID: 2526312
echo "...<br>\n";
> Actually it writes only what's written in this line

 $i=0;
 while (mssql_fetch_row($res)) {
   echo "<b>".mssql_result($res,$i,0)."</b><br>\n";
   echo "...<br>\n";
> but doesn't write this line at all! :(
   $i++;
 }

Please have a look at this...

Wizz.
0
 
LVL 1

Expert Comment

by:aikon
ID: 2526345
Hi wizz,

Ok, I understand (correct me if I mistake) that you only can write the first tuple, can't you?

Reagards.
0
 

Author Comment

by:wizzard
ID: 2526362
Yes, and the second one doesn't appear on the screen, as if the interpreter didn't visit the contents of the cycle at all... :(

Wizz
0
 
LVL 1

Expert Comment

by:aikon
ID: 2526447
Ok right!
Some times ago I had a similar problem with the odbc driver ... :)

try without use the $i variable ...
<?php
 while (mssql_fetch_row($res)) {
   echo "<b>".mssql_result($res,0)."</b><br>\n";
 }
?>

Regards.
0
 

Author Comment

by:wizzard
ID: 2526484
Adjusted points to 80
0
 

Author Comment

by:wizzard
ID: 2526485
khmm... I've figured out some strange things:

1)
   $res = mssql_query("select * from AnalogLive",$conn);

 echo "...1...<br>\n";

// while (mssql_fetch_row($res)) {
//   echo "<b>".mssql_result($res,0)."</b><br>\n";
//   echo "...2...<br>\n";
// }
> changing this to the lowermentioned code:
   mssql_fetch_row($res);
   echo "...2...<br>\n";
   echo "<b>".mssql_result($res,0)."</b><br>\n";
   echo "...3...<br>\n";
>  results in these results:
....1...
....2...

Warning: Wrong parameter count for mssql_result() in d:\wizzard\apache\www\home\mssql02.php3 on line 19

....3...

> OK, but if I'm writin this code:
 while (mssql_fetch_row($res)) {
   echo "<b>".mssql_result($res,0)."</b><br>\n";
   echo "...2...<br>\n";
 }
> I get the following result:
....1...

Regards,
Wizz.

0
 

Author Comment

by:wizzard
ID: 2526524
> This code also doesn't return ...2... string... :( :
 while (mssql_fetch_row($res)) {
   echo "<b>".mssql_result($res,0)."</b><br>\n";
 }
 echo "...2...<br>\n";

BW,
Wizz.
0
 
LVL 1

Expert Comment

by:aikon
ID: 2526536
Hi Wizz,
Ok it seems be a index problem ...
try to use '1' instead '0' index:

echo "<b>".mssql_result($res,1)."</b><br>\n";

Reagards.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:wizzard
ID: 2526553
khmmm...
the ruslt of using this code:

<?php
   // AnalogLive is a target table
   // prodlevel is the field of AnalogLive
   //        table you want to query about

   $conn = mssql_connect("wap", "wwUser", "wwUser") or die ("Can't connect to server!");
   mssql_select_db("Runtime",$conn) or die ("Can't select database!");
 
   $res = mssql_query("select * from AnalogLive",$conn);

 echo "...1...<br>\n";

 while (mssql_fetch_row($res)) {
   echo "<b>".mssql_result($res,1)."</b><br>\n";
   echo "...2...<br>\n";
 }
   echo "...3...<br>\n";


   mssql_close($conn);
?>

> is a lil better:
....1...
....3...

> ;)) it doesn't return anything from the cycle anyway... :P

BW
0
 

Author Comment

by:wizzard
ID: 2526572
Adjusted points to 85
0
 
LVL 1

Expert Comment

by:aikon
ID: 2526618
Ok ... I'm getting little fuzzy :)

So lets simplify the code ... try this:

<?php
mssql_fetch_row($res);

echo "step -- 1<br>\n";
echo "<b>".mssql_result($res,1,1)."</b><br>";
echo "step -- 2<br>\n";
?>

What is returned by php?

Regards.

0
 

Author Comment

by:wizzard
ID: 2526690
..1...
step -- 1

Warning: MS SQL: Bad row offset (1) in d:\wizzard\apache\www\home\mssql03.php3 on line 15

step -- 2
....3...

> is the result for the following code:

<?php
   $conn = mssql_connect("wap", "wwUser", "wwUser") or die ("Can't connect to server!");
   mssql_select_db("Runtime",$conn) or die ("Can't select database!");
 
   $res = mssql_query("select * from AnalogLive",$conn);

 echo "...1...<br>\n";

mssql_fetch_row($res);
echo "step -- 1<br>\n";
echo "<b>".mssql_result($res,1,1)."</b><br>";
echo "step -- 2<br>\n";

   echo "...3...<br>\n";

   mssql_close($conn);
?>

BW,
Wizz
0
 

Author Comment

by:wizzard
ID: 2526701
Mistake in last message, I ment error in line 15!!!

bW
0
 

Author Comment

by:wizzard
ID: 2526703
Khmmm, in "LINE 11"
0
 
LVL 1

Expert Comment

by:aikon
ID: 2526713
Ok!
There are two possible ways:

a)The table is empty, so this is the reason why we can't see anything

b)The MSSQL Server just returned only one row, and it must be referenced by 0 (like C).
Change:
echo "<b>".mssql_result($res,1,1)."</b><br>";

by
echo "<b>".mssql_result($res,0,1)."</b><br>";

And let me know what was happened.

Regards
0
 

Author Comment

by:wizzard
ID: 2526722
You're right, as I have told (maybe I've forgotten) this table consists only of one (1) row.
I'll check this out in a sec and 'll answer immediately!

Thanks.
0
 

Author Comment

by:wizzard
ID: 2526726
Adjusted points to 90
0
 

Author Comment

by:wizzard
ID: 2526727
..1...
step -- 1

Warning: MS SQL: Bad row offset (0) in d:\wizzard\apache\www\home\mssql03.php3 on line 11

step -- 2
....3...

> is the result... ;(
actually, the base cannot be empty, for i'm querying it from the other GUI program, made on DELPHI.

....
0
 

Author Comment

by:wizzard
ID: 2526768
Khmmm, something strange with server.... I'll check out what exactly, for that script most probably WILL work!
The server is being restarted now, for it was 100% busy with something... ;)

I'll answer immediately.
0
 

Author Comment

by:wizzard
ID: 2527293
Adjusted points to 95
0
 

Author Comment

by:wizzard
ID: 2527294
I've examined the server's settings and figured that MSSQL Server on that server "WAP" uses "Linked Server" (Security/Linked Servers in SQL Server Enterprise Manager) that is named INSQL and is available if I'm querying it from that same server, but how can I use it from distance computer, like mine (it's in the same LAN)?

Wizz.
0
 

Author Comment

by:wizzard
ID: 2527485
Well, I can say that now we can finish this discussion, for I've figured out what to do:

I had to write the full path in Query:

"...insql.Runtime.dbo.AnalogLive..." in order to use that Linked Server (INSQL).

Thanks for help and I hope a grade A will will make you feel a lil better! ;)

BW,
[WizzarD].
0
 
LVL 1

Expert Comment

by:aikon
ID: 2527524
Hi Wizz,
I just had arrived of my launch!

I'm happy to be able to help you.

Ok thanks :)

Regars.
AikoN

0
 

Author Comment

by:wizzard
ID: 2545371
Howdy again!!!

Can you please help me once more? ;)
I'm writing here in order to alarm you that i'm posting a new question that is similar to my previous, but much easier!

See Ya,
[WizzarD]
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now