Using php script to query the mysql database with no problem. However I'm unable to parse the result with JSON and send it back to android. I have no problem encoding/parsing arrays - is a single variable different? Is there a better way to send a row count back to android?
php code:
$result = mysql_query("SELECT * FROM logs WHERE uid = '$uid' AND tableid = '$tid'");
$num_rows = mysql_num_rows($result);
echo json_encode($num_rows);
You can just select the number of rows in your query.
SELECT COUNT(*) ...
I am not sure what you are asking, the above code will just output the number of rows, To make it accessible in android app, you have to make it as key value pair, So that you can access the row number using the key as given below,
$result = mysql_query("SELECT * FROM logs WHERE uid = '$uid' AND tableid = '$tid'");
$num_rows = mysql_num_rows($result);
$output = array("count" => $num_rows);
echo json_encode($output);
This will output {"count":123}
Related
echo $query = "SELECT user_id FROM login WHERE username = '8578896785'";
echo $result = $mysql_query($query);
cannot fetch contact number when use in php
I executed the same query on mysql and it gives results. The same query does not work in PHP.
The column is of varchar(200) datatype and contains data as string,alphanumeric as well as numbers
if u use numeric date to fetch then u have to use
SELECT user_id FROM login WHERE username = 123 withought ''
and when you try to fetch string data
SELECT user_id FROM login WHERE username = 'abc' with ''
you can't output a resource with the echo statement, you have to fetch the data in the result before you can output it.
$query = "SELECT user_id FROM login WHERE username = '8578896785' LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['user_id'];
1st Edit: removed unnecessary echo's
2nd Edit: adding a LIMIT 1 to your query, the username schould be just one time in the table ;)
3rd Edit: removed $ in front of a function (mysql_query())
4th Edit: fetching data from the ressource (as array, for other methods to fetch the data look into the php manuals)
5th Edit: output the userid
I have this:
$query = "SELECT time FROM table_schedule GROUP BY time ORDER BY count(*) desc LIMIT 1 ";
then I stored it in:
$result = mysql_query($query);
I will output the value by <?php echo $result ?> but I am always getting this value Resource id #20.
I am new in sql so I hope someone can help me with my problem.
You are trying to show the result in a unproper way. In PHP mysql_query() will execute a query. In order to get the result, you should use
$result = mysql_query($query);
$row=mysql_fetch_array($result); // fetches the result of the query and stores in an associative array
$time=$row['time']; // get the result from the associate array with field name as the index
You can also use ,
$row=mysql_fetch_row($result);
$time=$row[1];
which will fetch the result as an enumerated array where you want to display each fields with the column number starting from 0.
For reference
looking at the php function bellow i have this scenario: there is more then one IdUserfollow each id have to be passed on result2 and all the retrieved values have to be combined and returned to Xcode
function viewhome($IdUser){
$result1 = query("SELECT * FROM follows WHERE IdUser = '%s' ", $IdUser);
$IUser = $result['result'][0]['IdUserfollow'];
$result2 = query("SELECT * FROM photos WHERE IdUser = '%s' ORDER BY IdPhoto DESC LIMIT 50", $IUser);
print json_encode($result2);
}
how can i do that.
i sow this problem Using a Loop Inside a Function which was similar to mine but still I'm confused. considering that my return will be using JSON and also i don't know how to set the loop limit from the first query result. is it possible that i will have to do two functions, the first one will return the number of repetition and the second one will only have the loop and return the result? if yes then how can i combine two results - or whatever the value of the repetition is- and send it back using json
any help would be appreciated.
You may collect all retrieving data from second query to result array like this:
function viewhome($IdUser){
$result1 = query("SELECT * FROM follows WHERE IdUser = '%s' ", $IdUser);
$IUser = $result['result'][0]['IdUserfollow'];
$result = array();
foreach ( $result['result'] as $data ) {
$result[] = query("SELECT * FROM photos WHERE IdUser = '%s' ORDER BY IdPhoto DESC LIMIT 50", $data['idUserfollow']);
}
print json_encode($result); // all the results for each IdUserfollow from first query
}
But all the same, I advice you not to send several queries to DB, but build only one query, wich solve you problem.
MY SQL QUERY:
$q = mysql_query("SELECT * FROM `ads` WHERE keywords LIKE '%$key%' ORDER BY RAND()");
RESULTS: KEYWORD123
This query searches and results in one random row but i want to show 2 random rows.
How to do that?
any solution?
how??
im grabbing it using this
$row = mysql_fetch_array($q); if ($row
<= 0){ echo 'Not found'; }else{ echo
$row['tab']; }
That query (as-is) will return more than one row (assuming more than one row is LIKE %$key%). If you're only seeing one record, it's possible you're not cycling through the result set, but rather pulling the top response off the stack in your PHP code.
To limit the response to 2 records, you would append LIMIT 2 onto the end of the query. Otherwise, you'll get every row that matches the LIKE operator.
//Build Our Query
$sql = sprintf("SELECT tab
FROM ads
WHERE keyword LIKE '%s'
ORDER BY RAND()
LIMIT 2", ('%'.$key.'%'));
// Load results of query up into a variable
$results = mysql_query($sql);
// Cycle through each returned record
while ( $row = mysql_fetch_array($result) ) {
// do something with $row
echo $row['tab'];
}
The while-loop will run once per returned row. Each time it runs, the $row array inside will represent the current record being accessed. The above example will echo the values stored in your tab field within your db-table.
Remove your order by and add a LIMIT 2
That happens after the execution of the SQL.
Right now you must be doing something like
$res = mysql_query($q);
$r = mysql_fetch_array($res);
echo $r['keywords'];
what you need to do
$q = mysql_query("SELECT * FROM ads WHERE keywords LIKE '%$key%' ORDER BY RAND() LIMIT 2");
$res = mysql_query($q);
while($r = mysql_fetch_array($res)){
echo "<br>" . $r['keywords'];
}
Hope that helps
This query will return all rows containing $key; if it returns only one now this is simply by accident.
You want to add a LIMIT clause to your query, cf http://dev.mysql.com/doc/refman/5.0/en/select.html
Btw both LIKE '%... and ORDER BY RAND() are performance killers
I have this
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['id'];
}
This echo's all id's found in the table.
How can I choose to echo only a selected id.
Say the second id found on the table?
EDIT
I think I have confused people and myself aswell.
Let me try to explain again.
Using the above query I can echo all results found in the table with echo $row['id'];
However I do not want echo all results, just selected ones.
You guys have suggested I use limit or a Where clause.
If I do this I will be limited to just one record. This is not what I want.
I want to echo a selection of records.
Something likes this
echo $row['id'][5], $row['id'][6], $row['id'][6]
But obviously this is incorrect syntax and will not work but hopefully you get what I am trying to do.
Thanks
If you only want the second row then you could change your query to use offset and limit e.g.
SELECT id FROM table LIMIT 1, 1
You could also use a for loop instead of the while loop and then put in a conditional.
UPDATE
Just noticed comments above - you also need to sort the PHP bug by changing mysql_fetch_array to mysql_fetch_assoc.
UPDATE 2
Ok based on your update above you are looking to get all of the rows into an array which you can then iterate over.
You can just use mysql_fetch_array and then use $array[0]. For example:
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
$ids = array();
while($row = mysql_fetch_array($result)) {
$ids[] = $row[0];
}
From what I can gather from your questions you should not be selecting all records in the table if you wish to just use the Nth value, use:
SELECT id FROM table LIMIT N, 1
That will select the Nth value that was returned. Note: The first result is 0 so if you wish to get the second value the Nth value should be 1.
mysql_data_seek() let's you jump to a specific data-set(e.g. the 2.nd)
Example:
$sql = "SELECT id FROM table";
$result = mysql_query($sql) or die(mysql_error());
//get the 2nd id(counting starts at 0)
if(mysql_data_seek($result,1))
{
$row=mysql_fetch_assoc($result);
echo $row['id'];
}
OR:
use mysqli_result::fetch_all
It returns an array instead of a resultset, so you can handle it like an array and select single items directly (requires PHP5.3)