select data from row error - php

I have some code which selects data from a row. It is working and prints the result correctly, but I have an error I can not figure out.
$rs=array();
$select=array ("system_name", "location", "alarmtype", "severity", "start_time", "end_time", "duration", "reason","shift_oparation", "system_oparation");
for ($i=0;$i<=9;$i++){
$SQL = "SELECT (".$select[$i].") FROM (".$is.") WHERE duration=('".$ic."') AND location=('".$id."')";
$result = mysql_query($SQL);
$db_field = mysql_fetch_array($result);
$rs[$i]=$db_field[$select[$i]];
echo $rs[$i];
}
Echo prints correctly, but there is an error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given.

I think some of your queries dont return anything. In order to get rid of the warning you should check if the $result was in fact a resource.
Try this:
$rs=array();
$select=array ("system_name", "location", "alarmtype", "severity", "start_time", "end_time", "duration", "reason","shift_oparation", "system_oparation");
for ($i=0;$i<=9;$i++){
$SQL = "SELECT (".$select[$i].") FROM (".$is.") WHERE duration=('".$ic."') AND location=('".$id."')";
$result = mysql_query($SQL);
if($result){
$db_field = mysql_fetch_array($result);
$rs[$i]=$db_field[$select[$i]];
echo $rs[$i];
}
}
However, your code will be a lot more efficient if you use only one query.

You are doing something really strange. It looks like $select is a list of columns in a table, and you are fetching them one column at a time!
You can select them all at once, with this query:
$sql = "SELECT system_name, location, alarmtype, severity,
start_time, end_time, duration, reason, shift_oparation, system_oparation
FROM " . $is . " WHERE duration = '" . $ic . "' AND location = '" . $id . "'";
Note I've removed some extraneous parenthesis you had. Also be very careful with building SQL queries dynamically like this. I'm assuming you know the values of $is, $ic, and $id to be safe from SQL injection attacks,
Run the result, and then iterate over the row array
$result = mysql_query($sql);
if ($result)
{
$row = mysql_fetch_array($result); //fetches the first result row as an array
// Can be accessed like $row['system_name']
foreach ($row as $key => $value)
echo $key, " = ", $value;
}
else
die(mysql_error() . $sql);
If the result is false, it will perform the die statement and print out the error returned from MySQL and also the query we built, so you can see if it looks correct.

Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in.
This means that mysql query produced errors .
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
Use mysql_error to display teh error and see why it happened.
e.g.
if(!$result){
echo mysql_error();
}
Protip: donlt use mysql libraries as noted in php manual

there is problem in select statement please check the select query or you can run individual select query and check the output

Related

mysqli_fetch_object returns nothing

I try querying very simple sql statement with mysqli
"select * from area where area_pre_id=6035;"
it returns nothing.
After querying this in phpmyadmin , it returns 78 rows ....
PHP code is as below;
$sql = "select * from area where area_pre_id=6035;";
if ($result = mysqli_query($conn, $sql, MYSQLI_USE_RESULT)) {
while($obj = $result->fetch_object()){
if($obj->area_local_name_th){
$my_province = $obj->area_local_name_th . "(" . $obj->area_eng_name . ")";
}else{
$my_province = $obj->area_eng_name;
}
$line[] = array("ProvinceID"=>$obj->area_id,"ProvinceName"=>$my_province);
}
}
Please tell me what's wrong with my code or sql statement.
Your mysqli command is right.I think there is no value in your database for that particular id.
Is the datatype of that id fild integer?
If it is integer then the query is right.But if it is varchar then you have to put a single quote.
select * from area where area_pre_id='6035';
You are trying to use both procedural and OOPs concept. THat will be the issue.
Try this
Change $result->fetch_object() to mysqli_fetch_object($result)

SQL query doesn't seem to work

I'm trying to run the following query but it doesn't seem to work properly and an error comes back with:
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\VACANCY\pages\search\booking2.php on line 83
Which is while:
$row = mysqli_fetch_row($Result)
All I want to is to return the chosen JOB_TITLE AND LOCATION from my jobs table. Please any help would be great as I have spent hours trying to solve it.
<?php
$Query = "(SELECT FROM jobs " .
"WHERE jobs.JOB_TITLE ='$_POST[JOB_TITLE]' " .
"AND jobs.LOCATION = '$_POST[LOCATION]')";
$Result = mysqli_query($DB, $Query);
while ($row = mysqli_fetch_row($Result))
{
echo $row['JOB_TITLE'] . " " . $row['LOCATION'];
echo "<br />";
}
?>
You need to add something after SELECT. For example, SELECT JOB_TITLE, LOCATION FROM...
Your statement is "SELECT FROM", so you don't indicate what you want to select. If you query "SELECT * FROM" then all fields will be selected and the query will work.
But please be aware that your code is very insecure. The POST value from the search form can be manipulated by an attacker and the query can be used e.g. to truncate the whole table. Read more at http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php.

SQL Resource ID #4 from SELECT COUNT? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
I have a statement:
$query1 = mysql_query("SELECT COUNT(author) FROM userpages WHERE `author` = '" . $userid . "'") or die();
echo $query1;
Ignore the amazingly awful naming convention their, its just for testing it before i beef it up. Because im not actually getting data from the table i should just be able to echo the statement shouldnt i? However its giving me an error.
The error is: Resource id #4
Any advice?
Thanks.
mysql_query returns a resultset resource, you have to loop through the resultset using
mysql_fetch_array($query1)
(or similar) to retrieve each record. It doesn't matter whether the query returns 1 row or 100,000 rows, the principle is the same
please read the relevant sections of the manual that explain this in great detail before feeling the need to ask for help.... reading the manual is your friend!
EDIT
while ($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
echo $row['COUNT(author)'];
}
or change your SQL query to
$query1 = mysql_query("SELECT COUNT(author) AS authorcount FROM userpages WHERE `author` = '" . $userid . "'") or die();
and then
$row = mysql_fetch_array($res,MYSQL_ASSOC))
will return an array with an index of 'authorcount'
echo $row['authorcount'];
http://www.php.net/manual/en/function.mysql-query.php
Returned values:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning
resultset, mysql_query() returns a resource on success, or FALSE on
error.
And:
The returned result resource should be passed to mysql_fetch_array(),
and other functions for dealing with result tables, to access the
returned data.
I think, this will be helpfull
$query = "SELECT COUNT(author) as cnt FROM userpages WHERE `author` = '" . $userid . "'";
echo $query;
$res = mysql_query($query);
while ($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
print_r($row);
}

Retrieving results for query from MySQL using PHP

I want to query a MySQL database. I have the following. $name keeps changing in a loop. ($query is a sample query here)
$query = "SELECT id FROM table1 WHERE name='$name';";
$result = mysql_query($query) or die(mysql_error());
echo "$result<br/>";
While ($row = mysql_fetch_array($result)) {
echo $row["id"] . " - " . $row["name"];
}
with the echo "$result<br/>" it just prints something like Resource id #. Nothing from $row is printed. The MySQL connection is fine. If I run the query without PHP, it works fine. What might be wrong?
Everything is correct, you'll get nothing from $result until you use some function like mysql_fetch_array() or mysql_fetch_assoc() like you do in your loop.

MySQL PHP count(*) returning something weird

I'm running the following query, expecting it to return an INTEGER, but it returns "Resource id #3"
What am I doing wrong?
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
echo $queryPlans;
There are actually 15 rows in this table, and I would like to return the number 15.
Any suggestions?
mysql_query will return a php resource(see: http://www.php.net/manual/en/language.types.resource.php).
The returned resource should then be passed to mysql_fetch_assoc or similar.
Since you are only getting the count, you can use the following:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$count = mysql_result($queryPlans,0,0);
echo $count;
You need:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$row = mysql_fetch_array($queryPlans);
echo $row[0];
mysql_query() isn't returning the result. It's returning a resource you can loop across and interrogate for rows (as above).
This is actually expected behavior according to the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
It's a regular select that returns one row with one column and should be treated as such. You can call mysql_fetch_array on the result:
$row = mysql_fetch_array($resource);
$count = $row[0];
mysql_query() returns a result resource. You need another function the get "valuable information" from that resource. In this case mysql_fetch_array()/mysql_fetch_row()/mysql_fetch_object as cletus pointed out. Or (since it's only a single value) mysql_result().
Any sql query may fail for various reasons. You should always check the return value of mysql_query(). If it's FALSE something went wrong and mysql_error() can tell you more about it.
$mysql = mysql_connect(...) or die(mysql_error());
mysql_selecT_db(.., $mysql) or die(mysql_error($mysql));
$query = "SELECT count(*) FROM infostash.rooms";
$queryPlans = mysql_query($query, $mysql) or die(mysql_error($mysql));
$cRows = mysql_result($queryPlans, 0);
echo $cRows;
If you are planning on using the full query later (e.g. select , rather than count()), you can save yourself a database hit by using mysql_num_rows() on the full query. Example:
$queryPlans = mysql_query("SELECT * FROM infostash.rooms");
$results = mysql_fetch_array($queryPlans);
echo "There were " . mysql_num_rows($queryPlans) . " results";
while($row = mysql_fetch_assoc($queryPlans)){
// Do stuff here
}
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
mysql_num_rows($queryPlans);
http://us.php.net/manual/en/function.mysql-num-rows.php

Categories