Jump to content

select statement


Pawan_Agarwal

Recommended Posts

Hello, 

 

I want to select all records in php code with mysql database. I have a table called "Customer", it has 2 fields "Name", "Number"

How to select all records ?

 

If I write 

select * from customer;

It does not return me anything

 

 

 

If I write 

select name,number from customer;

It does return all records. 

 

Can some one tell me what to do here.............. :happy-04:

 

Link to comment
https://forums.phpfreaks.com/topic/279120-select-statement/
Share on other sites

<?php

$username = "*******";

$password = "*******";

$hostname = "*******";

$database = "*******";;

 

$conn = mysql_connect($hostname, $username, $password, $database) or die("Could not connect: ".mysql_error());

 

if ($conn) {

echo ("Connection is success<br>");

} else {

echo ("Connection is failed");

}

 

$selected = mysql_select_db("*****",$conn) or die("Could not select database");

 

if ($selected) {

echo ("Connection is success to database<br><br>");

} else {

echo ("Connection is failed");

}

 

$result = mysql_query("SELECT * from student");

 

//fetch tha data from the database

while ($row = mysql_fetch_array($result)) {

   echo "Name: " . $row{'name'} . ":::::Class:" .$row{'class'}. ":::::Number:" .$row{'number'} . "<br>";

}

 

?>

----------------------------------------------------------------------------------------------------------------------------------------------------------------

After executing the code in PHP, I get no records display on my php page, I hope you are able to understand that the code I have written over here, Thanks for replying...........
Link to comment
https://forums.phpfreaks.com/topic/279120-select-statement/#findComment-1435995
Share on other sites

echo "Name: " . $row{'name'} . ":::::Class:" .$row{'class'}. ":::::Number:" .$row{'number'} . "<br>";

Please use


tags when posting your code. Also I moved your topic as you posted in the MS SQL server forum, which is not what you are using.

 

The way you access an array element is using square-brackets ([]), not curly's ({}) so the above line should be:

   echo "Name: " . $row['name'] . ":::::Class:" .$row['class']. ":::::Number:" .$row['number'] . "<br>";
Lastly, add error checking to your query so you can see if it is failing or not:

$result = mysql_query("SELECT * from student");
if (!$result){
   die('Could not run query: '.mysql_error());
}
Link to comment
https://forums.phpfreaks.com/topic/279120-select-statement/#findComment-1436075
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.