Misia
Misia

Reputation: 1

Check if the name is in database?

I have a table, which includes 16 car names. I want to change the font color and size if a car's name is found in a database.

This is my table:

enter image description here

And this is my PHP code for the first three cars:

<?php
$connection = mysql_connect($host,$user,$password);
if (!$connection){
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("cars", $connection);

$carname="car";
$squery = mysql_query("SELECT * FROM marks WHERE care_name=$car_name ");
echo"<div class='cars'>";
if(mysql_fetch_array($squery))  {
    echo"<r><strong>". $row['car1'] ."<strong></r>";
}
else{
    echo"<p>". $row['car1'] ."</p>";
}
echo"</div>";

echo"<div class='cars'>";
if(mysql_fetch_array($squery))  {
    echo"<r><strong>". $row['car2'] ."<strong></r>";
}
else{
    echo"<p>". $row['car2'] ."</p>";
}
echo"</div>";

echo"<div class='cars'>";
if(mysql_fetch_array($squery))  {
    echo"<r><strong>". $row['car3'] ."<strong></r>";
}
else{
    echo"<p>". $row['car3'] ."</p>";
}
echo"</div>";

?>

However, this doesn't work, it makes only on the first car (car1).

Upvotes: 0

Views: 135

Answers (1)

FluffyKitten
FluffyKitten

Reputation: 14312

You are using variables such as $row without assigning it a value first. You need to save the return value from mysql_fetch_array into the $row variable e.g.

$row = mysql_fetch_array($squery);
if($row){
...
}

EDIT: On further examination of your code, it contains multiple basic errors such as

  • using variables that are not given a value;
  • you use a different name each time you access the car from $row (at
    least I hope this is a coding error and not that you have 16 columns in your database table, one for each car...);
  • if mysql_fetch_array fails, you still try to print the contents
  • You are writing code for each individual mysql_fetch_array instead of using a loop

and there are probably many others. You really need to improve your basic understanding of coding practices (and possibly databases) before we can help further, as there are too many basic errors here that show a lack of understanding of what you are trying to do

Upvotes: 2

Related Questions