I'm trying to read from my database and display the info in my browser but it shows up a blank page,how do i fix this?
This is my html form:
This is my php script:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit'])){
$sql = "SELECT * FROM mydb";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>";
}
} else {
echo "0 results";
}
}
$conn->close();
?>
Thank you in advance
Related
I have a page that shows some of the information in the database. I'd like to add a link in each row (like making the first name a link) that I can click on that brings me to a page that shows the rest of that row's info (like a profile page). I'm thinking of making a link that passes the id to the profile page so that the profile page can gather the info.
I'm sure this is simple, but I'm just not getting it. How do I make the link show up in each row that sends only that row's id number? Because I'd rather not have to go into each row and make a special link.
Here is the code I have:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$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["FirstName"]. "
" . $row["LastName"]. "
" . $row["Phone"]. "
" . $row["Email"]. " <br>";
}
} else {
echo "0 results";
}
$conn->close();
?>```
Based on your example you could do this:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$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["FirstName"] . " " . $row["LastName"] . " " . $row["Phone"] . " " . $row["Email"] . " <br/>";
}
} else {
echo "0 results";
}
$conn->close();
?>
That profile.php page would check if the id is set with isset and query the data based on id simular to this: PHP & MYSQL: Select from where id=$id
Not tested and make sure to sanitize any user-generated variables.
I'm a bit new to MySQL and I am trying to create a search system. I am using some simple PHP code from w3schools.
I connect to my database:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "img";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then echo the results.
$sql = "SELECT ID, Name, Description, Path FROM system";
$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["Name"]. " " . $row["Description"]. " " . $row["Path"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My database contains one entry, but when I run this code, "0 Results" is echoed to the screen. Is there something I am doing wrong here?
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I think, I might need help with my PHP code...
I'm trying to echo the info in a MySQL database and it gives me the Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /usr/local/ampps/www/php/db.php on line 18
My code is:
<?php
$servername = "blabla"; //changed, connecting works
$username = "blabla";
$password = "blabla";
$database = "blabla";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = mysql_query("SELECT * FROM Schueler");
while($data = mysql_fetch_array($sql)) //This is line 18...
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
$conn->close();
?>
Would be nice if somebody could help me :)
Edit:
I fixed it using MySqli only, this is my working code now:
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connection success \n";
}
$sql = "SELECT ID, Vorname, Nachname FROM Schueler";
$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["Vorname"]. " " . $row["Nachname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
Thanks for the quick advice.
You have used mysqli for your connection setup, then you used simple mysql functions to get the resultset and the fetch_array logic. You need to be uniform in this case.
I have changed the mysql_query() call to mysqli_query() call, and similarly the mysql_fetch_array() call to mysqli_fetch_array() call.
The final code becomes:
$sql = mysqli_query("SELECT * FROM Schueler", $conn);
while($data = mysqli_fetch_array($sql))
{
echo "ID: " . $data['ID'] . " Vorname: " . $data['Vorname'] . " Nachname: " . $data['Nachname'] . " Klasse: " . $data['Klasse'] . "<br>";
}
I have a mysql database. There are categories in Db for my articles. I want to fetch these categories and display them as a list (side navigation). I was able to establish connection and print the categories but can't put them into the list.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voltage";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$sql = "SELECT ID, NAME FROM categs";
$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["NAME"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I know it's a dumb question, but I'm PHP newbie.
<ul>
<li>Categ 1</li>
<li>Categ 2</li>
<li>Categ 3</li>
</ul>
Thanks in advance.
you nearly got it by yourself... try this one:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "voltage";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully <br>";
$sql = "SELECT ID, NAME FROM categs";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<ul>";
while($row = $result->fetch_assoc()) {
echo "<li>" .$row["NAME"]. "</li>";
}
echo "</ul>";
} else {
echo "0 results";
}
$conn->close();
?>
replace
echo "ID: " . $row["ID"]. " " . "Name: " . $row["NAME"]. "<br>";
with
echo "<li>" . $row["NAME"]. "</li>";
Im working on a project at the moment and I have succesfully made a simple php web site that lists data from a sql server. Is it possible to use the data from the server and insert each row into a bootstrap 'Panel with heading' Box
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname, ooureason, reg_date FROM MyGuests";
$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["firstname"]. " " . $row["lastname"]. " Reason: " . $row["ooureason"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>