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; ?>
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);
Great, how about to update the record ?
I would like to add data to direc, for example.
Thanx
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.
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);
"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?
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
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
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)
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.
IT issues often require a personalized solution. With Ask the Experts™, submit your questions to our certified professionals and receive unlimited, customized solutions that work for you.
Premium Content
You need an Expert Office subscription to comment.Start Free Trial