I'm learning php and mysql at the moment and I'm working on getting my current website to be dynamic.
I've looked at some guides and worked on my own code and to my knowledge I believe it should work, but when I check it online it does nothing.
Is there anything obvious I have missed in the code below, and is there anyway to display errors in php which would help me discover what I've done wrong?
<?
function functions($method, $query) {
$con = mysql_connect("hostname","username","password");
mysql_select_db("databasename", $con) or die(mysql_error());
if($method = 'get') {
$result = mysql_query($query);
return $result;
echo $result;
}
if($method = 'send') {
$result = mysql_query($query);
}
}
$query = "SELECT * FROM 'content' where 'ID' = 1";
$heading = functions('get', $query);
?>
<h1><?php echo $heading;?></h1>
First enable error reporting. Add this line inside your php tags as the first line.
Then avoid using Short tags. Use
<?php ?>
tags. Sometimes short tags may have not enabled by your php configuration.Check whether anything is returned from your query. Whether it was successful or failed.
and comparison operator is == or ===. You have used the assign operator (=) inside your function. You need to fix that, otherwise nothing will return.
and when you
echo mysql_query();
it returns the resource only (if it was successful, otherwise false). You have to usemysql_fetch_array()
or to get data from the query before you echo.Finally, avoid using
mysql_*
extensions. They are deprecated. UseMySQLi
orPDO_MySQL
instead.