Log in

View Full Version : check if mysql row exists


Nullifi3d
09-23-2005, 03:56 AM
I need to check if a mysql row exists for the visiting user ($vbulletin->userinfo['userid']). I am using the below code but am getting an error at the top of the pages.
if (!isset($_REQUEST['do'])) {
$uid = $vbulletin->userinfo['userid'];
$check = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
$result = mysql_num_rows($check);
if ($result !== "0") {
$bannerinfo = $db->query_first("SELECT status FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
if ($bannerinfo[status] == "declined") {
$_REQUEST['do'] = "add";
} else {
$_REQUEST['do'] = "statistics";
}
} else {
$_REQUEST['do'] = "add";
}
}
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /banners.php on line 58

Marco van Herwaarden
09-23-2005, 11:45 AM
query_first don't return a recordset, but an array with the values itself.

Change:
$result = mysql_num_rows($check);
if ($result !== "0") {

to:
if ($check) {

Nullifi3d
09-23-2005, 12:41 PM
thank you that works. I have one more question.

Why does the following work:if ($_REQUEST['do'] == 'statistics') {
$uid = $vbulletin->userinfo['userid'];
$check = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
$bannerinfo = $db->query_first("SELECT status FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
if ($check && $bannerinfo[status] !== "declined") {
$bannerinfo = $db->query_first("SELECT userid, username, title, url, html, ext, width, height, clicks, available, impressions, status FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
$banner = "<a target=\"_blank\" title=\"$bannerinfo[title]\" href=\"$bannerinfo[url]\"><img alt=\"$bannerinfo[title]\" src=\"http://www.webhostdebate.com/images/banners/$uid$bannerinfo[ext]\" border=\"0\" /></a>";
if ($bannerinfo[status] == "inactive") $inactive = "<div style=\"padding:5px\"><font color=\"#FF0000\"><b>Banner is awaiting activation!</b></font></div>";
$navbits[''] = $vbphrase['banner_statistics'];
$templatename = 'banners_statistics';
} else {
print_no_permission();
}
}And this does not:if ($_REQUEST['do'] == 'statistics') {
$uid = $vbulletin->userinfo['userid'];
$check = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
$bannerinfo = $db->query_first("SELECT status FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
if ($check && $bannerinfo[status] !== "declined") {
printStats();
} else {
print_no_permission();
}
}
function printStats() {
$bannerinfo = $db->query_first("SELECT userid, username, title, url, html, ext, width, height, clicks, available, impressions, status FROM " . TABLE_PREFIX . "banners WHERE userid = '$uid'");
$banner = "<a target=\"_blank\" title=\"$bannerinfo[title]\" href=\"$bannerinfo[url]\"><img alt=\"$bannerinfo[title]\" src=\"http://www.webhostdebate.com/images/banners/$uid$bannerinfo[ext]\" border=\"0\" /></a>";
if ($bannerinfo[status] == "inactive") $inactive = "<div style=\"padding:5px\"><font color=\"#FF0000\"><b>Banner is awaiting activation!</b></font></div>";
$navbits[''] = $vbphrase['banner_statistics'];
$templatename = 'banners_statistics';
}

Marco van Herwaarden
09-23-2005, 12:53 PM
This don't work becuase $db (and probably other var's) are outside scope in that function. Bring them into scope by adding a 'global $db;'

PS Better use AND then &&

Nullifi3d
09-23-2005, 01:28 PM
adding global $db; did stop the error, but produces a blank page.

I'm actually going to try and simply this by using another route in my code. I need to stop checking if the row exists and using functions. Instead I need to create a row for every username (upon registration).

Thanks for any help you gave me so far. Even though I decided not to use this now, knowledge is still usefull.

Marco van Herwaarden
09-23-2005, 01:32 PM
You will need to put all variables you want to use outside the function (like $navbits, $templatename) in the global line, same goes for all variables you want to use inside the function that already are set outside.

Nullifi3d
09-23-2005, 02:47 PM
I knew that (remembered from past code). I just don't want to deal with having to call a function multiple times in my code depending on whether or not a mysql row exists. Nor do I want to check for the row in each of my ifs. It's a hassle and requires more code.

Instead I would rather automatically create the new row for each user upon registration.

If you would like to help further please refer to threadid 96751. Thanks for all your help so far.