-1

How can I make this query or script work properly with having these deprecated errors I am getting when using Wamp I am not getting the errors while using Xampp though. The errors or warning i'm getting is on this pic enter image description here

this is my php script

<?php
$con = mysql_connect("localhost","root","");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("db", $con);

$result = mysql_query("SELECT gender as gender_occupation, COUNT(*) as total FROM hostel_blocks GROUP BY gender");

$rows = array();
while($r = mysql_fetch_array($result)) {
    $row[0] = $r[0];
    $row[1] = $r[1];
    array_push($rows,$row);
}

print json_encode($rows, JSON_NUMERIC_CHECK);

mysql_close($con);
?> 

when i changed some connection code i got a blank page.

lokusking
  • 7,396
  • 13
  • 38
  • 57
benebake
  • 3
  • 5

1 Answers1

0

PHP mysql was deprecated in PHP 5.5 and removed starting PHP 7 You should move to mysqli instead.

Your code using mysqli would look like this:

<?php
    $con = mysqli_connect("localhost","root","");

    if (!$con) {
      die('Could not connect: ' . mysqli_error($con));
    }

    mysqli_select_db( $con, "db");

    $result = mysqli_query($con, "SELECT gender as gender_occupation, COUNT(*) as total FROM hostel_blocks GROUP BY gender");

    $rows = array();
    while($r = mysqli_fetch_array($result)) {
        $row[0] = $r[0];
        $row[1] = $r[1];
        array_push($rows,$row);
    }

    print json_encode($rows, JSON_NUMERIC_CHECK);

    mysqli_close($con);
    ?> 
Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38