why do i get the result in $result [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why do I get “Resource id #4” when I apply print_r() to an array in PHP?
How do i “echo” a “Resource id #6” from a MySql response in PHP?
why do i get "Resource id #6" as result in $result? want 1 or 0 :P
$sql = "SELECT * FROM members WHERE rchat=1 LIMIT 1";
$result = mysql_query($sql);
if (!$result) {
unlink($fn);
//$fn = $_SESSION['sess_user'].'.txt';
$fn = 'hittaingen.txt';
mysql_query("UPDATE members SET rchat=1 room='" . $_SESSION['sess_user'] . "' WHERE user='" . $_SESSION['sess_user'] . "'");
}
else {
//$fn = $result['room'].'.txt';
$fn = 'hitta.txt';
mysql_query("UPDATE members SET rchat=2 room='" . $result['room'] . "' WHERE user='" . $_SESSION['sess_user'] . "'");
}

mysql_query() returns just a reference of the result object and not the result itself. So in order to get you 0 or 1, you got to parse the result first using, e.g., mysql_fetch_array()
$row = mysql_fetch_array( $result );
Besides, you should definitely look into PDO and mysqli, as the the mysql_X() functions are marked as deprecated and generally not considered safe against SQL injections!

Related

MySQL - updating the average from one table into another table

I'm a MYSQL/PHP newbie and I'm sure this is a simple question. I'm trying to calculate the average of several questions and respondents from one table and updating a Group table with that value.
For example Table answers consists of (name, group_id, TaskClarity1, TaskClarity2, TaskClarity3) in Table B i want (group_id, avg(TaskClarity1,TaskClarity2,TaskClarity3)).
This is what I've got...
$avg_task_clarity_1 = mysql_query("SELECT AVG(TaskClarity1) WHERE gruppid = '$group_id'");
$avg_task_clarity_2 = mysql_query("SELECT AVG(TaskClarity2) WHERE gruppid = '$group_id'");
$avg_task_clarity_3 = mysql_query("SELECT AVG(TaskClarity3) WHERE gruppid = '$group_id'");
$avg_task_clarity = ($avg_task_clarity_1+$avg_task_clarity_2+$avg_task_clarity_3)/3;
$print_task_clarity_1" UPDATE results SET results.TaskClarity = '$avg_task_clarity'";
if (mysql_query($print_task_clarity_1)) { echo $print_task_clarity_1; } else { echo "Error TaskClarity1: " . mysql_error();
First, mysql_query() returns a resource, and you then need to extract information from it. Your query doesn't mantion any table name (I'll call it MyTable).
Also, you can get all three averages with one query.
Here's how I would start:
$table = "MyTable";
$sql = "SELECT AVG(TaskClarity1) AS avgClarity1,
AVG(TaskClarity2) AS avgClarity2,
AVG(TaskClarity3) AS avgClarity1
FROM $table WHERE gruppid = '$group_id'";
$resource = mysql_query($sql); //execute the query
if (! $resource = mysql_query($sql) ){
echo "Error reading from table $table";
die;
}
if (! mysql_num_rows($resource ) ){
echo "No records found in $table";
}
else {
$row = mysql_fetch_assoc($resource); // fetch the first row
$avg_task_clarity_1 = $row['avgClarity1'];
$avg_task_clarity_2 = $row['avgClarity2'];
$avg_task_clarity_3 = $row['avgClarity3'];
$avg_task_clarity =
($avg_task_clarity_1+$avg_task_clarity_2+$avg_task_clarity_3)/3;
//...
// other stuff you want to do
}
Please comment if this is not helpful enough, and I will revise my answer.

MySQL Update Not Updating Certain Rows

Here is my double-minded query:
$Quest = "SELECT * FROM TOAWorkorders";
$FindTechResult = mysql_query($Quest, $cxn)
or die ('The easter bunny is watching you' . mysql_error());
while ($row = mysql_fetch_array($FindTechResult))
{
if (strpos($BBT, 0, 3) != 'Sys')
{
$IdNum = $row['IdNum'];
$BBT = $row['BBT'];
$BBTArray = explode("-", $BBT);
$TechNum = $BBTArray["0"];
$Title = $BBTArray["2"];
$Name = explode(" ", $BBTArray['1']);
$FirstName = $Name["0"];
$LastName = $Name["1"];
}
echo $TechNum . ' !! ' . $FirstName . ' !! ' . $LastName . ' !! ' . $Title . '<br>';
$Quest = "UPDATE TOAWorkorders SET TechNum = '$TechNum', FirstName = '$FirstName', LastName = '$LastName', Title = '$Title' WHERE IdNum = '$IdNum'";
$result = mysql_query($Quest, $cxn) or die(mysql_error());
}
Everything works for about 2/3s of the database. That leaves 33,000 rows that are not updated. I cannot find any difference between the data that works and the data that doesn't.
Since you're doing an UPDATE, and you say the rest of the code works (meaning, I hope, that you get 109,112 echo'ed results), it must be that the ID isn't being found (WHERE IdNum = '$IdNum').
Try preceding that command with "SELECT COUNT(*) from TOAWorkorders WHERE IdNum = '$IdNum'" and see if you get 33,000 zeros when the program runs. If you do, then you have missing IdNum values in your table.
If you don't, please provide details and I'll let you know.

MYSQL query omitting deleted

I have a column in my MySQL database for deleted values are usually 0 or 1. Usually when doing searches I omit the deleted by doing something like "and deleted = "0"" but I cant figure out how to get this query below to omit my deleted column. any ideas would be appreciated thanks!
$query = "SELECT Status, COUNT(Status) FROM Assets GROUP BY Status";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result))
{
echo "". $row['COUNT(Status)'] ." systems ". $row['Status'];
echo ", ";
}
This should work:
$query = "SELECT Status, COUNT(Status) FROM Assets WHERE Assets.deleted = 0 GROUP BY Status";
Also, you should use mysqli or PDO rather than mysql (the php mysql library is obsolete).

how would I change this over to do a full text search instead of LIKE?

after some researching I put this code together to search a mysql table in the db. while it works fine, it limit itself to match the words exactly as the user enters it. anyone know how to make it so that it matches my some sort of relevancy? I have been reading about the full text search but I cant really seem to grasp it.
for example, if you search for 'unanswered questions' in two fields, I want to be able to get result like that include the searched word(s) in any string that it show up in, and list it according to relevancy, like so (search results example output):
- unanswered questions
- answered questions
- answer question
- unanswered questions
- unanswered questions
- questions
- answer
$k = trim ($_GET['search']);
$i = "";
$terms = explode (" ", $k);
$query = "SELECT * FROM table1 WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
else
$query .= "OR fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
}
// connect
include_once "connect.php"; //connect 2 db
$query = mysql_query($query);
$numrows = mysql_num_rows ($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc ($query)){
//
//
// echo out something here
//
//
}
}else
{
echo "No results found for <b>$k</b>";
}
to do a fulltext search you have to:
Create a Fulltext index in the table (note the fields can't be BLOB)
ALTER TABLE tablename ADD FULLTEXT(field1, field2,...);
in your case:
ALTER TABLE table1 ADD FULLTEXT(fld_title, fld_keyword);
in php change
$k = trim ($_GET['search']);
$i = "";
$terms = explode (" ", $k);
$query = "SELECT * FROM table1 WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
else
$query .= "OR fld_title LIKE '%$each%' OR fld_keyword LIKE '%$each%' ";
}
for
$k = trim ($_GET['search']);
$query="SELECT * FROM table1 WHERE MATCH(fld_title, fld_keyword) AGAINST ('".$k."')";
if you want to see the relevancy of the results:
$query="SELECT *, MATCH(fld_title, fld_keyword) AGAINST ('".$k."') as relevancy FROM table1 WHERE MATCH(fld_title, fld_keyword) AGAINST ('".$k."')";
The MATCH-AGAINST returns a number: 0 for no match or other depending on matching.
You can "order by relevancy", change the query for make more relevant the search... MATCH(fld_title, fld_keyword) AGAINST ('".$k."') > 0.5
Only one problem: the AGAINST part ($k for you) must be greater than 3 characters.

Is there a way to combine SQL with FQL (Facebook Query Language)

Is there a way to combine SQL with FQL.
I am talking about something like FQL multiqueries.
This is my sample code.
//Here I take data from a SQL database
$result_user_pages = mysql_query("SELECT Page_ID FROM user_fanpage WHERE User_ID='$uid'");
while($row = mysql_fetch_array($result_user_pages))
{
$user_page_ID[$m] = $row['Page_ID'] ;
$m++;
}
//Here I pass '$user_page_ID' array into a for loop and call facebook api
for ($i=0; $i
$query1 = "SELECT post_id FROM stream WHERE source_id='$user_page_ID[$i]'";
$query2 = "SELECT fromid,text from comment WHERE post_id IN (SELECT post_id FROM #query1)";
$queries = '{
"query1": "' . $query1 . '",
"query2": "' . $query2 . '"
}';
$attachment = array("method"=>"fql.multiquery","queries"=>$queries,'access_token'=>$access_token);
$ret_code = $facebook->api($attachment);
}
If '$user_page_ID' has 10 elements, api is called 10 times.Therefore sometime it gives me a connection time out error message.If there is a away to combine sql query with fql(like fql multiqueries), I can avoid this error message.
Can any one tell me whether there is a way to do this or any other solution...??
You cannot do that with the restrictions that are in place