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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tables in MySQL

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi.

I have made a php script with an MySQL database.
Whar I want to do is to count the number of enteries i the databas Cars.

Whats wrong wit this code ?

$db = @mysql_connect("143.141.2.122","root","math")
or die ("Can't open ...");

$databasen = mysql_select_db("Cars");
$fraga = "SELECT COUNT(*) AS Color FROM Modell";

$resultat = mysql_query($fraga);

echo $resultat;

I only get thi error code 'Resource id #2'
 
The function mysql_query returns the resource identifier for the query, not the results of that query.

after $resultat = mysql_query($fraga); you are going to want to 'fetch' the querys result from the resource id. The query you have there will just count the number of total records in the table. If you wanted to get a count for each color you would need to change your query to something like this:

$fraga = "SELECT Color,COUNT(*) AS count FROM Modell GROUP BY Color";
$resultat = mysql_query($fraga);
while ($modellcount = mysql_fetch_array($resultat)){
echo &quot;$modellcount[Color] : $modellcount[count] <BR>&quot;;
}

If you were just trying to count all the records in the table Modell, then you could do the following:

$fraga = &quot;SELECT * FROM Modell&quot;;
$resultat = mysql_query($fraga);
echo mysql_num_rows($resultat);

hope this helps :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top