SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple SELECT COUNT Problem

    I have what appears to be a very simple problem, though I don't understand it. I created a new static page at MySite/K-2/Alphabet, which includes another file with all the scripting, Alphabet.php. It accesses a database table with the following script:

    PHP Code:
    $MyURL = ($_GET['Ltr']);

    $result mysql_result(mysql_query("SELECT COUNT(URL)
     FROM px_alphabet
     WHERE URL = '
    $MyURL'"),0);

    switch(
    $result)
    {
     case 
    1:
     echo 
    $MyURL;
     break;

     case 
    0:
     echo 
    'ERROR!';
     break;

     default:
     echo 
    'DUPE';
     break;

    The table/field px_alphabet.URL simply contains one-character values for each letter of the alphabet, A-Z. So if I type MySite/K-2/Alphabet/A into my browser, it should display A. if I type in MySite/K-2/Alphabet/A2, I should get an error message. Instead, it displays A2.

    If I delete $MyURL = ($_GET['Ltr']);, then it displays nothing at all, which suggests that my .htaccess file is working.

    In a situation as simple as this, I could simply use a switch like the following:

    PHP Code:
    switch($MyURL)
    {
     case 
    'A';
     case 
    'A/';
     case 
    'B';
     case 
    'B/';
     case 
    'C';
     case 
    'C/';
     echo 
    $MyURL;
     break;

     default:
     echo 
    'ERROR!';
     break;

    But I'd like to figure out what I'm doing wrong here. Thanks.

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_result returns a string. try mysql_num_rows. also have you var_dump()'ed $result to see what its getting back from the database?

  3. #3
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming your query returns an associative array, it should be:

    PHP Code:
    switch($result['COUNT(URL)']) {
    ...


  4. #4
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he used mysql_result to get the data so it returns a string not an array

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    he used mysql_result to get the data so it returns a string not an array
    My bad. I've been using PDO for so long, I don't even remember all the mysql_x functions.

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    mysql_result returns a string. try mysql_num_rows. also have you var_dump()'ed $result to see what its getting back from the database?
    Thanks for the tips. I appear to have it squared away now...though I went back to $result = mysql_result(mysql_query...

    I discovered that's indeed what I'm using in my original queries, which do work. When I tried mysql_num_rows, I got the following error message:

    Wrong parameter count for mysql_num_rows()
    That's probably because I was implementing it incorrectly. But it's working now.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe your passing a wrong prameter
    parameter should be a resource data from sql (mysql_query)

  8. #8
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite View Post
    Thanks for the tips. I appear to have it squared away now...though I went back to $result = mysql_result(mysql_query...

    I discovered that's indeed what I'm using in my original queries, which do work. When I tried mysql_num_rows, I got the following error message:



    That's probably because I was implementing it incorrectly. But it's working now.
    It's because mysql_num_rows accepts only 1 parametere, where as if you just replaced mysql_result to mysql_num_rows, u forgot to remove the other parameters that came along with mysql_result.

    I do that all the time


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •