query msaccess database with php and update record

crescue
crescue used Ask the Experts™
on
Hi, I am querying an MsAccess database with PHP
My code attached is working as specified, but I want to create a variable with the usuario (in this case "crescue")
If I replace "crescue" with the variable THEN IT DOESN'T WORK
Ex.
$abc = "crescue"
WHERE usuario = $abc
Why it does not work with the variable abc$

Also, how can I update the record once it is retrieved.  I would like to update the DIREC field for example.
A sample of how to do it with the code provided would be greatly appreciated.
Thanx again
<?php 
$db = 'C:\\Inetpub\\wwwroot\\locos\\DB\\bodega.mdb'; 
$conn = new COM("ADODB.Connection"); 
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db");
$abc = "crescue"
$sql = 'SELECT   usuario, nombre, contrasena, direc
      FROM     users WHERE usuario = "crescue" --> WORKS
      FROM     users WHERE usuario = $abc  --> DOESN'T       
      ORDER BY usuario'; 
$rs = $conn->Execute($sql); 
?> 
  
<table> 
<tr> 
      <th>Account #</th> 
      <th>Account Type</th> 
      <th>First Name</th> 
</tr> 
<?php while (!$rs->EOF) { ?> 
      <tr> 
            <td><?php echo $rs->Fields['usuario']->Value ?></td> 
            <td><?php echo $rs->Fields['nombre']->Value ?></td> 
            <td><?php echo $rs->Fields['contrasena']->Value ?></td> 
      </tr> 
      <?php $rs->MoveNext() ?> 
<?php } ?> 
</table> 
  
<?php 
  
$rs->Close(); 
$conn->Close(); 
  
$rs = null; 
$conn = null; 
  
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
WHERE usuario = '$abc'
Most Valuable Expert 2011
Top Expert 2016

Commented:
You might want to use the single and double quotes this way, with double quotes around the entire select string (to permit PHP replacement of the $vars) and single quotes around the escaped vars inside the string.

Best, ~Ray
$sql = "SELECT   usuario, nombre, contrasena, direc
        FROM     users WHERE usuario = '$abc'     
        ORDER BY usuario"; 
$rs = $conn->Execute($sql);

Open in new window

Author

Commented:
Great, how about to update the record ?
I would like to add data to direc, for example.

Thanx
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Most Valuable Expert 2011
Top Expert 2016

Commented:
What rows would you want to update?  That would be part of the WHERE clause.
$sql = "UPDATE users SET direc = '??' WHERE ...";

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
that would be like:
$sql = "UPDATE users SET direc = direc & 'abc'
        WHERE usuario = '$abc'     
        "; 
$conn->Execute($sql);

Open in new window

Author

Commented:
I followed ur directions, and the query doesn't work :(  What could be wrong ?
Attaching my code (the database is working and it has the USUARIO as CRESCUE

On the update, can I update more than 1 field, lets say DIREC and CONTRASENA

Thanx

$db = 'C:\\Inetpub\\wwwroot\\locos\\DB\\bodega.mdb'; 
$conn = new COM("ADODB.Connection"); 
$conn->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$db");
$abc = "crescue"
$sql = "SELECT   usuario, nombre, contrasena
      FROM     users WHERE usuario = '$abc'
      ORDER BY usuario"; 
$rs = $conn->Execute($sql); 

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
"the query doesn't work" is like saying "i feel sad" - we would love to help, but there is not very much information there.  Please tell us the exact symptoms in as much detail as possible.  Use copy / paste to ensure that the symptoms are delivered accurately.  Error message? Incorrect output? Syntax error?

Thanks, ~Ray

Author

Commented:
Je Je je, u r right Ray,
I attached the code in my previous message.
When I open the browser, it just returns a blank screen when using the $ABC, but if use the name directly, then it displays the record

Thanx
Most Valuable Expert 2011
Top Expert 2016

Commented:
Pleaes print out the query string (var $sql) after it is created and before $s = $conn->Execute and then print out the value of $rs after the execution.  Use var_dump() to print these values.  Thanks, ~Ray

Author

Commented:
It appears that the problem is by declaring the $abc = "crescue"
declared just before the SQL
Because if I remove it, then IT WORKS (but I have to declare 'crescue' in the SQL query)

Where or how should I declare the variable ?
Most Valuable Expert 2011
Top Expert 2016
Commented:
Interesting.  Looks like you got a parse error because the line with $abc = "crescue" did not have the ending semi-colon.

Where is this hosted?  You need to get the PHP errors like that to come out to the browser.  If you do not it will be nearly impossible to do development on this site.
// BAD
$abc = "crescue"

// GOOD
$abc = "crescue";

Open in new window

Author

Commented:
Thanx for your help !!! I'm learning a lot from all the experts.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points! ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial