I'm getting the following two errors. Can someone tell me where I'm going wrong.
[Error 1]mysqli_query() expects at least 2 parameters, 1 given
[Error 2]mysqli_fetch_object() expects parameter 1 to be mysqli_result, null given{"projects":[]}
Here is my code, I'm trying to return JSON data with table row names from mysqli query.
$databaseName = "laravel";
$tableName = "projects";
// 1) Connect to mysql database
$con = mysqli_connect('localhost','root','root', 'laravel');
$dbs = mysqli_select_db($con, 'laravel') or die(mysqli_error($con));
$return = new stdClass();
$return->projects = array();
// 2) Query database for data
$result = mysqli_query("SELECT * FROM projects");
//echo mysql_errno($link) . ": " . mysql_error($link). "
";
if($result !== false) {
while($row = mysqli_fetch_object($result)){
$return->projects[] = $row;
}
}
echo json_encode($return);
?>
When you call the mysqli_query() the first parameter needs to be the connection. So here is how you are suppose to call it:
mysqli_query($con,"SELECT * FROM projects")
// 1) Connect to mysql database
$con = mysqli_connect('localhost','root','root', 'laravel');
$return = new stdClass();
$return->projects = array();
// 2) Query database for data
$result = mysqli_query($con,"SELECT * FROM projects");
//echo mysql_errno($link) . ": " . mysql_error($link). "
";
if($result !== false) {
while($row = mysqli_fetch_object($result)){
$return->projects[] = $row;
}
}
echo json_encode($return);