I'm pretty new to php, and I'm teaching myself. I've looked at a few different resources, and the php script I have now doesn't return any critical errors when executed, but its not returning the data from the table.
<?php
$connect = mysqli_connect("localhost","*","*","*");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
$fetch = mysqli_fetch_array($rs);
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
echo $fetch;
mysqli_close($connect);
echo "hello";
?>
you have double entry:
$fetch = mysqli_fetch_array($rs); //<--- remove this as you are calling it again in the while loop
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
Check this
$connect = mysqli_connect("localhost","turlough","samus1","comments");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
if($rs)
{
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
}
else
{
// no results from query
}
mysqli_close($connect);
}
Related
I got an HTTP ERROR 500 whiles attempting to run this code in Nginx. My aim is to read test_parameter from the test database and print it on screen in the browser. I can't figure out what is wrong and any help will be appreciated, thank you
<?php
$conn = new mysqli("localhost", "test_user", "t3$tp#ss", "testdb", 3306);
if ($conn) {
$sql = "SELECT * FROM test_table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "ID: " . $row["id"] . " VALUE: " . $row["test_parameter"];
}
else {
echo "No rows to display";
}
else {
echo "Could not connect to MySQL";
}
$conn->close();
?>
I have a MySQL table and i would like to delete a row in my table and after deleting the row the result must show all data left in my table .
<?php
include 'Connection.php';
// Create connection
$con= mysqli_connect($host,$user,$pass,$db);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT * FROM productos";
$result = $con->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc())
{
$tem = $row;
$json = json_encode(array("productos"=>$tem));
}
} else {
echo "No Results Found.";
}
echo $json;
$con->close();
?>
This is my select code... now i don't know how create the delete function..
Any help please.
You are overwritting, make array of rows and then echo json_encode at last
$items = array();
if ($result->num_rows >0) {
while($row = $result->fetch_assoc())
{
$items[] = $row;
}
} else {
echo "No Results Found.";
}
echo json_encode(array("productos"=>$items));
$con->close();
I am looking for a way to display an error message if there is nothing listed in the table.
I have a photos table.
If this tables is empty, id like to echo something.
else, show the pictures.
inside of that table I have
id, name, url
id = id
name = name of image
url = url of image.
If there are no rows, we have an error.
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
mysql_fetch_array($query1);
if(empty($query1)) {
echo "nothing";
} else {
echo "good";
}
Try this,
$query = "SELECT * FROM photos";
$result= mysql_query($query);
$length= mysql_num_rows($result);
if($length>0)
{
while($rows = mysql_fetch_array($result))
{
echo $rows['name'];
echo "<img src='$rows[url]' />";
}
}
else
{
echo "Nothing to display";
}
Hope this will work
What about something like...
$sql = "SELECT COUNT(*) AS amountPhotos FROM photos";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row["amountPhotos"] == 0) {
echo "There are no photos in the photo table.";
}
or
$sql = "SELECT * FROM photos LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "There are no photos in the photo table.";
}
Try this
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
$result = mysql_fetch_array($query1);
if(empty($result)) {
echo "nothing";
} else {
echo "good";
}
This pretty much sums up the answer for this question: http://www.w3schools.com/php/php_mysql_select.asp
They even provided a sample code:
<?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 FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>
Just modify this and you'll be good to go.
For the code below added, i am not getting any result printed.
$con = #mysqli_connect("localhost","root","","temp");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * FROM `login`";
echo $query;
$result=#mysqli_query($query) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
echo $row["username"];
}
Try the below code it will work
//conection:
$con = mysqli_connect("localhost","root","","temp") or die("Error " . mysqli_error($con));
//consultation:
$query = "SELECT * FROM login" or die("Error in the consult.." . mysqli_error($con));
//execute the query.
$result = $con->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
Use this code as it is.
$con=mysqli_connect("localhost","root","","temp");
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result))
{
echo $row["username"];
}
// use this code and plz check your db name
$host='localhost';
$user='root';
$pass='';
$db_name='temp';
$con=mysqli_connect($host,$user,$pass,$db_name);
if($con)
{
echo "db connect succecssfully";
}
$slt="select * from login";
$query=mysqli_query($slt,$con);
while($row=mysqli_fetch_array($query))
{
echo $row["username"];
}
<?php
$con=mysqli_connect("localhost","root","","temp");
// Here localhost is host name, root is username, password is empty and temp is database name.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
mysqli_close($con);
?>
Use this. it may solve your problem.
//connection
$con = mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
The table Users contains data but still it shows Records Not Found
<?php
$conn = mysql_connect("localhost", "root", "pass", "Assign1");
$records = mysql_query($conn, "select * from Users");
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($records))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
mysql_close($conn);
?>
You have the parameters to mysql_query reversed. It should be:
$records = mysql_query("select * from Users", $conn);
Your other issue is with the if statement. You're checking if on a query, not on a result set.
Also, I'm sure you probably know but mysql libraries are deprecated and are being removed. You should really learn to use mysqli functions as they will be far more useful to you in the future.
Link to MySQLi documentation - It's really no harder than mysql libraries.
To re-implement in correct libraries:
<?php
$mysqli = new mysqli("localhost", "user", "pass", "database");
$query = $mysqli->query("SELECT * FROM users");
$results = $query->fetch_assoc();
if($results) {
foreach($results as $row) {
echo $row['name'] . " " . $row['pwd'] . "<br/>";
}
} else {
echo "No results found.";
}
?>
Hopefully I didn't just do your whole assignment for you, but it'd probably be worth it to get one more person using mysqli properly.
You have a wrong usage of mysql_query function
use it like this:
<?php
$conn = mysql_connect("localhost", "root", "pass","Assign1");
$result = mysql_query("select * from Users", $conn);
if(!$records)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
mysql_close($conn);
?>
Lets resolve this issue first.The error it was actually showing is no database selected you have to select the database that needs the code
mysql_select_db("Assign1",$conn);
Hope this code will perfectly sole your issue .Try it once .........
<?php
$conn = mysql_connect("localhost", "root", "pass");
mysql_select_db("Assign1",$conn);
$result = mysql_query("select * from users", $conn);
if(!$result)
{
echo "No Records Found";
exit();
}
while($row = mysql_fetch_array($result))
{
echo $row[0]['name'];
echo "<br />";
}
mysql_close($conn);
?>
here you go
<?php
$conn = mysql_connect("localhost", "root", "pass", "Assign1");
mysql_select_db(' ----your-database-here---', $conn ) ;
$records = mysql_query($conn, "select * from Users");
if(mysql_num_rows($records) > 0 )
{
while($row = mysql_fetch_array($records))
{
echo $row['name'] . " " . $row['pwd'];
echo "<br />";
}
}else
{
echo "No Records Found";
exit();
}
mysql_close($conn);
?>