mysql can not compare data and parse it with php - 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.

Related

trying to check if user exists with php

hi i am trying to make a php script that checks if a user is in the database only it gives me the error
mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\stage\userExist.php on line 22
below is the code
<?php
$link = mysqli_connect('localhost', 'root', '', 'k3462_top-tree');
$query = "SELECT * FROM users";
$userName = $_GET['userName'];
$result = mysqli_query($link, $query);
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr><td>" . $row['userName'] . "</td><td>" . $row['email'] . "</td></tr>";
}
echo "</table>";
$query1 = "SELECT * FROM users WHERE userName = $userName";
$result1 = mysqli_query($link, $query1);
if(mysqli_num_rows($result1)>=1){
echo "user exists";
}
else {
echo "user doesnt exist";
}
mysqli_close($link);
?>
You have a syntax error in your query that fill the variable $result1 with false. That occurs your error. Try my example below. I just added ' around $userName in your query and an if statement around your mysqli_num_rows function.
$link = mysqli_connect('localhost', 'root', '', 'k3462_top-tree');
$query = "SELECT * FROM users";
$userName = $_GET['userName'];
$result = mysqli_query($link, $query);
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr><td>" . $row['userName'] . "</td><td>" . $row['email'] . "</td></tr>";
}
echo "</table>";
$query1 = "SELECT * FROM users WHERE userName = '$userName'";
$result1 = mysqli_query($link, $query1);
if( $result1 ) {
if(mysqli_num_rows($result1)>=1){
echo "user exists";
}
else {
echo "user doesnt exist";
}
} else {
echo "query or db error";
}
mysqli_close($link);
$query1 = "SELECT * FROM users WHERE userName = '$userName'";

If 0 items in DB table, return error

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.

Displaying ALL data from sql table in PHP?

When I print my code it only prints the question and description of id = 1 but not the rest of the table.
here is my code.
Please show me how to print my entire table which has like 20 questions or so...and also please show me how to make it so that the questions stay on the browser (even when I refresh the page) because currently the data does not stay on the browser when i refresh the page.
Thanks So Much!
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
} else {
echo "failed to fetch array";
}
}
?>
You need a for each loop:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
if( $result = $conn->query($query)) {
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
} else {
echo "failed to fetch array";
}
}
?>
All I've done there is change:
$fetch = $result->fetch_assoc();
echo "<p>{$fetch['question']}</p>";
echo "<p>{$fetch['description']}</p>";
to:
$fetch = mysql_fetch_array($result, MYSQL_ASSOC);
foreach($fetch as $ques) {
echo "<p>" . $ques['question'] . "</p>";
echo "<p>" . $ques['description'] . "</p>";
}
fetch_assoc() — Fetch a result row as an associative array
so it gets only 1 row you need to loop through the rest of the rows check the examples reference from php docs

mysql_query not returning data

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

Mixing html with php search results?

I am trying to make the different the different rows have line breaks but its not working.
How is this done!? Please check my code below
Thanks guys!
James
<?php
$conn = mysql_connect("", "", "");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
{
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
}
if (!mysql_select_db("")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT name,lastname,email
FROM test_mysql
WHERE name LIKE '$search%' AND lastname LIKE '$searchterm'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["name"];
echo $row["lastname"];
echo $row["email"];
}
mysql_free_result($result);
?>
<?php echo $row["name"];?>
<br>
<?php echo $row["lastname"];?>
<br>
<?php echo $row["email"];?>
Beats me what you find so hard about it:
while ($row = mysql_fetch_array(...)) {
echo ...
echo '<br>';
}

Categories