I want my PHP coding to read/fetch my database’s first attribute/entry like the first number of the client in that first row and then fetch it to be shown on PHP so that it will show ‘1’. How would I do that?
Here’s the table of my example database (MySQL)
I know the command to select the line via the database is this:
SELECT * FROM CUSTOMER WHERE CUST_NO=1;
What PHP library/extension are you using to access the database?
I am using none, that’s why I was wondering how can I implement only this part.
Ah I see. In which case, for simplicity’s sake, try this [fphp]mysqli[/fphp] based example.
<?php
/*
Increase error reporting, code
properly, or not at all.
*/
error_reporting(-1);
ini_set('display_errors', true);
/*
Helper function; exits script
*/
function stop($message){
echo $message;
exit;
}
/*
Connect to server
*/
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
/*
Check if connection failed
*/
if(false === is_resource($con)){
stop(mysqli_connect_error());
}
/*
Create query
*/
$sql = '
SELECT
CUST_NO
, CUST_FNAME
, CUST_LNAME
, CUST_PHONE
FROM
customer
ORDER BY
CUST_NO ASC
LIMIT 1;';
/*
Execute query
*/
$res = mysqli_query($con, $sql);
/*
Check if query failed
*/
if(false === is_resource($res)){
stop(mysqli_error());
}
/*
Convert result to data
*/
$row = mysqli_fetch_assoc($res);
/*
Display data
*/
foreach($row as $key => $value){
printf("<p>%s = %s</p>\
", $key, $value);
}
You could alternatively, display the data like so…
/*
Display data
*/
echo '<p>CUST_NO = ', $row['CUST_NO'], '</p>', PHP_EOL ;
echo '<p>CUST_FNAME = ', $row['CUST_FNAME'], '</p>', PHP_EOL ;
echo '<p>CUST_LNAME = ', $row['CUST_LNAME'], '</p>', PHP_EOL ;
echo '<p>CUST_PHONE = ', $row['CUST_PHONE'], '</p>', PHP_EOL ;
Additionally, if really did only want to display the fist attribute (yes I know, as originally requested :)), you could do it like this…
/*
Display data
*/
echo current($row);
Hi Anthony,
That coding looks a big complicated to fetch for just one string from the database. Because I am looking for a small instruction which looks into the database and bring the customer number that is being display in the first entry, in that case, it would be ‘1’. So it brings that 1 to the PHP page and it shows it as ‘1’. I am not proficient with PHP/database coding, I was hoping if you can help me out. Thanks.
Hi Anthony, your second provided codings seem to simplify the work.
How would I implement to this coding:
<?PHP
exec(“mode com8: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off”);
$fp =fopen(“com3”, “w”);
fwrite($fp,“x ”); //WRITE TO THE CUSTOMER # BY REPLACING X
fclose($fp);
?>
Basically, fwrite writes the data to my USB device so ‘x’ is my customer # which I want it to fetch from my database. How would I do this?
I’m sorry, but the following is the minimum amount of code required to do that task.
<?php
/*
Increase error reporting, code
properly, or not at all.
*/
error_reporting(-1);
ini_set('display_errors', true);
/*
Helper function; exits script
*/
function stop($message){
echo $message;
exit;
}
/*
Connect to server
*/
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
/*
Check if connection failed
*/
if(false === is_resource($con)){
stop(mysqli_connect_error());
}
/*
Create query
*/
$sql = '
SELECT
CUST_NO
, CUST_FNAME
, CUST_LNAME
, CUST_PHONE
FROM
customer
ORDER BY
CUST_NO ASC
LIMIT 1;';
/*
Execute query
*/
$res = mysqli_query($con, $sql);
/*
Check if query failed
*/
if(false === is_resource($res)){
stop(mysqli_error());
}
/*
Convert result to data
*/
$row = mysqli_fetch_assoc($res);
/*
Display data
*/
echo current($row); #1
Let’s break it down.
You have to connect to the database
You have to execute a query
You have to return that result
You want to display that result
We could remove some of the error checking, but I’d advise against it. Maybe it won’t look so scary when we remove the comments…
<?php
error_reporting(-1);
ini_set('display_errors', true);
function stop($message){
echo $message;
exit;
}
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
if(false === is_resource($con)){
stop(mysqli_connect_error());
}
$sql = '
SELECT
CUST_NO
, CUST_FNAME
, CUST_LNAME
, CUST_PHONE
FROM
customer
ORDER BY
CUST_NO ASC
LIMIT 1;';
$res = mysqli_query($con, $sql);
if(false === is_resource($res)){
stop(mysqli_error());
}
$row = mysqli_fetch_assoc($res);
echo current($row); #1
Maybe not?
<?php
error_reporting(-1);
ini_set('display_errors', true);
function stop($message){
echo $message;
exit;
}
function write_to_usb($data){
exec('mode com8: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off');
$fp = fopen('com3', 'w');
fwrite($fp, $data);
fclose($fp);
}
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
if(false === is_resource($con)){
stop(mysqli_connect_error());
}
$sql = '
SELECT
CUST_NO
, CUST_FNAME
, CUST_LNAME
, CUST_PHONE
FROM
customer
ORDER BY
CUST_NO ASC
LIMIT 1;';
$res = mysqli_query($con, $sql);
if(false === is_resource($res)){
stop(mysqli_error());
}
$row = mysqli_fetch_assoc($res);
write_to_usb($row['CUST_NO']);
How’s that?
AnthonySterling:
<?php
error_reporting(-1);
ini_set('display_errors', true);
function stop($message){
echo $message;
exit;
}
function write_to_usb($data){
exec('mode com8: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off');
$fp = fopen('com3', 'w');
fwrite($fp, $data);
fclose($fp);
}
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
if(false === is_resource($con)){
stop(mysqli_connect_error());
}
$sql = '
SELECT
CUST_NO
, CUST_FNAME
, CUST_LNAME
, CUST_PHONE
FROM
customer
ORDER BY
CUST_NO ASC
LIMIT 1;';
$res = mysqli_query($con, $sql);
if(false === is_resource($res)){
stop(mysqli_error());
}
$row = mysqli_fetch_assoc($res);
write_to_usb($row['CUST_NO']);
How’s that?
Wow, a big thanks to you Anthony. You made the break up of the work sound so simple. I will try out the code right now by executing it and I will let you know if it runs or not. Thanks!
I am testing right now and I have trouble connecting to the database. I am guessing something to do with my database program which is not opening MySQL. I will reply back by tomorrow if it will work. Many thanks!
oddz
March 27, 2011, 8:44pm
12
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
Did you replace those arguments with your actual MySQL authentication info.
oddz:
$con = mysqli_connect('host', 'user', 'pass', 'schema', 3306);
Did you replace those arguments with your actual MySQL authentication info.
Yes I did but I got errors not be able to connect. I think it has something to do with my database program. I have to check it out with another computer to confirm. Will update tomorrow. Thank you all for replying!
updating: the code doesn’t seem to work on php. not being able to write it the usb. is there anyways to fix this?
What USB device are you writing to, and what are you expecting to happen?
I am writing it to my LCD and it show display the CUST_NO on it. I know that this works because I tried:
<?PHP
exec(“mode com8: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off”);
$fp =fopen(“com3”, “w”);
fwrite($fp,“x”); //WRITE TO THE CUSTOMER # BY REPLACING X
fclose($fp);
?>
I just want to fetch the current first cust_no for ‘x’ so that it will show cust_no like the first person the screen.