PHP MySQL is not getting anything from the database - php

Trying to get all the users from my database, yet when I do so, the query fails.
Attempting to do so with
$userNames = mysqli_query($con, "SELECT * FROM Login");
Where in PHPMyAdmin the database has a few records in the table login.
I checked if the connection is connected, its connected.
Is there any reason this wouldn't work?
Here is a examplepage, you can try logging in with Username and Password.
EDIT: From the example page, here is the code used to test:
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error() . "<br />";
} else {
echo "Connected to MySQL!<br />";
}
if(mysqli_query($con, "SELECT * FROM Login")){
echo "Query good!<br />";
} else {
echo "Query bad!<br />";
}
EDIT 2: Here is a screenshot of the table existing, and the data existing:

I use this
$con = StartDatabase("localhost", "username", "password", "mydatabase");
function StartDatabase($dblocation, $dbuser, $dbpasswd, $dbname){
$link = mysqli_connect($dblocation, $dbuser, $dbpasswd, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
return $link;
}
if(mysqli_query($con, "SELECT * FROM Login")){
echo "Query good!<br />";
} else {
echo "Query bad!<br />";
}

Try adding this to the else of the query to get an error output:
echo "Error code ({$con->errno}): {$con->error}";

Try like this:
$con = new mysqli("pdb1.awardspace.com", "*******", "****", "*******");
$query = "SELECT * FROM Login";
if ( !$con->query($query) ) {
echo "Query bad!<br />";
echo mysqli_connect_error() . "<br />";
echo "Error code ({$sql->errno}): {$sql->error}";
} else {
echo "Query good!<br />";
}
$con->close();

Related

Can PHP or mysqli capture equivalent of mysql console responses? [duplicate]

This is the code I'm using for deleting row from my DB:
<?php
$eid = $_GET['eid'];
$con = mysqli_connect("localhost", "root", "","project") or die("Connection failed");
echo "connection is done";
$query = "delete from exam where eid='$eid'";
if ($con->query($query)==TRUE)
{
echo " record deleted";
}
else
{
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
?>
The else statement is not getting executed. It displays "record deleted" for every value even if the value is not found in the database.
Why is this happening? how can I verify that my record has been deleted from my DB?
You can use mysqli.affected-rows.
Consider the following:
$query="delete from exam where eid='$eid'";
if ($con->query($query)==TRUE && $con->affected_rows > 0) {
echo " record deleted";
} else {
echo "Error: " . $query . "<br>" . $con->error;
}

How to fix blank MySQL queries after hosting provider upgraded to PHP 7?

Host updated the environment to PHP 7. Had to update db.php and added the following:
<?php
include('administrator/configuration.php');
//echo DATABASE;
//mysql_select_db(DATABASE, mysql_connect(HOSTNAME, DBUSER, DBPASS));
$con = mysqli_connect(HOSTNAME, DBUSER, DBPASS, DATABASE) or die("Error message...");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$con) {
"Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
printf ("Success: A proper connection to MySQL was made! The ???? database is great." . PHP_EOL);
printf("Host information: " . mysqli_get_host_info($con) . PHP_EOL);
echo "<p><hr />";
try {
//$query = sprintf("SELECT database() AS the_db");
$query = sprintf("SELECT COUNT(*) FROM albums");
/* return name of current default database */
if ($result = mysqli_query($con, $query)) {
$row = mysqli_fetch_row($result);
//printf("Default database is %s.\n", $row[0]);
printf($row[0]);
}
//throw new Exception();
} catch (Exception $e) {
print "Something went wrong /n" . $e;
} finally {
// do nothing
}
?>
As per How to change mysql to mysqli?
, I tried making the change mysql_query( -> mysqli_query($con, .
Navigating to ~/administrator/login.php results in a blank page. I edited ~/public_html/administrator/lib/connection.php - with the above change, nothing.
I'm guessing a WordPress upgrade to start then take it from there.
Any other suggestions?

I am not getting the result properly when conneting to MySQL

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();
}

Search Using PHP displays all Database Data

I am trying to search some data from a database. The search works fine, however if I click on search without entering anything into the form, it displays all the data on the database. Anyway I can fix this?
This is my php code.
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
$searchKeyword = $_POST['find']; // Sanitize this value first !!
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
//}
?>
Just make sure $searchKeyword has a (valid) value
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"AnimalTracker1");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error($link));
}
// checks to see if $_POST['find'] is actually set.
if ( array_key_exists('find',$_POST) )
{
$searchKeyword = $_POST['find']; // Sanitize this value first !!
// sanitize $searchKeyword here
}
// checks to see if $searchKeyword has no value, or just contains empty space
if ( empty(trim($searchKeyword)) )
{
echo "You must enter a search term";
}
else
{
$sql=mysqli_query($link, "Select * FROM Locations WHERE `Animal_Type` LIKE '%$searchKeyword%' ");
if ($sql == FALSE)
{
die($sql." Error on query: ".mysqli_error($link));
}
while($result = mysqli_fetch_array($sql))
{
echo $result ['Animal_Type'];
echo "<br>";
echo $result ['Latitude'];
echo "<br> ";
echo $result ['Longitude'];
echo " <br>";
echo $result ['Seen'];
echo " <br> ";
echo $result ['Time'];
echo "<br> ";
echo "<br> ";
}
}
?>
Try removing the "%" from either the back or the front of the $searchKeyword in the query and I guess it should do the work.
"%" is used when match all or none. So if you send an empty string it will return the whole database.

PHP-mysql_fetch_array return nothing

I've trying to display values from mysql but it return any empty page. The connection is fine but it does not fetch the data from mysql. I tried all the answers from the similar questions asked. But nothing helped. Can somebody please help me? This is the code
$con= mysql_connect($host, $username, $pwd);
if(!$con)
die("not connected". mysql_errno());
echo(Connected);
mysql_select_db("info",$con);
$query="select * from people";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['people_name'];
echo "<br />";
}
Try to check if your db user,password are correct! I test the code above :
<?php $con=mysqli_connect("localhost","root","","test"); // Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " -- " . $row['people_name']; echo "<br>";
}
?>
and give me the result without error: 10 -- JOHN 11 -- PRADEEP
I just change mysql_connect to mysqli_connect add in $con= mysql_connect($host, $username, $pwd); a dbname. and $con become $con= mysqli_connect($host, $username, $pwd,$dbname); I use mysqli_query instead of mysql_query. Here is a stackQuestion for the mysql vs mysqli in php which can explain you the difference.
Try this
<?php
$con= mysql_connect('hostname', 'username', 'password');
if(!$con)
die("not connected". mysql_errno());
echo("Connected");
mysql_select_db("test",$con);
$query="select * from tabale_name";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
?>
check this
<?php
$con=mysqli_connect("hostname","username","password","info");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>
OR
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if ($con)
{
echo "connected to db";
}
else
{
echo "not connected to db";
}
$db_selected = mysql_select_db("info", $con);
if (!$db_selected)
{
die ("Can\'t use info: " . mysql_error());
}
$result = mysqli_query("SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>

Categories