mysql_fetch_assoc very strange [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
okay so here is my code.
<?php
include 'connect.php';
$id = addslashes($_REQUEST['id']);
$image = "SELECT * FROM images WHERE id=$id";
$result = mysql_query($image);
$imagearray = mysql_fetch_assoc($result);
$realimage = $imagearray['image'];
header("Content-type: image/jpeg");
echo $realimage;
?>
the page uses GET to get the id but then it gives me this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a6289454/public_html/get.php on line 9

var_dump(mysql_error()); will help you find the error message. But most likely thing is you need quotes:
$image = "SELECT * FROM `images` WHERE `id`='".$id."'";

I think the problem might be in addslashes(), try mysql_real_escape_string() instead.

Why are you adding slashes to $_REQUEST['id']? It should be an integer
Try this code:
if(is_numeric($_REQUEST['id'])){
$q = "SELECT * FROM images WHERE id = " . $id; // Don't put variables in strings!
$rs= mysql_query($q) or die("There is an error in the query:<br>" . $q . "<br><br>The error is: ". mysql_error()); // Throw and error if there is one.
$result = mysql_fetch_assoc($rs);
} else { die('Not a valid ID'); }
Also for good practice use require_once('connect.php'); rather than include 'connect.php';

$image = "SELECT * FROM images WHERE id=$id";
$result = mysql_query($image) or die(mysql_error());
Try that
Also for your $id use type casting it's a simple solution to a simple problem
$id = (int) $_REQUEST['id'];

Related

Illegal mix of collations (ascii_general_ci,IMPLICIT) and (latin1_swedish_ci,COERCIBLE)

<?php
include 'db_connect.php';
$name = $_REQUEST['sname'];
$email = $_REQUEST['email'];
$query = "SELECT * FROM info_enrol_student WHERE ES_name = '$name' AND ES_EMAIL = '$email'";
$in = mysqli_query($conn,$query) or die(mysqli_error($conn));
$count = mysqli_num_rows($in);
if (mysqli_num_rows($in) > 0)
{
header('location:index.php');
exit;
}
?>
I am making a login form and giving data to this php code but getting this error.
Illegal mix of collations (ascii_general_ci,IMPLICIT) and
(latin1_swedish_ci,COERCIBLE) for operation '=' in php
I am not able to understand the error because earlier with the same script I was able to login in the system.
Based on the error stated by you you need to do following:-
Go to the database table and change the collation of these two fields (ES_name and ES_EMAIL) to utf-8-bin and change your query like below:-
$query = "SELECT * FROM info_enrol_student WHERE `ES_name` = $name AND `ES_EMAIL` = $email"; `

php warning why ? unable to solve it [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 8 years ago.
I am getting a warning message which I don't understand why and unable to resolve, (see below)
Warning: Supplied argument is not a valid MySQL result resource in /detail.php on line 34
Here is my code:
$rs = mysql_query($strSQL);
$strSQL = "SELECT * FROM <tablename> WHERE id=" . $_GET["serviceName"];
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) **(line 34) here ***
{
echo $row['ID']."<br />";
echo $row['serviceName']."<br />";
// Close the database connection
mysql_close();
?>
</dl>
<p>Return to the list</p>
</body>
</html>
thanks in advance, I am not getting any of the data on this webpage either,thanks...singhy
The query is failing - you need to wrap quotes around strings in MySQL:
$strSQL = "SELECT * FROM gu_service_cat WHERE id = '" .
$_GET["serviceName"] . "'";
plus, the $rs should be BELOW the $strSQL...
You need to do it like this
$strSQL = "SELECT * FROM gu_service_cat WHERE id=" . $_GET["serviceName"];
$rs = mysql_query($strSQL);
Because before setting value to the variable you are using it in the query. This is why it is throwing the error.

gettingerror as Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 9 years ago.
<?php
{
session_start();
include "dbconnect.php";
$target1=$_SESSION['target1'];
echo $target1;
$query = "SELECT * FROM userpictures where pictures = $target1";
$result = mysql_query($query);
var_dump($result);
while($row=mysql_fetch_object($result))
echo $row;
{
$_SESSION['picid1']=$row['picid'];
//$_SESSION['picid1']=$row->picid;
echo $_SESSION['picid1'];
}
}
?>
it is returning me the output as
images/2101.jpgbool(false)
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource
it is giving the target1 but not the picid
please help.
Try this, Added ' - pictures = '$target1'
$query = "SELECT * FROM userpictures where pictures = '$target1' ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_object($result))
{
$_SESSION['picid1']=$row->picid;
echo $_SESSION['picid1'];
}
Use,
$row->picid;
instead of
$row['picid']
Note: Use mysqli_* fucntions or PDO instead of mysql_* functions(deprecated)

PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
(6 answers)
Closed 9 years ago.
After uploading the site to my web server I get this message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
in /home1/m1k3ey/public_html/MikeyDev.com/teamdesire/playersheet.php on line 8
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
in /home1/m1k3ey/public_html/MikeyDev.com/teamdesire/playersheet.php on line 9
My PHP version is:
PHP Version 5.2.17
I cant see where im going wrong in my code, can anyone please assist:
mysql_select_db('teamdesire', $link);
$query = "SELECT * FROM playershowercase";
$result = mysql_query($query,$link);
$row = array();
Line 8 > while($row[] = mysql_fetch_array($result));
Line 9 > $count = mysql_num_rows($result);
$random = rand(0,$count-1);
Maybe something like the following?
mysql_select_db('teamdesire', $link);
$query = "SELECT * FROM playershowercase";
$result = mysql_query($query,$link);
$row = mysql_fetch_assoc($result);
$count = mysql_num_rows($result);
$random = rand(0,$count-1);
Or
mysql_select_db('teamdesire', $link);
$query = "SELECT * FROM playershowercase";
$result = mysql_query($query,$link);
while ($row = mysql_fetch_array($result, MYSQL_NUM))
$count = mysql_num_rows($result);
$random = rand(0,$count-1);
Possible error sources:
Db is not running on specified socket or port.
Insufficient permissions on the table or database.
Incorrect login credentials.
Table does not exist or is misspelled.
Echo line per line when you troubleshoot.
is_resource($link) or die('Could not connect');
mysql_query(...);
echo mysql_error();
Try this:
mysql_select_db('teamdesire', $link);
$query = "SELECT * FROM playershowercase";
$result = mysql_query($query,$link);
$row = array();
while($result = mysql_fetch_array($result))
{
$row[]=$result;
}
$count = mysql_num_rows($result);
$random = rand(0,$count-1);

Why can not I fetch data from mysql db table with mysqli_query? [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 6 years ago.
I am at prototype stage so I have error_reporting(-1); at my 1st row. Despite this, I have no php error but php prints 'could not get data'.
As I understood from php.net manual and stackoverflow similar cases, my $sorgula returns FALSE. But why? Can you help, regards
//i am sure that i am connected to db
if ($sorgula = mysqli_query($dbc, "SELECT * FROM tb_yazilar ORDER BY kolon_sn"))
{
while ($satir = mysqli_fetch_array($sorgula, MYSQLI_ASSOC))
{
echo $satir['kolon_yazar'].' - '.$satir['kolon_baslik'].' - '.$satir['kolon_yazi'].' - '.$satir['kolon_etiketler'].' - '.$satir['kolon_ytarihi'].' - - - - ';
}
}
else
{
echo 'could not get data';
}
mysqli_close($dbc);
try to use mysqli_error in your code .
procedural example:
$sorgula = mysqli_query($dbc, "SELECT * FROM tb_yazilar ORDER BY kolon_sn")
or error_log(mysqli_error($dbc));
I used this and it worked: without the if, once it extracts, go back and add the if. :)
require 'db.php';
$query = "SELECT * FROM thoughts";
$result = mysqli_query($conn, $query);
while($row=mysqli_fetch_assoc($result)) {
echo "<td>" . "TEXT: ". $row['text'] . "</td>";
}
mysqli_close($conn);

Categories