Nasru-Deen Saani Nasru-Deen Saani - 5 months ago 14
PHP Question

my php codes aint connecting to mysql database table.what is wrong with my codes?

i want my php file with name getData.php to get data from mysql database table and its not working?
Please help me.What is wrong with my code?

$mysql_host='localhost';
$mysql_user='username';
$mysql_password='password';

@mysqli_connect($mysql_host,$mysql_user,$mysql_password);
@mysqli_select_db("records");

if(!@mysqli_connect($mysql_host,$mysql_user,$mysql_password) )
{
die("CanNot connect to database");
}
else
{
if(!@mysqli_select_db("student"))
{
echo 'connection successful';
}
else{
die ("CanNot connect to database");
}
}
echo "<br />";
$query="SELECT * FROM `info`";
if(@$is_query_run=mysql_query($query))
{
echo"query executed";
while($query_execute=mysql_fetch_assoc($is_query_run))
{
echo $query_execute["Name"];
}
}
else
{
echo "query not executed";
}

?>


the name of my database is $records and the target table is named $info the codes connects to the database alright but fails to connect to the table name $info in the $student database.what is wrong with my codes?

Answer

small mistake interchange die and echo in database selection.

$mysql_host='localhost';
$mysql_user='username';
$mysql_password='password';
$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password);


 if(mysqli_connect_errno())
 {
   die("CanNot connect to database");  
 }
 else
 {
    if(!(mysqli_select_db($link, "student")))
    {
        die('cannot connect to the database');
    }
    else{
         echo "connected"; 
     }
  }
 echo "<br />";
 $query="SELECT * FROM `info`";
 if($is_query_run=mysql_query($link,$query))
 {
    echo"query executed";
    while($query_execute=mysql_fetch_assoc($is_query_run))
    {
       echo $query_execute["Name"];
     }
  }
  else
  {
     echo "query not executed";
  }