Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Noobie

Status
Not open for further replies.

andyc209

IS-IT--Management
Dec 7, 2004
98
GB
after years of doing classic ASP i am giving PHP a go so bear with me.

i am trying to test a simple build a table from data in my SQL database and i keep getting errors, can someone check my code

<?php
// connect to the database
$connect = odbc_connect("ODBCNAME", "USERNAME", "PASSWORD");

// select everything from the table

$query = "SELECT * FROM test_table";
$result = odbc_connect($query);

while( ($row = mysql_fetch_array($result)))
{
echo " <td><input type='checkbox' /></td>";
echo " <td>".$row['ref']."</td>";
echo " <td><a href='client.html' class='tablelink'>".$row['name']."</a></td>";
echo " <td>".$row['type']."</td>";
echo " <td class='center'>".$row['tel']."</td>";
echo " </tr>";
// disconnect from the database
mysql_close();
?>

my last error is PHP Parse error: syntax error, unexpected $end

can someone please look at my code and see why i get the error and also let me know if its constructed badly etc. I want to use ODBC connections like i do in ASP and I also want to start including the connection string rather than using it on each page

thanks for any help
 
The unexpected end is because you have an open curly bracket for the WHILE loop that is not closed later. Add a closed curly bracket before // disconnect from the database.
 
Thanks for that - that got rid of the error. Problem is that when i run the page now it returns no results.I know the SQL statement is correct so i suspect its to do with the ODBC bit.

$connect = odbc_connect("DSN", "USERNAME", "PASSWORD");

Is there a way to be sure this is working? In ASP if it cannot connect to the DSN it gives an error but this does not, even when I deliberately change the password or DSN to an incorrect value.

my full code is now

<?php
// connect to the database
$connect = odbc_connect("DSN", "USERNAME", "PASSWORD");
$query = "SELECT * FROM test_table";
$result = odbc_connect($query);

while( ($row = mysql_fetch_array($result)))
{
echo "<tr class='gradeX'>";
echo "<td><input type='checkbox' /></td>";
echo " <td>".$row['ref']."</td>";
echo " <td><a href='client.html' class='tablelink'>".$row['name']."</a></td>";
echo " <td>".$row['type']."</td>";
echo " <td class='center'>".$row['tel']."</td>";
echo "</tr>";
}
// disconnect from the database
mysql_close();
?>
 
Hi

I have no idea about the [tt]odbc_*()[/tt] functions, but I am quite sure [tt]odbc_connect()[/tt] will not execute an SQL statement like in this line :
andyc209 said:
[tt]$result = odbc_connect($query);[/tt]

Beside that, wondering how good idea is to mix [tt]odbc_*()[/tt] and [tt]mysql_*()[/tt] functions. I would not expect their resources to be compatible with each other.

What kind of database are you using anyway ?

Feherke.
[link feherke.github.com/][/url]
 
odbc_connect() connects to a database, it does not execute a query, for that you use odbc_execute. If you are using odbc_ functions why are you then attempting to execute a mysql function that has no previous connection. I would assume that if using odbc connections you would want to use the follow-up odbc functions such as odbc_fetch_array.

But yes as feherke inquired, what Database are you using?, as that would mostly dictate what set of functions or abstraction layer you would want to use.


Perhaps a read through the PHP online manual may be useful. Here's the section for ODBC.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
on playing around with it i gotit to work

Code:
$sql = "SELECT * FROM test_table";
                $rs = odbc_exec($conn, $sql);
                if (!$rs)
                {exit("Error in SQL");}
                while (odbc_fetch_row($rs))
                {
                  $id = odbc_result($rs,"ID");
                  $name = odbc_result($rs,"name");
				  $type = odbc_result($rs,"type");
                  $tel = odbc_result($rs,"tel");
				  
				 				echo "    <tr class='gradeX'>";
								echo "    <td><input type='checkbox' /></td>";
                                echo "    <td>$id</td>";
                                echo "    <td><a href='client.html' class='tablelink'>$name</a></td>";
                                echo "    <td>$type</td>";
                                echo "    <td class='center'>$tel</td>";
                          		echo "    </tr>";
                }
                odbc_close($conn);

I am using SQL2005 btw for now.

My next issue is now i have this table with multiple rows and a checkbox on each, what i want is when a user selects say 5 rows of data when they hit the submit button it updates the rows in the database (for example changing a field from 0 to 1) - please bear with me but this is day one of my PHP voyage - desperate to get away from the old classic ASP - and i am only 4 hours into this voyage.

ASide from this great site is there any sites you know of where i can follow noobie tutorials - all my programming so far has been ASP and SQL VB script version so this is all totally different.

thanks
 
As I said read the online manual. It has many examples for many things and will help you get to know and understand the functions and methods that exist in PHP.

In any case, you'll want to incorporate a form so you can submit your checkboxes to another PHP script and have it process them and update the database.

Look into the POST and GET variables. Anything submitted from an HTML form will be in one of those superglobal array variables.

Start by being able to see what was sent from the previous script.

Code:
if(isset($_GET))
{
[tab] print_r($_GET);
}

From there you can craft your script to see which values were different from the ones in the database, and construct an update statement for the DB to alter those columns.

ASP encapsulates many things, in PHP you get down and gritty with the database calls and queries. You also need to get familiar with the interaction between html forms and PHP scripts.

NOTE: unchecked checkboxes do not get passed over to the superglobal variables. So you need to take that into consideration whe building your script and query.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top