Vir Vir - 4 months ago 15
MySQL Question

Please rewrite this code using mysql functions

I am making an enhancement on our clients old website by adding a video gallery. I would like to write a mysql_connect query instead of mysqli query. because right now, when I tested below mysqli code in my wamp server its works fine and shows Database result on .php page. when i upload .php page to server from cpane it displays error "Fatal error: Class 'mysqli' not found in......" right now, we are not able to update our php/mysql (servers) both in cpanel because of huge database dependency (all configuration code written using mysql_connect). we using something version 4.6. I know its not a good way but I have no option right now. Could you please help me out in this regard?

Configuration Code :

<?php
class imageGallery
{
private $sql, $query;
function __construct()
{
/* DB Connection */
$this->sql = new mysqli('localhost', 'root', '', 'mediagalleryfinaltest');
}
/*
|--------------------------------------------------------------------------
| Get Gallery
|--------------------------------------------------------------------------
*/
function getGalley()
{
if ($query = $this->sql->query("SELECT * FROM `media` ORDER BY media_id ASC ")) {
$rows = array();
while ($row = $query->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
} else {
return "Error";
}
}
}

/* Create Object */
$obj = new imageGallery();
?>


Video Selection Code :

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<hr>
</div>

<!-- Photo Gallery Starts -->
<div class="row gallery">
<?php
$result = $obj->getGalley();
if (!empty($result)) {
foreach ($result AS $row) {
/* IF is Video */
if ($row['media_type'] == 'YouTube')
{
$rel = 'video';
$href = "http://www.youtube.com/embed/" . $row['media_name'];
$src_bg = 'http://img.youtube.com/vi/' . $row['media_name'] . '/mqdefault.jpg';
$src = 'http://img.youtube.com/vi/' . $row['media_name'] . '/mqdefault.jpg';
} else {
/* IF is Image */
$rel = '';
$href = "images/large/" . $row['media_name'];
$src_bg = "images/small/" . $row['media_name'];
$src = "images/small/" . $row['media_name'];
}
?>
<div class="col-xs-6 col-md-3 col-lg-2 col-sm-4 gallery-block"
data-media_guid="<?php echo $row['media_GUID']; ?>">

<div class="gallery-block-inner">
<a class="photobox_a <?php if ($rel != '') { ?> media-video<?php } ?>"
href="<?php echo $href; ?>" rel="<?php echo $rel; ?>" <?php if ($rel != '') { ?>
style="background-image:url('images/placeholder.png'), url('<?php
echo $src_bg; ?>');"<?php } ?>>

<img src="<?php echo $src; ?>" class="img-responsive">
</a>
</div>
<span class="media-name"><?php echo $row['media_name_original']; ?></span>
</div>
<?php
}
}
else
{
?>
<p id="gallery-empty">It's Empty</p>
<?php
}
?>

</div>
<!-- Photo Gallery Ends -->
<br>
</div>
</body>
</html>

Answer

For the class you have shown, switching to mysql_* would look like this.

I changed the errorhandling a little, so it will give you a message if it fails to connect, will throw an error when the query is false etc., ask if you want to know something about the specific changes

You should really tell your customer to upgrade his php-version. Maybe tell him, adding new features takes a lot of additional time because the language has developed a lot and you are not used to this old stuff.

class imageGallery {

    private $sql;

    function __construct() {
        $this->sql = mysql_connect('localhost', 'root', '');
        if(!$this->sql){
            trigger_error(mysql_error());
        }
        $db_select = mysql_select_db('mediagalleryfinaltest');
        if(!$db_select){
            trigger_error(mysql_error());
        }
    }

    /**
     * @return array|string
     */
    function getGalley() {
        if ($query = mysql_query("SELECT * FROM `media` ORDER BY media_id ASC ", $this->sql)) {
            $rows = array();
            while ($row = mysql_fetch_assoc($query)) {
                $rows[] = $row;
            }
            return $rows;
        } else {
            trigger_error(mysql_error());
        }
    }

}