I'm not very good yet with sessions or arrays.
Here is what I have so far but I am getting an error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
Here is my code:
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin
WHERE contact_id = $contactid");
while ($adminrow = mysql_fetch_array($checkgroupadmin, MYSQL_ASSOC)) {
$_SESSION[group_admin] = array('group'=>$adminrow['group_id']);
}
Any help would be appreciated. :)
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
You are getting your error because $checkgroupadmin is not an actual query resource that mysql_fetch_array() requires.
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin WHERE contact_id = $contactid");`
In other words the above object oriented call is not returning a mysql resource query.
Why are you mixing object oriented style with procedural style anyways. Secondly the above code $this>db->query should be happening inside a Class' method
mysql_fetch_array() returns an associative array of the current results, but in your code you would just be overwriting the same value in the $_SESSION for each result. You likely need to create your own array of resulting IDs and then assign that to a $_SESSION variable.
// query database
$checkgroupadmin = $this->db->query("SELECT group_id FROM groupadmin WHERE contact_id = $contactid");
// array to hold each group_id
$result_array = array();
// varify that $checkgroupadmin returned results
if ($checkgroupadmin){
// get results into associative array
while ($adminrow = mysql_fetch_array($checkgroupadmin, MYSQL_ASSOC)) {
// put the group id into your results array
$result_array[] = $adminrow['group_id'];
}
}
// set results array to the 'group_admin' key in $_SESSION
$_SESSION['group_admin'] = $result_array;
Please note that the mysql_* extensions have been deprecated and you should be using mysqli or PDO instead.
Related
I have execute query using PHP which previously executed on mssql server database . Now with the same table and data. I using mysql database to execute my query. But error happen. Any suggestion for my query below in order to can execute using mysql database :
$year = mysql_query("SELECT * FROM education_year ORDER BY id DESC");
if (isset($_GET['year'])){
$educationyear= mysql_fetch_array(mysql_query("SELECT * FROM educationyear WHERE year='{$_GET['year']}'"));
}else {$educationyear = mysql_fetch_array($year);}
$kode['KODE'] = mysql_fetch_array(mysql_query("SELECT KODE FROM educationyear WHERE year='$educationyear'"));
$result = mysql_query("SELECT * FROM Province");
while($row = mysql_fetch_array($result))
{
$xd = mysql_fetch_array(mysql_query("SELECT COUNT (*) AS total FROM child WHERE id_province='{$row['province_code']}' AND education='A'
AND educationyear='{$educationyear['KODE']}'"));
}
Error message like below :
Notice: Array to string conversion in C:\xampp\htdocs\xy\demo.php on line 19
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xy\demo.php on line 20 .
Its line when execute $xd query.
There are a few problems with your code
1st: When you use an array within double-quoted string, do not quote the array key. Change
"...WHERE year='{$_GET['year']}..."
"...WHERE id_province='{$row['province_code']}'..."
To:
"...WHERE year='{$_GET[year]}..."
"...WHERE id_province='{$row[province_code]}'..."
2nd: The design pattern below is not good:
mysql_fetch_array(mysql_query("SELECT...")
You're taking the result of mysql_query and feeding it directly to mysql_fetch_array. This works as long as the query succeeds and returns a resource. If the query fails, it will return FALSE and mysql_fetch_array will trigger the error you see:
mysql_fetch_array() expects parameter 1 to be resource, boolean given
Instead, make sure there is no error before proceeding
$result = mysql_query("SELECT...")
if($result===false){
//Query failed get error from mysql_error($link).
//$link is the result of mysql_connect
}
else{
//now it's safe to fetch results
$record = mysql_fetch_array($result);
}
3rd: do not use mysql_ functions. They have been abandoned for years and have been removed from the most recent version of PHP. Switch to MySQLi or PDO
4th: learn about prepared statements. You're using user supplied input directly in your query ($_GET['year']) and this makes you vulnerable to SQL injection.
I've been running the query oh phpMyAdmin and it shows all the rows, but my query in php only returns the first row.
$result = $mydb -> query("SELECT * FROM music_sheet WHERE category='".$_REQUEST["submitter"]."'");
print(count($result)); //always returns 1
for ($x = 0; $x < count($result); $x++) {
$row = mysqli_fetch_row($result);
}
For reference, here's why count() is returning 1. From the manual:
If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable [the parameter] is NULL, 0 will be returned.
Since $result is a resource (object) that is returned from your query, not an array which you would get within your loop getting the actual data out of the resource, count($resource) will return 1.
Your solution is of course to use mysqli_num_rows(). To retrieve data from this resource, you need to loop over it as you are doing, but either use mysqli_num_rows() in place of count(), or other (more common) ways of looping through a result set, like so:
while($row = mysqli_fetch_row($result)) {
// do stuff
}
You have to use mysqli_num_rows($result) function to count rows which are returned by MySQLi query.
Try this
echo mysqli_num_rows($result);
while($row = mysqli_fetch_row($result)) {
// write your code...
}
Use this instead of the for loop
the first thing that the query method returns to you is a resource/object. This query method always return a mysqli_result object for successful queries using SELECT, SHOW, DESCRIBE or EXPLAIN queries . For other successful queries mysqli_query() will return TRUE. For that reason it always count it as "1", what you should try is:
$result = $mydb -> query("SELECT * FROM music_sheet WHERE category='".$_REQUEST["submitter"]."'");
$numRows = $result->num_rows;
print(count($numRows)); //it should show you the total amount of rows
//verify the amount of rows before of tryng to loop over it
if ($numRows) {
while($object = mysqli_fetch_object($result)){
//for each loop you will have an object with the attributes of the row
echo $object->song_name;
}
}
This should work,
Regards.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am getting this error while trying to run a function. I am sending a value $id to this function and trying to return the associated array. But I get a warning supplied argument is not a valid MySQL-Link resource.
But if i run the same code by eliminating the function part and give a static value to $id it returns the result correctly. This is the function
<?php
mysql_select_db($database_spyware, $spyware);
function get_product($id)
{
$query = sprintf("select product.descr as product_descr, product_id as Product_id,
category.descr as category from sp_url
inner join product
on product.id = sp_url.product_id
inner join category
on product.category_id = category.id
where sp_url.id = $id");
$query_result = mysql_query($query, $spyware) or die(mysql_error());
$row_rs_query = mysql_fetch_assoc($query_result);
$totalRows_rs = mysql_num_rows($query_result);
return($row_rs_query);
}
?>
That happens because your second parameter spyware in function "mysql_query" is declared outside the function and is unreadable on your function scope.
try
global $spyware;
at the beginning of the function.
or leave the second parameter empty.
I wanted to arrange the array of table list with sort() function but i am getting same kind of warning.
<?php
require_once("lib/connection.php");
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
sort($result);
foreach ($result as $result){
echo $result ;
}
?>
and the warning I am getting are:
Warning: sort() expects parameter 1 to be array, resource given in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 9
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 10
The warning is pretty clear: mysql_query does not return an array with results from the query, but a resource. You need a function like mysql_fetch_array() to return the data you need (and on which you can perform a sort operation).
See the manual for the use of mysql_query() http://nl3.php.net/mysql_query
And maybe unrelated, but you can sort your results in MySQL right away by adding ORDER BY <fieldname> to your query.
The variable $result is only a resource of the type result. You need to fetch then the data from the result set with e.g. mysql_fetch_assoc().
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row["Tables_in_st_db_1"];
}
sort($array);
foreach ($array as $item) {
echo $item;
}
I'm not providing the most efficient code imaginable, but this should make it clear what's going on and solve your problem:
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$my_array_of_table_names = array();
while ( $row = mysql_fetch_array($result, MYSQL_NUM)) {
$my_array_of_table_names[] = $row[0];
}
sort($my_array_of_table_names);
foreach ($my_array_of_table_names as $table_name){
echo "$table_name\n";
}
Your problem is that you aren't actually getting the data from the query.
mysql_query() doesn't give you a recordset.
What it does is query the database and returns a database resource which you can then use to get the data.
What you need is after calling mysql_query(), you then need to also call mysql_fetch_array() or similar. (there are a range of functions available, but that's probably the best one to use in this case). Then sort() the data from that, not $result.
It clearly says: it expects an array and you pass something else.
If you had checked the type of $result you would have seen that it is not an array, intead a resource.
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?
Below is the code:
$result=mysql_query("select * from choices where a_id='$taskid'")or die(mysql_error());
print_r($result);
I get "Resource id #4", any idea?
After I added
while($row=mysql_fetch_assoc($result))
{ print_r($row); }
I just got []
What's wrong?
You are trying to print a mysql resource variable instead of the values contained within the resource it references. You must first try to extract the values you have gotten by using a function such as mysql_fetch_assoc().
You might also try mysql_fetch_array() or mysql_fetch_row(), but I find associative arrays quite nice as they allow you to access their values by the field name as in Mike's example.
mysql_query() does not return an array as explained in the manual. Use mysql_fetch_array(), mysql_fetch_assoc(), or mysql_fetch_row() with your $result. See the link above for more info on how to manipulate query results.
$result = mysql_query('SELECT * FROM table');
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
$result is a resource variable returned by mysql_query. More about resource variables: http://php.net/manual/en/language.types.resource.php
You must use other functions such as mysql_fetch_array() or mysql_fetch_assoc() to get the array of the query resultset.
$resultset = array();
$result=mysql_query("select * from choices where a_id='$taskid'") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$resultset[] = $row; // fetch each row...
}
mysql_free_result($result); // optional though...
print_r($resultset);
See:
http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.mysql-fetch-assoc.php
http://php.net/manual/en/function.mysql-query.php
Resources are special variable types used by PHP to track external resources like database connections, file handles, sockets, etc.