I need to echo the 4th rows 3rd column from a mysql Database
$query = 'SELECT * FROM picture LIMIT 10';
$result = mysql_query($query);
$row = mysql_fetch_array($result,MYSQL_NUM );
-
Question 1 > What does the $result contain ? The table form as seen in MYSQL ?
-
Question 2 > What does the $row contain ? Just the 1st row ?
-
Question 3 > How can i move to the 4th row ? echo $row[4] doesn’t
work!! ? -
Question 4 > How can i move to the 4th row column 3 ?
echo $row[4][3]doesn’t work!! ? -
Question 5 – If i have to send 2 rows to back to the client side (
suppose that ajax requested it from php )
would it be php
$query = 'SELECT * FROM picture LIMIT 5';
$result = mysql_query($query);
$rec = mysql_fetch_array($result,MYSQL_NUM );
print_r (json_encode($result[0]));
And on html side
$.ajax({
url: "viewnew.php",
data: {
current_image: imId,
direction : dir
},
success: function(ret){
console.log(ret);
var arr = eval(ret);
alert("first: " + arr[0] + ", second: " + arr[1]);
Your answer is
echo mysql_result($result, 3, 3). You can also useecho mysql_result($result, 3, 'rowname'). Note that since it is zero-indexed, the fourth row is #3, as the first row is #0.$resultcontains a MySQL Query Result Object, you need to use the builtin functions to access it.$rowdoes indeed just contain an array of the first row only. The internal pointer is increased, so if you ran the$rowline of code twice,$rowwould now have the second row. Once you run out of rows, that line of code will evaluate tofalse(that’s useful below).php.net offers this piece of code:
Since the while statement will evaluate to false when it runs out of rows, you get all the rows. Since you’re making the query, you should know how to handle the relevant data in each row.
In regards to the AJAX, on server side, you need to assign the rows to array variables (as above) and then output the ones you want. Since you know how you will be outputting them, it is up to you how to handle them on client side.
htmlspecialchars()orhtmlentities()will make the data safe for output.A sample method would be as follows:
php.net has an extensive documentation of how to use the MySQL functions in PHP.
Also, don’t forget security!
mysql_real_escape_string()is almost always necessary when using user input to make a query.