|
-
Aug 13th, 2006, 08:41 AM
#1
Thread Starter
Addicted Member
[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?
... prints: Resource id #11
-
Aug 13th, 2006, 09:03 AM
#2
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
}
-
Aug 13th, 2006, 11:03 AM
#3
Thread Starter
Addicted Member
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'];
-
Aug 13th, 2006, 11:04 AM
#4
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.
-
Aug 13th, 2006, 11:37 AM
#5
<?="Moderator"?>
Re: php + mysql question
Have you tried using print_r to display the array of results that is being returned from your query?
-
Aug 13th, 2006, 01:30 PM
#6
Thread Starter
Addicted Member
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?
-
Aug 13th, 2006, 01:45 PM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|