create a loop to check all records

i have the following query

[php]mysql_select_db($database_TickTo, $TickTo);
$query_rsVoucher = “SELECT * FROM TickTo_vouchercode”;
$rsVoucher = mysql_query($query_rsVoucher, $TickTo) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher);
$totalRows_rsVoucher = mysql_num_rows($rsVoucher); [/php]

i need to search the db for all records but the results are currently only search and showing the results for the first col in the database, i have tried some tutorials and tried a few variation but it doesnt seem to be working as the tutorials are echoing out the information within the loop but i dont want it like that.

i tried this

[php]mysql_select_db($database_TickTo, $TickTo);
$query_rsVoucher = “SELECT * FROM TickTo_vouchercode”;
$rsVoucher = mysql_query($query_rsVoucher, $TickTo) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher) or die(mysql_error());

while($row_rsVoucher = mysql_num_rows($rsVoucher)){
echo $row_rsVoucher[‘VCode’];
echo “
”;
}
$totalRows_rsVoucher = mysql_num_rows($rsVoucher); [/php]

thanks

Well a while loop is the correct way to get all the rows from the db. What are you trying to achieve. You say you don’t want it echoed or you don’t want to use a while loop? Explain further please. If you’re wanting to put all the rows into a variable then this should do it.
[php]
mysql_select_db($database_TickTo, $TickTo);
$query_rsVoucher = “SELECT * FROM TickTo_vouchercode”;
$rsVoucher = mysql_query($query_rsVoucher, $TickTo) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher) or die(mysql_error());

while($row_rsVoucher = mysql_num_rows($rsVoucher)){
$new_var[] = $row_rsVoucher[‘VCode’];
}
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);
print_r($new_var);
[/php]

ok let me go into more depth of what i want to do

i have a discount code(voucher) that when the user adds a code to a form field it checks the database to make sure that code is a real one then applys the discount

this is the script

[php]// voucher code
if (isset($_POST[‘vouchCode’]) && $_POST[‘vouchCode’] == $row_rsVoucher[‘VCode’]) {
$mycode = $row_rsVoucher[‘VCode’];
$spos = strpos($mycode, “f”);
if ($spos !== false) {
$myvalue = substr($mycode, $spos+1);
$myvalue = $XCart_sumTotal * $myvalue / 100;
} else {
$spos = strpos($mycode, “p”);
if ($spos !== false) {
$myvalue = substr($mycode, $spos+1);
}
}
$myTotal = $XCart_sumTotal - $myvalue;
$_SESSION[‘vouchCode’] = $myvalue;
} else unset($_SESSION[‘vouchCode’]);

function DoFormatCurrency($num,$dec,$sdec,$sgrp,$sym,$cnt) {
setlocale(LC_MONETARY, $cnt);
if ($sdec == “C”) {
$locale_info = localeconv();
$sdec = $locale_info[“mon_decimal_point”];
$sgrp = $sgrp!="" ? $locale_info[“mon_thousands_sep”] : “”;
$sym = $cnt!="" ? $locale_info[“currency_symbol”] : $sym;
}
$thenum = $sym.number_format($num,$dec,$sdec,$sgrp);
return $thenum;
}[/php]

the form
[php]


Voucher Code:


[/php]

echoed value showing discount
[php]<?php echo $myvalue ?>[/php]

so thats why i dont want the echo where i had it as it going into another part of the script… does that make sense?

thanks in advance

If you’re just trying to see if a specific value is in a database row then you would use a WHERE clause in the query to find that value.
[php]
$code = mysql_real_escape_string($_POST[‘vouchCode’]);
$query_rsVoucher = “SELECT * FROM TickTo_vouchercode WHERE VCode = {$code}”;
[/php]
Then just check if the query returned any rows. If it did then the code is valid, if no rows returned then it’s not valid.

hi yeah i tried that but it returned and error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘TickTo_vouchercode’ WHERE ‘VCode’ =’ at line 1

i did miss a part of the code out so though i would post it again…if thats ok (this is all the voucher code script)

[php]if ((isset($_POST[“MM_update”])) && ($_POST[“MM_update”] == “form2”)) {
$updateSQL = sprintf(“UPDATE TickTo_voucher SET vouchCode=%s WHERE vouchID=%s”,
GetSQLValueString($_POST[‘vouchCode’], “text”),
GetSQLValueString($_POST[‘vouchID’], “int”));

mysql_select_db($database_TickTo, $TickTo);
$Result1 = mysql_query($updateSQL, $TickTo) or die(mysql_error());
}[/php]

this is the modified version

of the next part

[php]$code = mysql_real_escape_string($_POST[‘vouchCode’]);
mysql_select_db($database_TickTo, $TickTo);
$query_rsVoucher = “SELECT * FROM ‘TickTo_vouchercode’ WHERE ‘VCode’ = {$code}”;
$rsVoucher = mysql_query($query_rsVoucher, $TickTo) or die(mysql_error());
$row_rsVoucher = mysql_fetch_assoc($rsVoucher) or die(mysql_error());
$totalRows_rsVoucher = mysql_num_rows($rsVoucher);[/php]

[php]// voucher code
if (isset($_POST[‘vouchCode’]) && $_POST[‘vouchCode’] == $row_rsVoucher[‘VCode’]) {
$mycode = $row_rsVoucher[‘VCode’];
$spos = strpos($mycode, “f”);
if ($spos !== false) {
$myvalue = substr($mycode, $spos+1);
$myvalue = $XCart_sumTotal * $myvalue / 100;
} else {
$spos = strpos($mycode, “p”);
if ($spos !== false) {
$myvalue = substr($mycode, $spos+1);
}
}
$myTotal = $XCart_sumTotal - $myvalue;
$_SESSION[‘vouchCode’] = $myvalue;
} else unset($_SESSION[‘vouchCode’]);[/php]

<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2"> <div align="left"></div> <table align="left"> <tr> <td class="mediumPinkHeaders">Voucher Code:</td> <td><input type="text" name="vouchCode" value="<?php echo @$_POST['vouchCode']; ?>" size="32" /></td> <td><input type="submit" value="update" /></td> </tr> </table> </form>

Sponsor our Newsletter | Privacy Policy | Terms of Service