Running a select query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Running a select query

Post 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);

?>
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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'];
 }
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

For your query do

Code: Select all

$resultCOX = mysql_query("SELECT * FROM 'userbase' WHERE 'canchoice' = 'COX'") or die(mysql_error()); 
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Should be
= 'COX'
not
= COX
There are 10 types of people in this world, those who understand binary and those who don't
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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
kostyai
Forum Newbie
Posts: 3
Joined: Thu Sep 06, 2007 1:57 pm

Post by kostyai »

feyd | Please use

Code: Select all

,

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

,

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]
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
4Boredom
Forum Contributor
Posts: 176
Joined: Tue Nov 08, 2005 4:29 pm

Post by 4Boredom »

that works... thank you
Post Reply