Page 1 of 1
Running a select query
Posted: Wed Sep 05, 2007 12:17 am
by 4Boredom
Im trying to select canchoice from my userbase and then display a numerical result for several candidates... COX is the one I am attempting with..
Am I going about this right? Im going to hack at it more tommorrow but this is where I got to before bed
Code: Select all
<?
$resultCOX = mysql_query("SELECT * FROM 'userbase' WHERE 'canchoice' = 'COX'");
print($resultCOX);
?>
Posted: Wed Sep 05, 2007 12:26 am
by anjanesh
$resultCOX returns a resource to perform further mysql fumction on them - it does not result the result set in one large array.
Code: Select all
$total_rows = mysql_num_rows($resultCOX));
while ($rowCOX = mysql_fetch_array($resultCOX))
{
echo $rowCOX['Id'];
}
Posted: Wed Sep 05, 2007 11:28 am
by 4Boredom
These are the results of our user poll
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/whosyour/public_html/viewresults.php on line 53
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/whosyour/public_html/viewresults.php on line 54
Posted: Wed Sep 05, 2007 11:33 am
by anjanesh
For your query do
Code: Select all
$resultCOX = mysql_query("SELECT * FROM 'userbase' WHERE 'canchoice' = 'COX'") or die(mysql_error());
Posted: Wed Sep 05, 2007 11:40 am
by 4Boredom
ok I changed the code to
Code: Select all
$resultCOX = mysql_query("SELECT * FROM userbase WHERE canchoice = COX") or die(mysql_error());
to get it to say something.. it says... Unknown column 'COX' in 'where clause'
What I want to do is count the # of times a user selects COX as their candidate in the canchoice column...
am I going about this wrong?
Posted: Wed Sep 05, 2007 1:26 pm
by VladSun
Should be
= 'COX'
not
= COX
Posted: Thu Sep 06, 2007 1:34 pm
by 4Boredom
yeah I had that but it returns no results and I definately have COX entered in as canchoice in my database for a user.. am i going about what I want wrong
Posted: Thu Sep 06, 2007 4:01 pm
by kostyai
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
maybe this will help:
[quote="4Boredom"]
Code: Select all
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$result = mysql_query("SELECT * FROM userbase WHERE canchoice = 'COX'");
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["field1"];
echo $row["field2"];
echo $row["field3"];
}
mysql_free_result($result);
?>
[/quote]
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Sep 07, 2007 3:32 pm
by 4Boredom
for field 1 if i write canchoice it returns COX.... if I put COX it returns no result
there are no errors..
this solves nothing however... I need to count the NUMBER of USERS that have selected COX as their canchoice
Posted: Fri Sep 07, 2007 3:39 pm
by s.dot
Of course, 'cox' is not a field in your database, however 'canchoice' is, so that is logical.
Code: Select all
$result = mysql_query("SELECT count(*) FROM `userbase` WHERE `canchoice` = 'COX'") or die(mysql_error());
echo mysql_result($result, 0); //should print the number who selected cox
Posted: Mon Sep 10, 2007 2:20 am
by 4Boredom
that works... thank you