$con = mysql_connect("localhost","username","password","db name") or die('not connected');
echo "connected";
$sql = "SELECT id, first_name, last_name FROM reg-users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
}
}
else
{
echo "0 results";
}
I have used above code. But not working. mysql select query is not working. Please give me solution.
Change the following line of code
$result = $conn->query($sql);
with
$result = $con->query($sql);
As you have $con as mysql connection object.
And also try to learn basic select queries from http://www.tutorialspoint.com/
Do the following changes in the code:
Your code should be:
$con = mysqli_connect("localhost","username","password","db name") or die('not connected');
echo "connected";
$sql = "SELECT id, first_name, last_name FROM reg-users";
$result = $con->query($sql); // <<-- please note spelling mistake in $con
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
}
}
else
{
echo "0 results";
}