0

When i use this code. It says:

Database access failed: no database selected"

And it also gives the:

Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

error. The following is the code i have used.

$con = mysqli_connect("localhost", "root", "","radian")
    or die ("Couldn't connect to  mySQL");  


$query = "SELECT Staff_id,Fname,Sname,Gender,username FROM staff  ";

$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)


{
    $row = mysql_fetch_array($result);
    $staff_id = $row['Staff_id'];
    $Fname = $row['Fname'];
    $Sname = $row['Sname'];
    $Gender = $row['Gender'];
    $username = $row['username'];

    echo '<tr> <td>'.$staff_id.'</td> <td>'.$Fname.'</td> <td>'.$Sname.'</td> <td>'.$Gender.'</td> <td>'.$username.'</td> </tr>';
}

It says the error is from this line:

$result = mysql_query($query);
Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
  • you are using mysql_query to run query but making database connect using mysqli_connect. mysql and mysqli are both diffrent. – Ranjeet Singh Apr 13 '16 at 09:56

3 Answers3

0

You are using mysql in place of mysqli, and mysqli needs connection with the query.

Just Replace you code with this one:

$con = mysqli_connect("localhost", "root", "","radian")
    or die ("Couldn't connect to  mySQL");  


$query = "SELECT Staff_id,Fname,Sname,Gender,username FROM staff  ";

$result = mysqli_query($con, $query);
if (!$result) die ("Database access failed: " . mysqli_error($con));
$rows = mysqli_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)


{
    $row = mysqli_fetch_array($result);
    $staff_id = $row['Staff_id'];
    $Fname = $row['Fname'];
    $Sname = $row['Sname'];
    $Gender = $row['Gender'];
    $username = $row['username'];

    echo '<tr> <td>'.$staff_id.'</td> <td>'.$Fname.'</td> <td>'.$Sname.'</td> <td>'.$Gender.'</td> <td>'.$username.'</td> </tr>';
}
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
0

You mixed mysql / mysqli.

$con = mysqli_connect("localhost", "root", "","radian");
if(!$con)
{
  exit("Couldn't connect: ".mysqli_connect_error());
}
mysqli_set_charset($con, "utf8");

echo "<table>";
echo "<tbody>";
$query = "SELECT Staff_id,Fname,Sname,Gender,username FROM staff";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result))
{
    $staff_id = $row['Staff_id'];
    $Fname = $row['Fname'];
    $Sname = $row['Sname'];
    $Gender = $row['Gender'];
    $username = $row['username'];
    echo '<tr><td>'.$staff_id.'</td><td>'.$Fname.'</td><td>'.$Sname.'</td><td>'.$Gender.'</td><td>'.$username.'</td></tr>';
}
echo "</tbody>";
echo "</table>";
deadsway
  • 11
  • 2
0

You are building your db connection with mysqli

$con = mysqli_connect("localhost", "root", "","radian")
or die ("Couldn't connect to  mySQL");

While in your query you are using mysql you need to use mysqli in your query instead of using mysql

$query = "SELECT Staff_id,Fname,Sname,Gender,username FROM staff  ";

 $result = mysqli_query($query);
 if (!$result) die ("Database access failed: " . mysql_error());
  $rows = mysqli_num_rows($result);
 for ($j = 0 ; $j < $rows ; ++$j)
 {
$row = mysqli_fetch_array($result);
$staff_id = $row['Staff_id'];
$Fname = $row['Fname'];
$Sname = $row['Sname'];
$Gender = $row['Gender'];
$username = $row['username'];

echo '<tr> <td>'.$staff_id.'</td> <td>'.$Fname.'</td> <td>'.$Sname.'</td>    <td>'.$Gender.'</td> <td>'.$username.'</td> </tr>';
}
Zedd Index
  • 344
  • 2
  • 11