Jump to content

Recommended Posts

Hey,
 
How you been, I was trying to work with json data in php and mysql using ajax
 
Something strange is happening, when i comment the below line -
 
// $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
 
 
<?php
session_start();


$message=array();
include('database.php');

$sql="SELECT * from ratings ORDER BYRAND() LIMIT 1";
$result = mysqli_query($conn,$sql);

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
//$message["image"]=$row['image'];

$message["message"]='hello';
//$message["message2"]='hello2';
header('content-type: application/json');
echo json_encode($message);
 
I get the message in my console.log using ajax but when i uncomment this $row = mysqli_fetch_array....   i get nothing in my console log
 
what is wrong in here ? I am on xampp 3.2.2 incase and there is no db error, also pasting my databse.php code just incase you wish to see
 
<?php
$DBServer = 'localhost';
$DBUser = 'root';
$DBPass = '';
$DBName = 'starrating';

$conn = new mysqli($DBServer,$DBUser,$DBPass,$DBName);
if($conn->connect_error){
    // echo $conn->connect_error;
    die('Connect Error: ' . $mysqli->connect_error);
}


function cleanup($data){
	return mysql_real_escape_string(trim(htmlentities(strip_tags($data))));
}
 
incase you want to see my jquery code, but i know its not related to this
 //ajax call
               $.ajax({
                   type:"POST",
                   url:"server.php",
                   datatype:"json",
                   success: function(data){
                       console.log(data);
                   }

Also attaching both the files

 
 
 

// $row=mysqli_fetch_array($result,MYSQLI_ASSOC);

database.php

index.html

server.php

What is “ORDER BYRAND()” supposed to mean?

 

Otherwise, the usual problems:

  • You have no error checks whatsoever. No, mysqli doesn't do that for you, unless you explicitly ask for error reporting.
  • Your PHP error reporting also seems to be off. You should be seeing a big fatal error in your log.
  • The Ajax script is unable to handle errors, which is a problem.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.