Results 1 to 8 of 8

Thread: Querying MySQL [Resolved]

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Querying MySQL [Resolved]

    I though I knew a bit about querying MySQl, but the PHP syntax is killing me here...

    I want to get the results from a table called arrangement in a database that is called klub_info. I only want to get the fields that has an O in the aBy field. But I can't get the syntax to work at all. I have no clue here...I am stil reading in my stupid book here. But it's not a very good book.

    this is what I more or less wrote straight from my book:

    Code:
    $sterm = 'O';
    $stype = 'aBy';
    $resultat = mysql_query("select * from arrangement where ".$stype." like '%".$sterm."%'",$db);
    
    		
    while($arr = mysql_fetch_array($resultat)){
    	echo '$resultat';
    						
    }

    But I can't see where I am choosing the table I want. And I havn't even gotten to the point to use Select to strip down to the fields I actually want from my search result.

    PS: aBy is an Enum field that can only hold 'O', 'B' or 'T'....


    [Edit]Added two lines I forgot to the code[/edit]
    Any ideas?
    Last edited by NoteMe; Apr 13th, 2004 at 06:33 PM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The table is selected by the from part of the query.
    The actual database is selected with a call to mysql_select_db, which is usually issued directly after the mysql_connect.

    mysql_fetch_array retrieves one row of the result as a dual array. It has both numeric indices (0 being the first column etc.) and string indices (each column has a name, that may be the name of the column in the table, the full expression in the select or an alias set with the AS keyword).
    mysql_fetch_row retrieves only numeric indices, mysql_fetch_assoc only string indices.

    Example. Table foo has the columns id, start and end. start and end are time values.
    The query
    SELECT id, UNIX_TIMESTAMP(start), UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start) AS dur FROM foo WHERE end > '04-15-2002 11:30';
    (not sure about the time format)
    retrieves all entries that end after this date. It retrieves their id, the unix timestamp of their start time and their duration in seconds.
    The array resulting from mysql_fetch_array has these indices:
    0: value of id column
    1: unix timestamp of start time
    2: duration in seconds
    'id': value of id column
    'UNIX_TIMESTAMP(start)': unix timestamp of start time
    'dur': duration, note the use of the AS keyword

    mysql_fetch_row only gives the numeric indices of this, mysql_fetch_assoc only the strings.

    echo '$resultat';
    will everytime simply literally write
    $resultat
    because variable substitution is only done in double-quoted strings, not single-quoted.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I got most of what you said. But it is not working 100% yet.


    Here is my code now:

    Code:
    $resultat = mysql_query("select aDato, aStedID, aNavn from arrangement where aBy like 'O'",$db);
    //$resultat = mysql_query("select * from arrangement");
    
    while($row = mysql_fetch_row($resultat)){
    
    $Dato = $row["aDato"];
    $Sted = $row["aStedID"];
    $Name = $row["aNavn"];
    
    printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",$Dato,$Sted,$Name);
    						
    }
    If I use it is it is now. I am getting nothing...if I use the search term that I have commented out, the date is OK...but the field called aStedID and the field aNavn gives med 0, and 0. I am not 100% sure if that is wrong either....but I am pretty sure that the Name should come out as AGORA and not 0.

    I am using MyphpAdmin to make the tables. Anyone know if I can check the data in the tables using MyphpAdmin, so I don't have to track this "error" down if it maybe isn't an error?

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I found a search option in MyPHPAdmin. So it looks like the aNavn field was neer saved when I added the Record. So that means that every thing is working OK for now. Thanks CornedBee..

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    No, not everything...the WHERE condition is not working....the aBY field is still a ENUM field where it can only contain O, B or T....any ideas whats wrong with the syntax there?

  6. #6

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Think I got it working like this now.


    Code:
    $resultat = mysql_query("select aDato, aStedID, aNavn from arrangement where aBy like 'O'",$db);

    Thanks.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use =, not like, for exact comparisons.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Thanks, but since it is a ENUM field it has nothing to say in this query anyway. But I will remember it for the next one.

    ØØ

Posting Permissions

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



Click Here to Expand Forum to Full Width