Page 2 of 2
Posted: Wed Jun 26, 2002 7:36 pm
by Mahmoud
this code is full of mistakes
IS the companycode a string ex. CO123 or an integer ex. 12 ?
replace all from mysql_query with the following:
Code: Select all
$query = 'SELECT CompanyName, PR_Contact FROM recaps WHERE CompanyCode = "'.$CC.'"';
$result = @mysql_query($query);
while($row = mysql_fetch_array($result)) {
$Company = $rowї"CompanyName"];
$Payroll_Contact = $rowї"PR_Contact"];
echo 'Company Code '.$CC.' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n";
}
Re: parse error
Posted: Wed Jun 26, 2002 11:32 pm
by kaizix
offsite wrote:Parse error: parse error in /www/dougmillard.com/web/lanteam/lookup.php on line 17
line 17= while($row = mysql_fetch_array{
missing a ) line 17 should be
Code: Select all
while($row = mysql_fetch_array){
Posted: Thu Jun 27, 2002 2:17 am
by twigletmac
Basically you never finished typing your while conditions and made a bit of a mess with the query, try changing,
Code: Select all
mysql_query("SELECT * FROM recaps WHERE CompanyCode = $CC)", $Connect);
//$query = "SELECT * FROM recaps WHERE CompanyCode = $CC"
//$result = mysql_query($query, $connect)
// or die("query was not successful);
while($row = mysql_fetch_array{
to
Code: Select all
$sql = "SELECT * FROM recaps WHERE CompanyCode = '$CC'";
@$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
Mac
Oops didn't notice there was a second page, oh well...
Posted: Thu Jun 27, 2002 4:39 am
by Mahmoud
I already fixed it to him .. He messaged me and told me my code is working
Code: Select all
$query = 'SELECT CompanyName, PR_Contact FROM recaps WHERE CompanyCode = "'.$CC.'"';
$result = @mysql_query($query);
while($row = mysql_fetch_array($result)) {
$Company = $rowї"CompanyName"];
$Payroll_Contact = $rowї"PR_Contact"];
echo 'Company Code '.$CC.' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n";
}
Posted: Thu Jun 27, 2002 4:43 am
by twigletmac
Didn't notice the second page and said that. Just felt in some ways it was slightly clearer so didn't remove my post. No offense intended and I didn't say that there was anything wrong with your code did I? The only thing I would say is that there should be an or die statement after the mysql_query() since it would make it easier to debug if something goes wrong with the query.
EDIT-
Oh and I thought that someone might get confused by kaizix's post as he had mysql_fetch_array with no ($result) which would have caused an error.
EDIT AGAIN -
Keep thinking of things - you should put SQL statements into double quotes so that you can use single quotes within them and keep the database happier.
Mac
Posted: Thu Jun 27, 2002 4:51 am
by twigletmac
Based on what I said above, your code would be slightly better as:
Code: Select all
$query = "SELECT CompanyName, PR_Contact FROM recaps WHERE CompanyCode = '$CC'";
$result = @mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$Company = $rowї'CompanyName'];
$Payroll_Contact = $rowї'PR_Contact'];
echo 'Company Code '.$CC.' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n";
}
Mac
Posted: Thu Jun 27, 2002 5:22 am
by Mahmoud
twigletmac wrote:
Keep thinking of things - you should put SQL statements into double quotes so that you can use single quotes within them and keep the database happier.
I think its better to get the variables outside the quotes even if they are double.
in such a query I would better use $_POST['CC'] or $HTTP_POST_VARS['CC'].
So since I will get the $_POST outside the quotes in the two cases, so using single quotes will allow me to use double quotes inside or using double to have single quotes inside are the same, but single is better.
Code: Select all
$query = 'SELECT CompanyName, PR_Contact FROM recaps WHERE CompanyCode = "'.$_POSTї'CC'].'"';
$result = @mysql_query($query);
while($row = mysql_fetch_array($result)) {
$Company = $rowї"CompanyName"];
$Payroll_Contact = $rowї"PR_Contact"];
echo 'Company Code '.$_POSTї'CC'].' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n";
}
Posted: Thu Jun 27, 2002 5:43 am
by twigletmac
In that case do
Code: Select all
$query = "SELECT CompanyName, PR_Contact FROM recaps WHERE CompanyCode = '".$_POSTї'CC']."'";
In this case using single or double quotes in your PHP code will make no difference whatsoever. Ok so it's personal preference, I think that SQL statements look much nicer enclosed in double quotes and since some databases choke on double quotes as delimeters I prefer to use single quotes.
Basically there's not a lot of difference between the your way and my way except some databases don't like double quotes as delimeters. (Oh and I like one and you like the other

)
Mac
Posted: Thu Jun 27, 2002 8:00 am
by Mahmoud
Oh.. I did not know that some databases choke on double quotes as delimeters, I will start using single quotes from now. I only started learning PHP and mySQL from few weeks.
Posted: Thu Jun 27, 2002 10:42 am
by kaizix
my mistake on my last post....i saw the missing ) and that's all i saw so i didn't even think about the result pointer. perhaps i tried to reply too wuickly and i'll work on that.