Edited by project168, 03 April 2011 - 07:33 AM.
How to use PHP to read/fetch the database's first attribute/entry?
Started by project168, Mar 27 2011 10:09 AM
11 replies to this topic
#1
Posted 27 March 2011 - 10:09 AM
Is there a program which provides this compiling method?
|
|
|
#2
Posted 27 March 2011 - 11:45 AM
first, you need to connect wit the database using PHP: mysql_connect - Manual
then you need to select database with PHP: mysql_select_db - Manual
now, you're setup to run the query with PHP: mysql_query - Manual
after that, you fetch the result using PHP: mysql_fetch_array - Manual
so you have the first row in an array.
Thats the basics of accessing an MySQL from php.
then you need to select database with PHP: mysql_select_db - Manual
now, you're setup to run the query with PHP: mysql_query - Manual
after that, you fetch the result using PHP: mysql_fetch_array - Manual
so you have the first row in an array.
Thats the basics of accessing an MySQL from php.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#3
Posted 27 March 2011 - 12:20 PM
Example that uses mysql_fetch_row() instead of fecht_array()
Good luck with it.
$mysql_host = "localhost"; $mysql_database = "database"; $mysql_user = "user"; $mysql_password = "password"; $connection = mysql_connect($mysql_host, $mysql_user, $mysql_password); mysql_select_db($mysql_database); $sql = "SELECT COST_NO FROM CUSTOMER WHERE COST_NO = '1'"; $result = mysql_query($sql, $connection); $row = mysql_fetch_row($result); //print result echo $row[0]; // by index, echo $row['COST_NO']; // or by name
Good luck with it.
#4
Posted 27 March 2011 - 12:49 PM
Is there a program which provides this compiling method?
Edited by project168, 03 April 2011 - 07:33 AM.
#5
Posted 27 March 2011 - 12:55 PM
If x is the COST_NO the just run the code I posted, put your code after it and replace x in your code with $row['COST_NO']
#6
Posted 27 March 2011 - 01:14 PM
Upstream said:
If x is the COST_NO the just run the code I posted, put your code after it and replace x in your code with $row['COST_NO']
#7
Posted 27 March 2011 - 01:30 PM
echo is used to print to screen (browser) and used in this example to print the output of the query. Maybe you should look at the basics of variables, arrays and echo to sort this one out?
#8
Posted 28 March 2011 - 06:40 PM
I tried running the coding but it failed to run.
Can you show me how it is done exactly?
Can you show me how it is done exactly?
#9
Posted 29 March 2011 - 06:22 AM
project168 said:
I tried running the coding but it failed to run.
Can you show me how it is done exactly?
Can you show me how it is done exactly?
I checked the code for the database access and this works properly. Of course you need to replace the connetion string for mysql with your host, database etc. Did you do this? Did you try to change the query string in $sql to produce any result printed to screen? Use only $row[0]; for printing, this should always produce output if your qeury returns anything. Otherwise post your complete code so I can take a look at it.
Gr...
#10
Posted 29 March 2011 - 06:49 AM
Is there a program which provides this compiling method?
Edited by project168, 03 April 2011 - 07:33 AM.
#11
Posted 29 March 2011 - 08:46 AM
You are using mysql? Otherwise I expect it won't work. If you are using mysql:
config.php -> does it containt the connection? like $connection = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if your connection and query are ok then:
echo $row[0];
will print the contents of the first field of the first row from your result.
$row[0]; is this field.
If no result is printed tweak your sql or connection. Try printing $connection after mysql_select_db($mysql_database); like:
echo $connection;
It should print a resource id and if not their is no connection.
It looks ok. Maybe this will help, change:
$sql = "SELECT COST_NO FROM CUSTOMER WHERE COST_NO = 1";
and
fwrite($fp,$row[0]); //WRITE TO THE CUSTOMER # BY REPLACING X
I don't know if your code is valid but the rest is and should work if your query is ok.
config.php -> does it containt the connection? like $connection = mysql_connect($mysql_host, $mysql_user, $mysql_password);
if your connection and query are ok then:
echo $row[0];
will print the contents of the first field of the first row from your result.
$row[0]; is this field.
If no result is printed tweak your sql or connection. Try printing $connection after mysql_select_db($mysql_database); like:
echo $connection;
It should print a resource id and if not their is no connection.
It looks ok. Maybe this will help, change:
$sql = "SELECT COST_NO FROM CUSTOMER WHERE COST_NO = 1";
and
fwrite($fp,$row[0]); //WRITE TO THE CUSTOMER # BY REPLACING X
I don't know if your code is valid but the rest is and should work if your query is ok.
#12
Posted 29 March 2011 - 05:52 PM
Is there a program which provides this compiling method?
Edited by project168, 03 April 2011 - 07:33 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









