Need Programming Assignment Help?

Get Help with Programming Assignment in Java, C/C++, Dot Net, PHP, Database and UML Diagrams

MySQL Select Associative Array

MySQL Select Associative Array

MySQL Select Associative Array- This will fetch a result row as a numeric and an Associative Array from MySQL result handle.

<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) { 
 echo "Unable to connect to DB: " . mysql_error(); 
 exit; 
 }

if (!mysql_select_db("mydbname")) { 
 echo "Unable to select mydbname: " . mysql_error(); 
 exit; 
 }

$sql = "SELECT id as userid, fullname, userstatus 
 FROM sometable 
 WHERE userstatus = 1";

$result = mysql_query($sql);

if (!$result) { 
 echo "Could not successfully run query ($sql) from DB: " . mysql_error(); 
 exit; 
 } 
 
 if (mysql_num_rows($result) == 0) { 
 echo "No rows found, nothing to print so am exiting"; 
 exit; 
 }


 while ($row = mysql_fetch_assoc($result)) { 
 echo $row["userid"]; 
 echo $row["fullname"]; 
 echo $row["userstatus"]; 
 }

mysql_free_result($result);

?>