cheadirl Posted July 14, 2006 Share Posted July 14, 2006 Hi ,Ive got 2 tables in my db member and account - I want to be able to run a count to get me the ammount of times a member name matches in both tables, the SQL statement below is doing just that for me the problem I have is I have no idea how to echo that out on a website using php - can anyone help me ?[code]SELECT COUNT( member.Name ) AS counter, account.NameFROM memberINNER JOIN account act ON act.Name = member.NameGROUP BY member.Name[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/ Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 Try $Variable = Your SELECT statementecho "$Variable"; Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57922 Share on other sites More sharing options...
CheesierAngel Posted July 14, 2006 Share Posted July 14, 2006 You want echo your query :[code]<?php $query = <<<SQL SELECT COUNT( member.Name ) AS counter, account.Name FROM member INNER JOIN account act ON act.Name = member.Name GROUP BY member.NameSQL; echo $query;?>[/code]You want to echo the count:[code]<?php $query = <<<SQL SELECT COUNT( member.Name ) AS counter, account.Name FROM member INNER JOIN account act ON act.Name = member.Name GROUP BY member.NameSQL;$db = mysql_connect(HOST, USER, PASS)or die("Can't connect - " . mysql_error());mysql_select_db(DBNAME, $db)or die("Can't select db - " . mysql_error());$results = mysql_query($query, $db)or die("Can't execute query: $query - " . mysql_error());$result = mysql_fetch_object($results);echo $result->counter;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57923 Share on other sites More sharing options...
Oldiesmann Posted July 14, 2006 Share Posted July 14, 2006 Here's a slightly simpler version:[code]<?php// Connect to the database server...$connect = mysql_connect('localhost', 'username', 'password') or die("Could not connect to server!");// Tell MySQL which database we're working with$db = mysql_select_db('database', $connect) or die("Couldn't select database!");// Execute the query$query = mysql_query("SELECT COUNT(member.Name) AS counter, account.NameFROM memberINNER JOIN account act ON act.Name = member.NameGROUP BY member.Name") or die("Error executing query! MySQL said: " . mysql_error());// Display the resultecho 'Result: ' . mysql_result($query, 0);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57934 Share on other sites More sharing options...
cheadirl Posted July 14, 2006 Author Share Posted July 14, 2006 Hi ,Ive tried all these methods and Im not gettin any results unfortuantly - I either get Resource id #4 or only a single number printed - I need to be able to print the name and the ammount of times it appears, using the sql statment in SQL server I getName | CountJohn 10Anne 4etc no joy as of yet have tried all the usual echo statements but I just cant figure this one out :(Any more help would be great thanks Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57957 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 This may or may not help but have a look at this posting:- PHP Help > Check for duplicates in a MySQL database Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57963 Share on other sites More sharing options...
cheadirl Posted July 14, 2006 Author Share Posted July 14, 2006 Thanks for this - Ive seen that post it contains the same DB info that I was looking to generate but no php code to actually display the results on a page :( Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57968 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 To display your results you must put the results into a variable and then echo the variable.$VariableName = "Select statement";echo "$VariableName"; Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57977 Share on other sites More sharing options...
kenrbnsn Posted July 14, 2006 Share Posted July 14, 2006 In simplest terms, here is what you need to do to get results from a mysql DB:[code]<?php$q = "select * from tablename";$rs = mysql_query($q) or die("Problem with query:$q<br>" . mysql_error());while($rw = mysql_fetch_assoc($rs)) { echo '<pre>' . print_r($rw,true) . '</pre>';}?>[/code]Replace with your more complicated query ...Ken Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57981 Share on other sites More sharing options...
Guest huey4657 Posted July 14, 2006 Share Posted July 14, 2006 If there are many names and counts then the output needs to be in a loop in which you extract from the array Quote Link to comment https://forums.phpfreaks.com/topic/14586-help-needed-echoing-a-sql-statement/#findComment-57983 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.