Page 1 of 2

sql connect help

Posted: Wed Jan 05, 2005 6:14 pm
by rodrigorules
i found this script on a site...i pretty much understand it
BUT i have no idea what "rs" is... what is it?
like the line - while (odbc_fetch_row($rs))
or the lines ->
$compname=odbc_result($rs,"CompanyName");
$conname=odbc_result($rs,"ContactName");
anyone know?
------

Code: Select all

<html><body><?php
$conn=odbc_connect('northwind','',''); 
if (!$conn)
&#123;
exit("Connection Failed: " . $conn); 
&#125; 
$sql="SELECT * FROM customers"; 
$rs=odbc_exec($conn,$sql); 
if (!$rs)
&#123; 
exit("Error in SQL");
&#125; 
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
&#123;
$compname=odbc_result($rs,"CompanyName");
$conname=odbc_result($rs,"ContactName");
echo "<tr><td>$compname</td>";
echo "<td>$conname</td></tr>";
&#125;
odbc_close($conn);
echo "</table>";
?>
</body>
</html>

Posted: Wed Jan 05, 2005 6:21 pm
by timvw
(don't kill me if i'm wrong, but this is more or less how it goes)
If you want to know more about functions, this is a good tip:
surf to http://www.php.net/function_name, so in your case
http://www.php.net/odbc_exec
http://www.php.net/odbc_fetch_row
...


if you perform on a query on a rdbms, the result is a set (of tuples).. in other words, a resultset. so usually a programmer chooses a variable with the name $rs or $result.

Code: Select all

$rs=odbc_exec($conn,$sql);
and then you iterate over all the rows in that set like

Code: Select all

while (odbc_fetch_row($rs))
and finally you retrieve the info from each of those tuples into a variable:

Code: Select all

$compname=odbc_result($rs,"CompanyName");

Posted: Wed Jan 05, 2005 6:47 pm
by rodrigorules
SOLVED
well look...i understood! and i cahnged the varibles and made some more tables...
ima add a background and stuff...and there i have my Gameserver ranking page..
BUT
how would i make the Table in order from highest level to lowest lvl?
this is the page
SOLVED
http://www.simplemu.no-ip.org/branking.php
SOLVED
the code is this!

Code: Select all

<html><center><body><?php
$conn=odbc_connect('MuOnline','',''); 
if (!$conn)
&#123;
exit("Connection Failed: " . $conn); 
&#125; $sql="SELECT * FROM character WHERE CtlCode IS NULL ORDER BY cLevel LIMIT 35 DESC";
 $rs=odbc_exec($conn,$sql); 
if (!$rs)
&#123; 
exit("Error in SQL");
&#125; 
echo "<table border=1><tr>";echo "<th>Name</th>";
echo "<th>Level</th>";
echo "<th>Money</th>";
echo "<th>Experiance</th>";
echo "<th>Strength</th>";
echo "<th>Agility</th>";
echo "<th>Vitality</th>";
echo "<th>Energy</th></tr>";
while (odbc_fetch_row($rs))


&#123;
$xp=odbc_result($rs,"Experience");
$str=odbc_result($rs,"Strength");
$agi=odbc_result($rs,"Dexterity");
$vit=odbc_result($rs,"Vitality");
$nrg=odbc_result($rs,"Energy");
$zen=odbc_result($rs,"Money");
$pname=odbc_result($rs,"Name");
$level=odbc_result($rs,"cLevel");
echo "<tr><td>$pname</td>";
echo "<td>$level</td>";
echo "<td>$zen</td>";
echo "<td>$xp</td>";
echo "<td>$str</td>";
echo "<td>$agi</td>";
echo "<td>$vit</td>";
echo "<td>$nrg</td></tr>";
&#125;
odbc_close($conn);
echo "</table>";




?></body></html>
(right now it goes by ABC order by the name table.. =/ )
k thanks

Posted: Wed Jan 05, 2005 7:06 pm
by markl999
$sql="SELECT * FROM character ORDER BY cLevel DESC"

Posted: Wed Jan 05, 2005 7:10 pm
by rodrigorules
worked perfectly thanks

NOW how would i make it so...ONLY users with the row "CtlCode" witha value of NULL show up

BECUASE - GameMasters have a CtlCode of 8, i dont want GM's to show up on ranking.

i have tried this!

Code: Select all

<?php
$sql="SELECT * FROM character WHERE CtlCode=NULL ORDER BY cLevel DESC";
?>
but then all the users with CtlCode 8 AND null didnt show up..

any solution?
hope u understand :)

Posted: Wed Jan 05, 2005 7:33 pm
by timvw
in sql null is not equal to null.....

if i'm nto mistaken you should use the isnull function

select * from foo where isnull(bar) and bar2 = 'foo2';

Posted: Wed Jan 05, 2005 7:40 pm
by rodrigorules
huh? O_O
u mean
$sql="SELECT * FROM character WHERE isnull(CtlCode) ORDER BY cLevel DESC";

(character, clevel, and CtlCode are Collums)
? is that what u mean?
i didnt understand the "foo" and bars...
im new to php and stuff like this

Posted: Wed Jan 05, 2005 7:47 pm
by John Cartwright

Code: Select all

$sql="SELECT * FROM character WHERE CtlCode IS NOT NULL ORDER BY cLevel DESC";

Posted: Wed Jan 05, 2005 7:50 pm
by rodrigorules
not it only shows the ones which ARE NOT NULL...
how would i make it show those which ARE NULL only?
change it to...
WHERE CtlCode IS NULL
?
ill try it

YEP thanks u put me on the right track
right code was

Code: Select all

<?php
$sql="SELECT * FROM character WHERE CtlCode IS NULL ORDER BY cLevel DESC"; 
?>

Posted: Wed Jan 05, 2005 7:51 pm
by John Cartwright
change it to...
WHERE CtlCode IS NULL
Sorry, my mistake. Your correct.

Posted: Wed Jan 05, 2005 7:53 pm
by rodrigorules
another question...lol

how would i make the chart...35 people max?
like 36 rows(counting the stop row..so thats 35 people)
anyway to do this?

Posted: Wed Jan 05, 2005 7:54 pm
by John Cartwright

Code: Select all

<?php
$sql="SELECT * FROM character WHERE CtlCode IS NULL ORDER BY cLevel DESC LIMIT 35"; 
?>
refer to here when in doubt http://dev.mysql.com/doc/mysql/en/index.html

Posted: Wed Jan 05, 2005 8:02 pm
by rodrigorules
heh its huge, so anyone know how to make the table 35 MAX?

someone told me it had something to do with this!

Code: Select all

<?php
for($i=0;$i < 100;++$i)
{
$row = mssql_fetch_row($rs);
?>
but i dont know how to put it into mine..

Posted: Wed Jan 05, 2005 8:09 pm
by John Cartwright
use the previous code in my last post

Posted: Wed Jan 05, 2005 8:10 pm
by rodrigorules
lol didnt see that, thanks