mysql_query not returning data - php

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);
?>

Related

Reading Test from browser

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

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>";
}
?>

mysql can not compare data and parse it with php

i'm getting back to make some php code to make a login/register form.
Account data is stored in mysql database and I've used that code to validate the md5 and parse it trough the browser. But there's something wrong 'cause it doesn't work.
<?php
if (!$x) {
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
} else {
$con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
$db = mysql_select_db('dbname', $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
$num = $result1->num_rows;
if($num == 0){
//if not display an error message
echo "<script> alert('Usuario inexistente') </script>";
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
}else{
while($row=mysql_fetch_object($result1)){
$userName=$row->username;
echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
echo "[ " . $userName . " ]\n\n";
}
}
}
?>
I hope you can understand and I hope you can help me. I'm unhappy.
thanks
Hope it helps you, $num = mysql_num_rows($result); instead of $num = $result1->num_rows;
mysql_fetch_object($result) instead of mysql_fetch_object($result1)
<?php
if (!$x) {
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
} else {
$con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
$db = mysql_select_db('dbname', $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
$num = mysql_num_rows($result);
if($num == 0){
//if not display an error message
echo "<script> alert('Usuario inexistente');document.location.href='index.php';</script>";
}else{
while($row=mysql_fetch_object($result)){
$userName=$row->username;
echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
echo "[ " . $userName . " ]\n\n";
}
}
}
?>
Note: mysql_* functions deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
Working code:
<?php
if (!isset($x)) {
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
} else {
$con = mysql_connect('localhost', 'userdb', 'pwd') or die(mysql_error());
$db = mysql_select_db('dbname', $con) or die(mysql_error());
$result = mysql_query("SELECT * FROM `users` WHERE `md5pwd` = '". $x ."'");
$num = mysql_num_rows($result);
if($num == 0){
//if not display an error message
echo "<script> alert('Usuario inexistente') </script>";
echo "<script type='text/javascript'>document.location.href='index.php';</script>";
}else{
while($row=mysql_fetch_object($result)){
$userName=$row->username;
echo "<script> alert('BIENVENIDO " . $userName . "') </script>";
echo "[ " . $userName . " ]\n\n";
}
}
}
?>
PS: mysql_extension will be removed in the future, use mysqli or PDO...btw mysqli in procedural mode it's very similar to mysql extension.

trouble retrieving data from sql database with php

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

Categories