May 17, 2010

output all data from db to a php page

Question by Sarit

I’m a real beginner with PHP & mysql.
Just for studying and for a simple example at school I would like to work this simple query and possibly output all the rows (or maybe even one) to a very basic output on a php page:

<?php
$user= "root";
$host="localhost";
$password="";
$database = "PetCatalog";
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn’t connect to server");
$query = "SELECT * FROM Pet";
$result = mysql_query($cxn,$query) or die ("Couldn’t execute query.");
$row = mysqli_fetch_assoc($result);
echo "$result";
?>

this is the error message i’ve been getting:

Warning: mysql_query() expects parameter 1 to be string, object given in C:xampplitehtdocsmyblogquery.php on line 18
Couldn’t execute query.

What should I do?
Thanks!

Answer by Starx

<?php
$user= "root";
$host="localhost";
$password="";
$database = "PetCatalog";
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn’t connect to server");
$query = "SELECT * FROM Pet";
$result = mysql_query($query) or die ("Couldn’t execute query.");
while($row = mysqli_fetch_array($result)) {
          print_r($row);
}
?>

This should solve your problem

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!