PHP/MySQL Database

eliezer

Registered
i'm making a website and i want to create a mysql database that will link to it.

it's a car website. the database will contain one table which will contain a list of cars and there specifications.

i want to link this table with the website, so that all i have to do is type the car id at the top of a template page, and the rest of page will show all the car specs in a table.

this what i want to do:

<?php
connect to mysql databse, etc..........
$car_id="4"
?>

<table>
<tr>
<td>Make</td>
<td><?php print "SELECT Make FROM Cars WHERE car_id='$car_id'" ?></td>
</tr>
<tr>
<td>Model</td>
<td><?php print "SELECT Model FROM Cars WHERE car_id='$car_id'" ?></td>
</tr>
</table>
etc.

does this all make sense?
 
you want more like:

PHP:
<?
$query = mysql_query( "select * from `tblcars` WHERE `carID` = '$_GET[carID]'" ) or die( "Uh oh, there was an error!" . mysql_error() );

$car = mysql_fetch_assoc( $query );

echo( "Make: $car[make]<br />" );
echo( "Model: $car[model]<br />" );

?>

note that $_GET[carID] means you can use a <select> to change pages, and have one page called car.php which has no actual reference to a specific car on it (the whole idea behind this sorta thing).

and please don't use tables. yugh!
 
ok, i've done what you've said but it's not displaying anything, why is that?

this is the code:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PHP Test Page</title>
</head>

<body>

<?
$user="eliste_car";
$password="•••••••••";
$database="eliste_car";
mysql_connect("freesuperhost.com",$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$car_id="1";

$query1="SELECT Make FROM cars WHERE car_id='$car_id'";
$result1=mysql_query($query1);

$query2="SELECT Make FROM cars WHERE car_id='$car_id'";
$result2=mysql_query($query2);
?>

<table border="1">
    <tr>
        <td>Make</td>
        <td>
<? echo $result1; ?>
</td>
    </tr>
    <tr>
        <td>Model</td>
        <td>
<? echo $result2; ?>
</td>
    </tr>
</table>

<? 
$query = mysql_query( "select * from `cars` WHERE `car_id` = '$_GET[car_id]'" ) or die( "Uh oh, there was an error!" . mysql_error() ); 

$car = mysql_fetch_assoc( $query ); 

echo( "Make: $car[make]<br />" ); 
echo( "Model: $car[model]<br />" ); 

?>

<?
mysql_close();
?>
</body>
</html>

the table bit is the old code, but i've left it in.
your way and the table are both not showing the make and the model.
it should show: make --- nissan and model --- 350z
 
Your whole coding practise as well as how you approach analysing your problems stinks. At present you are misusing us to solve your problems instead of learning to debug your own code: http://www.onlamp.com/pub/a/php/2004/08/12/DebuggingPHP.html

And I previously gave you the link to the online php manual for the mysql funtions. Please follow their examples closely. Most of their examples are set up to provide feedback useful for debugging.

Please invest some more time in getting to know php and mysql. My advise is to invest the time in reading some books and articles on the subject:
http://www.oreilly.com/catalog/webdbapps2/
http://www.oreilly.com/catalog/0975240218/
http://scripting.oreilly.com/
http://databases.oreilly.com/
http://www.oreilly.com/catalog/0957921853/
http://www.oreilly.com/catalog/0957921845/
 
Back
Top