0

Dear I have following codes

<?php
// Connection variables
$host="localhost";
$username="root";
$password="";
$db_name="asia"; 

// Connect to database
$con=mysqli_connect("$host", "$username", "$password");

// Connect result
if(!$con){
die('Error Connecting to Database: ' . mysqli_error());
//die '<script type="text/javascript">alert("Error Connecting to Database"). mysqli_error($con);</script>'; 
}
else
{
echo '<script type="text/javascript">alert("Connected"). mysqli_error($con);</script>'; 
}

// Database Selection
$sel =mysqli_select_db($con,"$db_name");

// Database Selection result
if(!$sel){
die('Error selecting Database: ' . mysqli_error());
}
else
{
//echo "Database Selected" . mysqli_error($con);
echo '<script type="text/javascript">alert("Database Selected"). mysqli_error($con);</script>'; 
}
$result = mysql_query("select sno,packing,weight from ghee");  
 if(!$result){
  echo 'select query error or data not found';
  }
  else
  {
  echo 'data found';

echo "<table border='1' style='border-collapse: collapse'>";  
echo "<th>SNo</th><th>Name</th><th>Weight</th>";  
while ($row = mysql_fetch_array($result))
{  
echo"<tr><td>".$row['sno']."</td><td>".$row['packing']."</td><td>".$row['weight']."</td></tr><br>";
echo "</table><br>";
}
}
mysqli_close($con);  

?>

on this line number 35
echo 'select query error or data not found';

it says:select query error or data not found

connection with mysql is ok
and
Database is selected properly

but why I get above error message

Here is screen shot of database

[IMG]http://i41.tinypic.com/2w5lx8y.jpg[/IMG]

Please help me to get rid of error message

3
Contributors
2
Replies
16
Views
3 Years
Discussion Span
Last Post by veedeoo
0

Hi,

You can rewrite it to something like this..

<?php
    // Connection variables
    $host="localhost";
    $username="root";
    $password="";
    $db_name="asia"; 

    // Connect to database
    $con= new mysqli( $host, $username, $password); // you can add the fourth parameter here as you wish.

    // Connect result

    if($con->connect_errno){
    die('Error Connecting to Database: ' . mysqli_error());
    //die '<script type="text/javascript">alert("Error Connecting to Database"). mysqli_error($con);</script>';
    ## must use this
    // exit;
    }

    else
    {
    echo '<script type="text/javascript">alert("Connected"). mysqli_error($con);</script>';
    }
    // Database Selection
    //$sel = mysqli_select_db($con,"$db_name");
    // Database Selection result
    if(!$con->select_db($db_name)){
    die('Error selecting Database: ' . mysqli_error());
    }
    else
    {
    //echo "Database Selected" . mysqli_error($con);
    echo '<script type="text/javascript">alert("Database Selected"). mysqli_error($con);</script>';
    }
    $result = $con->query("select sno,packing,weight from ghee");  
    if(!$result){
    echo 'select query error or data not found';
    }
    else
    {
    echo 'data found';
    echo "<table border='1' style='border-collapse: collapse'>";
    echo "<th>SNo</th><th>Name</th><th>Weight</th>";
    while ($row = $result->fetch_array(MYSQLI_ASSOC))
    {
    echo"<tr><td>".$row['sno']."</td><td>".$row['packing']."</td><td>".$row['weight']."</td></tr><br>";
echo "</table><br>";
    }
    }
    mysqli_close($con);
    ?>

You can also shorten your codes by adding a fourth parameter on the mysqli object $db_name. Instead of selecting the database table midway on your codes. You can always redefine a new database table as you wish after the mysqli object.

Edited by veedeoo: info added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.