Search posts, tags, users, and pages
I'm not sure where it fails and what error and exactly what you want to achieve. But I have never seen top being used, that might just be me.. But you should be able to remove top 3 and add LIMIT 3 at the end instead
And where are those 3 prepared statements being used? Is there something I can't see on my mobile
OK, if I do, my problem is I don't know how to get them to work at the end of the code. I have this old code that I did to get one row and it worked fine. but I'm not sure how to do the same but with multiple rows..
Old Code:
<?php $con = mysqli_connect("", "", "", "");
$index = $_POST["index"];
$statement = mysqli_prepare($con, "SELECT * FROM jaralele WHERE index = ?");
mysqli_stmt_bind_param($statement, "iss", $index, $title, $comment);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $index, $title, $comment);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["index"] = $index;
$response["title"] = $title;
$response["comment"] = $comment;
}
echo json_encode($response);
?>
I have another code in java that calls this PHP code. it's going back to the code there, and I want 9 of them to go back instead of 3, just don't know how..
I think it's easier to explain what you want to do, then I can tell you how I would do it and you can compare. I will come back tomorrow (01.08am here now)
OK, so I have a table that has 3 Variables: int index (auto increment), String title, String comment. I want the PHP code to take the last 3 rows of the table (the rows with the biggest indexes), and return them to the java code I already built to use them. I dont know how to code PHP and therefore I don't know what to do after the "import" of the variables from the MySQL table.
And thank you vary much for doing this!
Columns not variables :-) Let me explain it with MySQL code, first at least.
To take out the 3 with the highest index:
SELECT * FROM `jaralele` ORDER BY `index` DESC LIMIT 3
Whatever resource you use, will now contain 3 rows with the 3 highest indices. I don't remember how it works in Java, sorry, I haven't worked with Java for some years. In PHP you can do like this
$query = mysql_query('SELECT * FROM `jaralele` ORDER BY `index` DESC LIMIT 3');
while ($row = mysql_fetch_array($query))
echo 'Index: '. $row->index .'. Title: '. $row->title .'. Comment: '. $row->comment .'.<br>';
The prepared statements are not really necessary as far as I know, since you have no variable input. The reason you would use prepared statements are to avoid SQL injections.
Hi, first of all thank you! just to be sure, what are these lines do?
$statement = mysqli_prepare($con, "SELECT * FROM jaralele ORDER BY index DESC LIMIT 3);
mysqli_stmt_bind_param($statement, "iss", $index, $title, $comment);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $index, $title, $comment);
$response = array();
$response["success"] = false;
I found them in the internet and used them with the login system.. also I need all rows to be returned like this:
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["index"] = $index;
$response["title"] = $title;
$response["comment"] = $comment;
}
echo json_encode($response);
getting them inside a variable and then returning the variable at the end. is that possible like you did just splitting it and using a variable instead of echo?
My output
$query = mysql_query('SELECT * FROM `jaralele` ORDER BY `index` DESC LIMIT 3');
Will give you a resource that can be turned in to an array. You can use mysql_fetch_array when you use it in a loop or you can use mysql_fetch_assoc and get an array with your data. Since you have 3 outputs (that's not for sure though, so I recommend an array, since there might be only 0, 1 or 2).
You can do something like this
$query = mysql_query('SELECT * FROM `jaralele` ORDER BY `index` DESC LIMIT 3');
$response = mysql_fetch_assoc($query);
Then you have
echo $response[0]->index;
And so on. But better to use them in a while for foreach rather than assuming there are 3.
I'm not sure why, but I'm getting a warning saying "mysql_fetch_assoc" is "deprecated in the current version of PHP". also how the array will look like when it gets back? I don't really sure how to behave with him in my java code.
Yeah it has been a while ago since I used those functions. I can see you need to use the mysqli interface now. Should work if you replace mysql with mysqli
@ItayMeir You need to use either mysqli or PDO to avoid these warnings. mysql_fetch_assoc was deprecated in PHP 5.5.0.
ok, is this code ok now? will it work?
$con = mysqli_connect("host", "user", "pass", "db");
$query = mysqli_query($con, 'SELECT * FROM `jaralele` ORDER BY `index` DESC LIMIT 3');
$response = mysqli_fetch_assoc($query);
$response[success] = false;
while($row = mysqli_fetch_array($query)){
$response["success"] = true;
$response["index"] = index;
$response["title"] = title;
$response["comment"] = comment;
}
echo json_encode($response);