Jump to content

How to use PHP to read/fetch the database's first attribute/entry?

- - - - -

  • Please log in to reply
11 replies to this topic

#1
project168

project168

    Newbie

  • Members
  • PipPip
  • 14 posts
Is there a program which provides this compiling method?

Edited by project168, 03 April 2011 - 07:33 AM.


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
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.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Example that uses mysql_fetch_row() instead of fecht_array()


  $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
project168

project168

    Newbie

  • Members
  • PipPip
  • 14 posts
Is there a program which provides this compiling method?

Edited by project168, 03 April 2011 - 07:33 AM.


#5
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
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
project168

project168

    Newbie

  • Members
  • PipPip
  • 14 posts

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']
Noob question: do I put echo along with $row['COST_NO']? Sorry I am not proficient with using PHP. After replacing that, do I remove the other two echo statements out assuming its already been known where to be placed?

#7
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
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
project168

project168

    Newbie

  • Members
  • PipPip
  • 14 posts
I tried running the coding but it failed to run.

Can you show me how it is done exactly?

#9
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts

project168 said:

I tried running the coding but it failed to run.

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
project168

project168

    Newbie

  • Members
  • PipPip
  • 14 posts
Is there a program which provides this compiling method?

Edited by project168, 03 April 2011 - 07:33 AM.


#11
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
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.

#12
project168

project168

    Newbie

  • Members
  • PipPip
  • 14 posts
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