We are currently running into an issue when we try and get the username from a SQL database. This is our code for the query and the fetch_object()
<?php
$user = 'root';
$password = 'root';
$db = 'collabowrite';
$host = 'localhost';
$port = 3306;
$conn = new mysqli($host, $user, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $link = mysqli_init();
// $success = mysqli_real_connect(
// $link,
// $host,
// $user,
// $password,
// $db,
// $port
// );
//$sql='SELECT * FROM users WHERE username ='.$_GET['username'];
$result = $conn->query('SELECT * FROM users WHERE username ="'.$_POST['username']).'"';
if(!$result){
die($conn->error);
}
$userids = array();
while ($row = $result->fetch_object()){
$userids[] = $row->id;
}
echo $userids[0];
$conn->close();
?>
Fatal error: Uncaught Error: Call to a member function fetch_object() on string in /Users/brianleaf/Google Drive/htdocs/login.php:34 Stack trace: #0 {main} thrown in /Users/brianleaf/Google Drive/htdocs/login.php on line 34
It looks like you have the quote on the outside of that last )
on line 34 rather than on the inside.
Try, for example:
$sql='SELECT * FROM users WHERE username ="'.$_POST['username'].'"';
$result = $conn->query($sql);
or
$result = $conn->query('SELECT * FROM users WHERE username ="'.$_POST['username'].'"');