在cPanel中使用mysqli_fetch_all时,如何解决“此页面无法正常工作”的问题

时间:2019-07-18 19:14:03

标签: php mysqli

当我使用下面提供的代码时,我不断收到“此页面无法正常工作”的信息,我不知道该怎么办,我尝试将其更改为mysqli_fetch_assoc,但这使页面空白,非常感谢您的帮助,如果这还不够清楚,我也表示歉意,我还没有机会阅读stackoverflow示例。

server.php:

<?php
// connect to database
$conn = mysqli_connect('localhost', 'FardMuhammad', '', 'imgupload1');

// lets assume a user is logged in with id $user_id
$user_id = 2;

if (!$conn) {
  die("Error connecting to database: " . mysqli_connect_error($conn));
  exit();
}

// if user clicks like or dislike button
if (isset($_POST['action'])) {
  $post_id = $_POST['post_id'];
  $action = $_POST['action'];
  switch ($action) {
    case 'like':
         $sql="INSERT INTO rating_info (user_id, post_id, rating_action)
               VALUES ($user_id, $post_id, 'like')
               ON DUPLICATE KEY UPDATE rating_action='like'";
         break;
   case 'bike':
        $sql="INSERT INTO rating_info (user_id, post_id, rating_action)
               VALUES ($user_id, $post_id, 'bike')
               ON DUPLICATE KEY UPDATE rating_action='bike'";
   break;
   case 'star':
        $sql="INSERT INTO rating_info (user_id, post_id, rating_action)
               VALUES ($user_id, $post_id, 'star')
               ON DUPLICATE KEY UPDATE rating_action='star'";
   break;
    case 'dislike':
          $sql="INSERT INTO rating_info (user_id, post_id, rating_action)
               VALUES ($user_id, $post_id, 'dislike')
               ON DUPLICATE KEY UPDATE rating_action='dislike'";
         break;
   case 'disbike':
         $sql="INSERT INTO rating_info (user_id, post_id, rating_action)
              VALUES ($user_id, $post_id, 'disbike')
               ON DUPLICATE KEY UPDATE rating_action='disbike'";
    break;
  case 'unstar':
        $sql="INSERT INTO rating_info (user_id, post_id, rating_action)
             VALUES ($user_id, $post_id, 'unstar')
           ON DUPLICATE KEY UPDATE rating_action='unstar'";
   break;
    case 'unlike':
          $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
          break;
    case 'unbike':
          $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
          break;
    case 'unstar':
          $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
          break;
  case 'undisbike':
          $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
    break;
  case 'undislike':
          $sql="DELETE FROM rating_info WHERE user_id=$user_id AND post_id=$post_id";
    break;
    default:
        break;
  }

  // execute query to effect changes in the database ...
  mysqli_query($conn, $sql);
  echo getRating($post_id);
  exit(0);
}

// Get total number of likes for a particular post
function getLikes($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info
          WHERE post_id = $id AND rating_action='like'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

function getStars($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info
          WHERE post_id = $id AND rating_action='star'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

function getBikes($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info
          WHERE post_id = $id AND rating_action='bike'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

// Get total number of dislikes for a particular post
function getDisbikes($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info
          WHERE post_id = $id AND rating_action='disbike'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

function getDisstars($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info
          WHERE post_id = $id AND rating_action='unstar'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}


// Get total number of dislikes for a particular post
function getDislikes($id)
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM rating_info
          WHERE post_id = $id AND rating_action='dislike'";
  $rs = mysqli_query($conn, $sql);
  $result = mysqli_fetch_array($rs);
  return $result[0];
}

// Get total number of likes and dislikes for a particular post
function getRating($id)
{
  global $conn;
  $rating = array();
  $likes_query = "SELECT COUNT(*) FROM rating_info WHERE post_id = $id AND rating_action='like'";
  $bikes_query = "SELECT COUNT(*) FROM rating_info WHERE post_id = $id AND rating_action='bike'";
  $stars_query = "SELECT COUNT(*) FROM rating_info WHERE post_id = $id AND rating_action='star'";
  $dislikes_query = "SELECT COUNT(*) FROM rating_info
                    WHERE post_id = $id AND rating_action='dislike'";
  $disbikes_query = "SELECT COUNT(*) FROM rating_info
        WHERE post_id = $id AND rating_action='disbike'";
  $unstars_query = "SELECT COUNT(*) FROM rating_info
        WHERE post_id = $id AND rating_action='unstar'";
  $likes_rs = mysqli_query($conn, $likes_query);
  $stars_rs = mysqli_query($conn, $stars_query);
  $bikes_rs = mysqli_query($conn, $bikes_query);
  $dislikes_rs = mysqli_query($conn, $dislikes_query);
  $unstars_rs = mysqli_query($conn, $unstars_query);
  $disbikes_rs = mysqli_query($conn, $disbikes_query);
  $likes = mysqli_fetch_array($likes_rs);
  $stars = mysqli_fetch_array($stars_rs);
  $bikes = mysqli_fetch_array($bikes_rs);
  $dislikes = mysqli_fetch_array($dislikes_rs);
  $unstars = mysqli_fetch_array($unstars_rs);
  $disbikes = mysqli_fetch_array($disbikes_rs);
  $rating = [
    'likes' => $likes[0],
    'bikes' => $bikes[0],
    'stars'  => $stars[0],
    'dislikes' => $dislikes[0],
    'disbikes' => $disbikes[0],
    'unstars' => $unstars[0]
  ];
  return json_encode($rating);
}

// Check if user already likes post or not
function userLiked($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id
          AND post_id=$post_id AND rating_action='like'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    return true;
  }else{
    return false;
  }
}

function userBiked($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id
          AND post_id=$post_id AND rating_action='bike'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    return true;
  }else{
    return false;
  }
}

function userStared($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id
          AND post_id=$post_id AND rating_action='star'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    return true;
  }else{
    return false;
  }
}


// Check if user already dislikes post or not
function userDisliked($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id
          AND post_id=$post_id AND rating_action='dislike'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    return true;
  }else{
    return false;
  }
}

// Check if user already dislikes post or not
function userDisbiked($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id
          AND post_id=$post_id AND rating_action='disbike'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    return true;
  }else{
    return false;
  }
}

// Check if user already dislikes post or not
function unStars($post_id)
{
  global $conn;
  global $user_id;
  $sql = "SELECT * FROM rating_info WHERE user_id=$user_id
          AND post_id=$post_id AND rating_action='unstar'";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    return true;
  }else{
    return false;
  }
}



$sql = "SELECT * FROM posts";
$result = mysqli_query($conn, $sql);
// fetch all posts from database
// return them as an associative array called $posts
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);

index.php:

<div class='posts-wrapper'>
   <?php foreach ($posts as $post): ?>
    <div class='post'>
      <div class='post-info'>
        <!-- if user likes post, style button differently -->
        <i <?php if (userLiked($post['id'])): ?>
              class='fa fa-thumbs-up like-btn'
          <?php else: ?>
              class='fa fa-thumbs-o-up like-btn'
          <?php endif ?>
          data-id='<?php echo $post['id'] ?>'><img src='image.ico'></i>
        <span class='likes'><?php echo getLikes($post['id']); ?></span>
        &nbsp;&nbsp;&nbsp;&nbsp;

        <i <?php if (userStared($post['id'])): ?>
            class='fa fa-star star-btn'
          <?php else: ?>
            class='fa fa-star-o star-btn'
          <?php endif ?>
          data-id='<?php echo $post['id']; ?> '><img src='image.ico'></i>
        <span class='stars'><?php echo getStars($post['id']); ?></span>
        &nbsp;&nbsp;&nbsp;&nbsp;
      </div>
   </div>
   <?php endforeach ?>
  </div>
  <script src="scripts.js"></script>

scripts.js:

$(document).ready(function(){
// if the user clicks on the like button ...
$('.like-btn').on('click', function(){
  var post_id = $(this).data('id');
  $clicked_btn = $(this);
  if ($clicked_btn.hasClass('fa-thumbs-o-up')) {
  action = 'like';
  } else if($clicked_btn.hasClass('fa-thumbs-up')){
  action = 'unlike';
  }
  $.ajax({
  url: 'index.php',
  type: 'post',
  data: {
  'action': action,
  'post_id': post_id
  },
  success: function(data){
  res = JSON.parse(data);
  if (action == "like") {
  $clicked_btn.removeClass('fa-thumbs-o-up');
  $clicked_btn.addClass('fa-thumbs-up');
  } else if(action == "unlike") {
  $clicked_btn.removeClass('fa-thumbs-up');
  $clicked_btn.addClass('fa-thumbs-o-up');
  }
  // display the number of likes and dislikes
  $clicked_btn.siblings('span.likes').text(res.likes);
  $clicked_btn.siblings('span.dislikes').text(res.dislikes);

  // change button styling of the other button if user is reacting the second time to post
  $clicked_btn.siblings('i.fa-thumbs-down').removeClass('fa-thumbs-down').addClass('fa-thumbs-o-down');
  }
  });

});

$('.star-btn').on('click', function(){
  var post_id = $(this).data('id');
  $clicked_btn = $(this);
  if ($clicked_btn.hasClass('fa-star-o')) {
  action = 'star';
  } else if($clicked_btn.hasClass('fa-star')){
  action = 'unstar';
  }
  $.ajax({
  url: 'index.php',
  type: 'post',
  data: {
  'action': action,
  'post_id': post_id
  },
  success: function(data){
  res = JSON.parse(data);
  if (action == "star") {
  $clicked_btn.removeClass('fa-star-o');
  $clicked_btn.addClass('fa-star');
} else if(action == "unstar") {
  $clicked_btn.removeClass('fa-star');
  $clicked_btn.addClass('fa-star-o');
  }
  // display the number of likes and dislikes
  $clicked_btn.siblings('span.stars').text(res.stars);
  $clicked_btn.siblings('span.disstars').text(res.disstars);

  // change button styling of the other button if user is reacting the second time to post
  $clicked_btn.siblings('i.fa-star').removeClass('fa-star').addClass('fa-star-o');
  }
  });

});


$('.heart-btn').on('click', function(){
  var post_id = $(this).data('id');
  $clicked_btn = $(this);
  if ($clicked_btn.hasClass('fa-heart-o')) {
  action = 'bike';
  } else if($clicked_btn.hasClass('fa-heart')){
  action = 'unbike';
  }
  $.ajax({
  url: 'index.php',
  type: 'post',
  data: {
  'action': action,
  'post_id': post_id
  },
  success: function(data){
  res = JSON.parse(data);
  if (action == "bike") {
  $clicked_btn.removeClass('fa-heart-o');
  $clicked_btn.addClass('fa-heart');
  } else if(action == "unbike") {
  $clicked_btn.removeClass('fa-heart');
  $clicked_btn.addClass('fa-heart-o');
  }
  // display the number of likes and dislikes
  $clicked_btn.siblings('span.bikes').text(res.bikes);
  $clicked_btn.siblings('span.disbikes').text(res.disbikes);

  // change button styling of the other button if user is reacting the second time to post
  $clicked_btn.siblings('i.fa-heart').removeClass('fa-heart').addClass('fa-heart-o');
  }
  });

});


// if the user clicks on the dislike button ...
$('.dislike-btn').on('click', function(){
  var post_id = $(this).data('id');
  $clicked_btn = $(this);
  if ($clicked_btn.hasClass('fa-thumbs-o-down')) {
  action = 'dislike';
  } else if($clicked_btn.hasClass('fa-thumbs-down')){
  action = 'undislike';
  }
  $.ajax({
  url: 'index.php',
  type: 'post',
  data: {
  'action': action,
  'post_id': post_id
  },
  success: function(data){
  res = JSON.parse(data);
  if (action == "dislike") {
  $clicked_btn.removeClass('fa-thumbs-o-down');
  $clicked_btn.addClass('fa-thumbs-down');
  } else if(action == "undislike") {
  $clicked_btn.removeClass('fa-thumbs-down');
  $clicked_btn.addClass('fa-thumbs-o-down');
  }
  // display the number of likes and dislikes
  $clicked_btn.siblings('span.likes').text(res.likes);
  $clicked_btn.siblings('span.dislikes').text(res.dislikes);

  // change button styling of the other button if user is reacting the second time to post
  $clicked_btn.siblings('i.fa-thumbs-up').removeClass('fa-thumbs-up').addClass('fa-thumbs-o-up');
  }
  });

});

});

我希望看到在ubuntu中得到的相同输出,因为在localhost中它不会给我一个错误,但是当我将其上载到godaddy的cPanel File Manager时,我得到“此页面无法正常工作”,请帮助:c < / p>

0 个答案:

没有答案