Home >Backend Development >PHP Tutorial >php reads data from mysql

php reads data from mysql

巴扎黑
巴扎黑Original
2016-11-23 10:50:56940browse

<?php
$dbhost = &#39;localhost:3306&#39;;
$dbuser = &#39;root&#39;;
$dbpass = &#39;root&#39;;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
 die(&#39;Could not connect to mysql&#39;);
}
echo "connected successfully!";
$sql = &#39;SELECT emp_id, emp_name, emp_salary FROM employee&#39;;
mysql_select_db(&#39;test_db&#39;);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
 die(&#39;Could not get data: &#39;);
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "EMP ID :{$row[&#39;emp_id&#39;]}  <br> ".
"EMP NAME : {$row[&#39;emp_name&#39;]} <br> ".
"EMP SALARY : {$row[&#39;emp_salary&#39;]} <br> ".
"--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php global spaceNext article:php global space