Results 1 to 7 of 7

Thread: [RESOLVED] php + mysql question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Resolved [RESOLVED] php + mysql question

    Something odd is going on here. The following code does not return $site_id when it should:

    Code:
    			$query = 'select `site_id` from `sites` where `url`='$url'';
    			$result = mysql_query($query);
    			echo mysql_error();
    			$row = mysql_fetch_row($result);
    			if ($site_id != "")
    				siteScreen($site_id, $message);
    			else
    				showsites($message);
    I excute the same query from PhpMyAdmin, and it works fine:
    Code:
    select `site_id` from `sites` where `url`='http://www.yahoo.com/'
    If its incorrect, then how do I get the $site_id based on a given $url string?

    Code:
    print $result;
    ... prints: Resource id #11

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: php + mysql question

    None of the mysql_* functions automatically populate variables for you. You access fields returned in a dataset by using the index notation, either by numerical index, or, if you use mysql_fetch_assoc(), the field name.

    Also, you should check whether you need to call mysql_error() or not.

    PHP Code:
    $result mysql_query('select `site_id` from `sites` where `url`='$url'');
    if (!
    is_resource($result)) echo mysql_error();

    if (
    mysql_num_rows($result) > 0) {
      
    $row mysql_fetch_assoc($result);
      if (
    $row['site_id'] != '')
        
    siteScreen($site_id$message);
      else
        
    showsites($message);
    }
    else {
      
    // Not found, place handler code here


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: php + mysql question

    Doesnt work either
    PHP Code:
    $result mysql_query('select `site_id` from `sites` where `url`=\'$url\''); 
    if (!
    is_resource($result)) { echo mysql_error(); }
    if (
    mysql_num_rows($result) > 0) {
        
    $row mysql_fetch_assoc($result);
        if (
    $row['site_id'] != '') {
            
    siteScreen($row['site_id'], $message);
            }
        else {
            
    showsites($message);
            }
        }
    else { 
        
    showsites($message);
        }
    print 
    "<hr>".$row['site_id']; 

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: php + mysql question

    What happens then?

    Also, on an unrelated note, don't use print or echo to output HTML code, unless it's just for debugging. There's no point otherwise.

  5. #5
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: php + mysql question

    Have you tried using print_r to display the array of results that is being returned from your query?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: php + mysql question

    PHP Code:
    print "<hr>";
    print_r ($row);
    print 
    "<hr>";
    print_r ($result); 
    $row does not print anything.
    $result prints Resource id #11

    What could that be?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: php + mysql question

    Thanks guys, I got this working:
    PHP Code:

                $result 
    mysql_query("select site_ID from sites where url='$url'");
                echo 
    mysql_error();
                
    $row mysql_fetch_row($result);
                
    $site_id $row[0];

                print 
    "<hr>".$site_id

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