Thursday, 14 December 2017

php - Warning function mysql_query() and mysql_fetch_array() using TCPDF

itemprop="text">

I'm trying to print data from mysql
but get these error
:




Warning:
mysql_query() expects parameter 1 to be string, resource given in
..

Warning: mysql_fetch_array() expects parameter 1 to be resource,
null given in ..

TCPDF ERROR: Some data has already been output,
can't send PDF file


I
have learn from these following links but still get
warning:






This
is the code:




$con=mysql_connect('localhost','root','','bkd_rev');
$sql = 'select * from
tbl';
$result = mysql_query($con,$sql);

if($result ===
FALSE) {
die(mysql_error());
}



while($row = mysql_fetch_array($result))
{
$id =
$row['id'];
$nam = $row['name'];
$tbl .= '

'.$id.''.$nam.'

';
}



Answer




The right syntax for
mysql_query is the opposite of your. From
documentation



mixed mysql_query (
string $query [, resource $link_identifier = NULL ]
)


So you need to
change to



$result =
mysql_query($sql,$con);



Since
connection link is not required if you only use a database connection you migh not use
it



$result =
mysql_query($sql);


You
might need to select your database after
connection



bool mysql_select_db (
string $database_name [, resource $link_identifier = NULL ]
)



As side
note i would advise you to switch to either href="http://it2.php.net/manual/en/book.pdo.php"
rel="nofollow">PDO or
rel="nofollow">mysqli
since rel="nofollow">mysql_* api are deprecated and soon
will be no longer mantained


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...