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.
Related
This question already has answers here:
Invalid argument supplied for foreach()
(20 answers)
Closed 1 year ago.
I have had problema with "foreach"...
<?php
/*** user ***/
$sql = "SELECT * FROM user WHERE user_login = '$login' ";
$users = selecionar($sql);
foreach($users as $user) {
$userId = $user['user_id'];
}
?>
<?php
$sqll = "SELECT * FROM cadastro WHERE user_id = '$userId' ";
$cadastro = selecionar($sqll);
foreach($cadastro as $cad) { ?> /* Line 41 */
..... HTML
<?php } ?>
If I register something in PhpMyAdmin this code shows the register. But if there's not register in DB the page shows
Warning: Invalid argument supplied for foreach() in C:\wamp64\www\banheiromovel\02-listagem\listagem_perfil.php on line 41
It looks like selecionar() returns something that isn't iterable if there are no results, like maybe null or false. (Remember that if your function doesn't reach a return statement it's going to return null.)
I think your two best options are either
Wrap the foreach in a conditional to make sure it's not empty before you try to iterate it
if ($users) {
foreach($users as $user) {
$userId = $user['user_id'];
}
}
Modify selecionar() so that it always returns an array, but just returns an empty array if the query returns no results.
I prefer the second one, personally. You can do this by initializing whatever variable you're fetching your query results into in the function to an empty array, then returning that variable after you (possibly) fill it with data.
Like this:
function selecionar(string $sql): array
{
$result = [];
// code that executes a query and fetches results,
// adding the rows to $result if there are any
return $result;
}
Also, you should be executing those queries using prepared statements. Inserting input into the SQL string like that is not safe.
Try the following
/*** user ***/
$sql = "SELECT * FROM user WHERE user_login = '$login' ";
$users = selecionar($sql);
if ($users) {
foreach(array($users) as $user) {
$userId = $user['user_id'];
}
}
?>
I had the same problem and i resolved by adding an array() .
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 6 years ago.
I have a line of code in my php that reads:
$sel_venue = "SELECT 'char_type' FROM 'character_type_allowed' WHERE status='Open'";
$run_venue = mysqli_query($con,$sel_venue);
while ($row = mysqli_fetch_array($run_venue))
if ($row['char_type'] == 'Mortal')
{ print ("<li><a href='http://houston-by-night.com/sheets/create/mortal.php'>Create Mortal</a></li>"); }
The link associated with this does nothing. Zero interaction beyond acting likeit wants to expand. My error log produces this: Why is it asking for this?
[08-Aug-2016 23:28:41 America/New_York] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/houchat/public_html/incl/creation.php on line 8
You can't use ' as ticks for field/tablenames.
Your query is producing an error. You can see the error with mysqli_error($con).
Please see the corrected code below
$sel_venue = "SELECT `char_type` FROM `character_type_allowed` WHERE status='Open'";
$run_venue = mysqli_query($con,$sel_venue) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($run_venue)) {
if ($row['char_type'] === 'Mortal') {
print ("<li><a href='http://houston-by-night.com/sheets/create/mortal.php'>Create Mortal</a></li>");
}
}
Your query failed, so $run_venue is the boolean false instead of what you expect. You should check for errors before you use any query result. Do this:
$run_venue = mysqli_query(...);
if(!$run_venue) die(mysqli_error($con));
... //<- we get here if the query succeeded
You will see the error. The problem is that your SQL statement wraps the table name between single quotes 'character_type_allowed', instead of backticks (backtick is above the tab key on my keyboard)
This question already has an answer here:
MysQl error : Invalid parameter number
(1 answer)
Closed 8 years ago.
Its probley something small but i been looking at this for ages and still cant get it to work at all im getting two errors
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number in
the code i have is this:
public function getdata ($tran_id)
{
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback Where feedback.feedback_username = trade.user_name_of_buyer
AND user_name_of_buyer = :user_name_buyer ";
$sth = $this->db->prepare($sql);
$sth->execute(array(':tran_id' => $tran_id, ':user_name_buyer ' => $_SESSION['user_name']));
$user = $sth->fetch();
You're binding a :tran_id parameter during your call to execute, but you're not using that parameter in your query.
Change your execute line to this
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
Remove :tran_id from your parameter list or add a condition for that parameter. I hope this help.
Your select statement does not have a where clause for tran_id, either remove the tran_id from your execute call
$sth->execute(array(':user_name_buyer ' => $_SESSION['user_name']));
or add a extra where clause to your sql statement
$sql = "SELECT tran_id, seller_user_name, user_name_buyer
FROM trade_transaction, feedback
WHERE feedback.feedback_username = trade.user_name_of_buyer
AND tran_id = :tran_id
AND user_name_of_buyer = :user_name_buyer ";
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.
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've done a search and couldn't find anything that could specifically help me.
I was hoping you could help.
I'd like to execute a MySQL query which searches a table for entries which meet two criteria (type = green AND on = yes)
I am presented with: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /link/to/my/file.php on line 36
Here is an extract from the code (line 36):
`$green = "SELECT * FROM homepage_vars WHERE type = 'green' AND on = 'yes'";
$green = mysql_query($green);
$green = mysql_fetch_array($green);`
ON is a MySQL reserved keyword. If you use it as a column or table identifier, you need to enclose it in backquotes:
SELECT * FROM homepage_vars WHERE type = 'green' AND `on` = 'yes'
You'll then have another problem once the query syntax is corrected. You have overwritten the variable $green several times. Originally, it held your query SQL, but was then used for the query result resource. That's fine, but then you will overwrite it with the row fetched by mysql_fetch_array() and its contents will be an array or FALSE. Subsequent attempts to fetch rows will fail since $green is no longer a result resource.
Always test for query success or failure before attempting to fetch rows. Call mysql_error() to see the error reported by the MySQL server, which would have pointed to invalid syntax near 'on or something similar.
$green = "SELECT * FROM homepage_vars WHERE type = 'green' AND on = 'yes'";
$query = mysql_query($green);
if ($query) {
// Don't overwrite your query resource!
// Use a new variable!
$row = mysql_fetch_array($query);
}
else {
// Failure!
echo mysql_error();
}