Simple PHP script to load data to mysql database.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post by Mahmoud »

this code is full of mistakes 8O

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"; 
&#125;
kaizix
Forum Commoner
Posts: 50
Joined: Tue Jun 18, 2002 9:16 pm
Location: california
Contact:

Re: parse error

Post 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)&#123;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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&#123;
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)) &#123;
Mac

Oops didn't notice there was a second page, oh well...
Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post 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)) &#123; 
$Company = $row&#1111;"CompanyName"]; 
$Payroll_Contact = $row&#1111;"PR_Contact"]; 
echo 'Company Code '.$CC.' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n"; 
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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)) &#123; 
    $Company = $row&#1111;'CompanyName']; 
    $Payroll_Contact = $row&#1111;'PR_Contact']; 
    echo 'Company Code '.$CC.' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n"; 
&#125;
Mac
Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post 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&#1111;'CC'].'"'; 
$result = @mysql_query($query); 
while($row = mysql_fetch_array($result)) &#123; 
$Company = $row&#1111;"CompanyName"]; 
$Payroll_Contact = $row&#1111;"PR_Contact"]; 
echo 'Company Code '.$_POST&#1111;'CC'].' belongs to the Company '.$company.' run by '.$Payroll_Contact."<br>\n"; 
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

In that case do

Code: Select all

$query = "SELECT CompanyName, PR_Contact FROM recaps WHERE CompanyCode = '".$_POST&#1111;'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
Mahmoud
Forum Newbie
Posts: 13
Joined: Tue Jun 25, 2002 5:21 pm

Post 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.
kaizix
Forum Commoner
Posts: 50
Joined: Tue Jun 18, 2002 9:16 pm
Location: california
Contact:

Post 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.
Post Reply