PHP help

dragon2309

P.I Dragon
Right, i am just getting started n the world of PHP, i know the 'conn' variable and ODBC connect syntax's. I upload this script and browse to it only to recieve errors saying that there was a call error to one of the variables, i ahve checked and rechecked everything and even taken out all of the IF and WHILE statements that could be going wrong. I simply have the odbc_connect() function and then the sql_(SELECT) function followng it seperate by standard break ';' I cant think of anything else it could be, anyone got any ideas.

If you need to view the script then just ask and i will send it to you.


**UPDATE** I think i got it to actually connect to the database but the page now comes up blank, no error message or anything like before. Is there something i need to add to actually get it to dispaly the data contained in the table that i queried? I heard something about exporting query data to XML sheets, is that what i need to do.
 
Last edited:
You need to echo a html table I believe with all the results of the query in it. A example of a program I wrote might be helpful.

PHP:
<body>
 
   <h1>Cars Database Table</h1>
 
   <hr />
   
   <table border="1">
     <tr>
       <th>ID</th>
       <th>Make</th>
       <th>Model</th>
       <th>Year</th>
       <th>Price</th>
     </tr>


<?php
     include "dbinfo.php";
     
     // Connect to MySQL DBMS
     if (!($connection = @ mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD)))
       showerror();
 
     // Use the cars database
     if (!mysql_select_db(DB_DATABASENAME, $connection))
       showerror();
 
     // Create SQL statement
     $query = "SELECT * FROM cars";
 
     // Execute SQL statement
     if (!($result = @ mysql_query ($query, $connection)))
       showerror();
 
     // Display results
     while ($row = @ mysql_fetch_array($result))
       echo "<tr><td>{$row["id"]}</td>
       <td>{$row["make"]}</td>
       <td>{$row["model"]}</td>
       <td>{$row["year"]}</td>
       <td>{$row["price"]}</td></tr>";
   ?>
 
Could you please explain to me what this is actually doing an what is the need for dbinfo.php. I cant seem to get it to work, it comes up with errors all the time.
 
dbinfo.php will have DB_HOSTNAME, DB_USERNAME, DB_PASSWORD defined in it. it's a method of making the script modular, fill these values in with what you should be using to log into your DB and remove the include line
 
Cromewell said:
dbinfo.php will have DB_HOSTNAME, DB_USERNAME, DB_PASSWORD defined in it. it's a method of making the script modular, fill these values in with what you should be using to log into your DB and remove the include line

opps should of mention that. It done for security reason A and B you can use it for mutiple php pages so if something change you only have to change that one page.

It includes the information cromewll mention plus DB_DATABASENAME. I also included the defintion of the showerror in there.

Here what my dbinfo.php looks like
PHP:
<?php
/*
* dbinfo.php
*/
define("DB_USERNAME", "mgoldb2"); 
define("DB_PASSWORD", "enter password here");
define("DB_HOSTNAME", "localhost:/usr/cslocal/var/mysqldata/triton.sock");
define("DB_DATABASENAME", "mgoldb2db"); 
// Show an error and stop the script
function showerror()
{
if ( mysql_error() )
{
die( "Error " . mysql_errno() . " : " . mysql_error() );
}
else
{
die( "Could not connect to the DBMS" );
}
}
?>

replace enter password here with your password and replace all other defintions with your info
 
To houssam_ballout: Thanks for the sugestion but i have been there and read the tutorial several times :)

To mgoldb2: You have been a saviour, thanks a million. Everything works fine. Of course i have some more questions for you.

1. Is it possible to call up more than one table on each page, if so, how?

2. Is it possible to filter out query results (e.g. Only show records in a table that have the value of 1 in a given field. I.E. My stockcount field for each record, if it has 0 in it i do not want it to be shown on the page. If it has a 1 in it then i want it to be shown.)

Thanks once again for all your help and your sample code. :)
 
Cromewell said:
You could even eliminate stock of 0 at the SQL level (select * from stuff where stock > 0)

Personally this the way I would do it. Just make the query only return results you want.

change this

PHP:
// Create SQL statement
     $query = "SELECT * FROM cars";

to
PHP:
// Create SQL statement
     $query = "SELECT * FROM cars WHERE stock > 1";

Also you can have the user make query choises for example the following code

PHP:
</head>
 <body>
 
   <h1>make Query</h1>
   
   <form id="make_select" action="<?php $PHP_SELF; ?>" method="post">
   <p>
     Select vehicle make:
     <select name="make">
       <option value="Jeep">Jeep</option>
       <option value="Chevrolet">Chevrolet</option>
       <option value="Ford">Ford</option>
     </select>
 
     <input type="submit" />
   </p>
   </form>
 
   <?php
     if( ! empty( $_POST["make"] ) )
     {
       echo '<table border="1">
             <tr>
               <th>ID</th>
               <th>Make</th>
               <th>Model</th>
               <th>Year</th>
               <th>Price</th>
             </tr>';
 
       require "dbinfo.php";
 
       // Connect to MySQL DBMS
       if (!($connection = @ mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD)))
         showerror();
 
       // Use the cars database
       if (!mysql_select_db(DB_DATABASENAME, $connection))
         showerror();
 
       // Create SQL statement
       $make = $_POST["make"];
 
       $query = "SELECT * FROM cars WHERE make='$make'";
 
       // Execute SQL statement
       if (!($result = @ mysql_query ($query, $connection)))
         showerror();
 
       // Display results
       while ($row = @ mysql_fetch_array($result))
         echo "<tr>
                 <td>{$row["id"]}</td>
                 <td>{$row["make"]}</td>
                 <td>{$row["model"]}</td>
                 <td>{$row["year"]}</td>
                 <td>{$row["price"]}</td>
               </tr>";
 
       echo "</table>";
     }
   ?>
 
Last edited:
I like the sound of user customisable queries. I may try that later on when i have everything else set up. I know have a few more questions if you dont mind answering them for me??

1. Can i make a record in a query result a hyperlink, say my table is of necklaces and when Necklace301 gets displayed as a result from a query i want the user to be able to click on Necklace301 and go to the decription page of that necklace, can this be done or is it too complex.

2. Can an image be attatched to each record in the database, this is so that when a query returns all the results and they are in the table there is a picture of Necklace301 next to it and 302 next to 302 and so on, can this be done or is it again too complex.

Thanks in advance because i just know you will conme up with something, you have been extremely good so far and have helped immensely, thank you. dragon2309
 
1. I would store the URL of the necklace in the table (probably as a php page so that i can say displayitem.php?item=necklace or something like that) and then print the html link in the php script
PHP:
print "<a href=$row[URL]> $row[name]</a>";

2. Same idea as 1 but store the img path (ie. images\necklace.jpg) and print img tags in the php script
 
Im sorry, you lost me in all of that, where should the hyperlink go and should it be in the same table as the necklace or a different one, should i create a new field or what. Sorry for sounding dumb here.
 
ok say your products table looks like this:
products---
ProductName, Price, Stock, URL, image

I would have a php page called showproduct.php or showitem.php or whatever you like really (like the way this forum works). So you pull the link out of the product table and put it in the table you are using to organize the query results using the GET variable to indicate what product description it should go to
 
Can you give an example of this, i think i understand but am not quite sure how to implement this new php page (showproduct.php). The URL's are now put into the products table, where do i go from there. can you give an exact piece of code to GET the page www.simplytrue.co.uk/necklaces_1.htm on this new showproduct.php page.
 
You don't have to use a showproduct page but it allows you to add/remove things very easily because all you have to do is add/remove a row from the table.
basically showproduct.php would recreate the page you've got but it wouldn't fill in any of the changing areas (the picture, description, price, etc)
You could even use that page as a template (change the image and description and stuff to {IMG} or something easy to find and replace) and use substr to replace the variable stuff with what you need.

I think I have my PHP forum project at home, it uses a show page I'll send it to you if you like.
 
I have entered the html link into the mysql table it is now being displayed in the query results. The only problem is that it is in just plain text, it is not a hyperlink and there is no way to click on it, unless the user physically copies and pastes it into their browser address bar, which defeats the object of it in the first place. Any ideas??

Is there a certain field type it has to be set to in the SQL level of things, at the moment it is on varchar which is default. I had a quick look through and didnt find any hyperlink stuff.

The same with any links to image files, i think i am doing it wrong, can you help.

*UPDATE* This is what it comes out like.....
 
Last edited:
Back
Top