php not displaying results on database - php

I have a database with 4 tables. (products,purchase,customer,user). When I tried to display all rows in products, no results. But in the user table, it displays. What should be the problem? Is it on my database?tables?php code?
Here's my code:
<?php
$db = mysqli_connect("localhost","root","", "prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sql = mysqli_query($db, "select * from user");
if( $sql === FALSE ) {
die('Query failed returning error: '. mysqli_error());
} else {
while($row=mysqli_fetch_array($sql))
{
echo $row['username']. "<br>";
}
}
?>
Hope you could help me.

Did you check the letters uppercase and lowercase in table columns?

You have inventory in your code, but in your question you named the table products, Is it this simple?

echo $row['item']. "<br>";
What is 'item'? Shouldn't you be selecting them by $row[0] or $row['products'].. or one of your other columns.

check your variables, for your example, try renaming your vairable $sql to something else, because you might have a similar variable somewhere in your code that you did not show.
try this :
<?php
$db = mysqli_connect("localhost","root","", "prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sqlstackoverflow = mysqli_query($db, "select * from user");
if($sqlstackoverflow === FALSE ) {
die('Query failed returning error: '. mysqli_error());
} else {
while($row=mysqli_fetch_array($sqlstackoverflow))
{
echo $row['username']. "<br>";
}
}
?>

Your code references an inventory table but you said your database has a products table (and no inventory table). Because of this, the query is failing.

Just Try With The Following :
<?php
$db = mysqli_connect("localhost","root","","prodpurchase");
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
$sql = mysqli_query($db,"select * from user");
if( $sql === FALSE ) {
die('Query failed returning error: '. mysqli_error());
} else {
while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC))
{
echo $row['username']."<br>";
}
}
?>
I think this may help you to resolve your problem.

Related

Moving deleted records to another table

I am trying to move the records from table studentrecords to passivestudents but i am not being able to. so far i've tried.
<?php
$db = new mysqli("localhost", "root", "","learndb");
if ($db->connect_error) {
die("Connection failed this is the error: " . $db->connect_error);
}
$id=$_GET['id'];
$sql="INSERT INTO passivestudents VALUES (DELETE FROM studentrecords WHERE id=$id)";
$result=$db->query($sql);
if(!$result)
{
echo"ERROR MOVING";
}
else
{
echo "<center><p style=\"color:green\">Information Moved!</p></center>";
}
?>
your query was wrong . you want to remove student with some id ? true ?
so follow these step:
1-INSERT INTO your_sec_table (select * from first_Table where id= $id;
2-DELETE FROM first_table WHERE id=$id

how to passed value from mysql database in php

I am new in php so please help me here
I have one page for collect customer details and i did successfully
from databse table i want to pass 2 value
1- amount 2- personid
Table-
CREATE TABLE personal_info(
MTrackid bigint(20) AUTO_INCREMENT PRIMARY KEY,
MAmount varchar(50)
i write mysql query to pass value
<?php
$con = mysql_connect("localhost","testdb","root#123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("testdb", $con);
$con = mysql_connect("localhost","testdb","root#123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("testdb", $con);
$result = mysql_query("SELECT MTrackid,MAmount FROM personal_info");
while($rowval = mysql_fetch_array($result))
{
$trackid = isset($_GET['MTrackid']) ? $_GET['MTrackid'] : '';
}
mysql_close($con);
?>
and this is the code where i want to pass and show value
$TranTrackid=isset($_POST['MTrackid']) ? $_POST['MTrackid'] : '';
So please tell me where i am wrong and how to pass it and who where i want..
Thank in advance
I think what you want is to save the value from the SQL.
Then you should replace $_GET with $rowval. Because in $rowval will be your mysql result.
while($rowval = mysql_fetch_array($result))
{
if(isset($rowval['MTrackid'])) {
$trackid = $rowval['MTrackid'];
echo $trackid;
}
}

How to detect whether users have accepted terms?

Basically I am writing a PHP and MYSQL script that will check whether a user has accepted the terms and conditions or not. In the databse every current user that has signed up is set to "unaccepted". When they log in the first page that they are directed to should have a scirpt on it that detects whether or not the status of the tos column in the users table is set to "accepted" or "unaccepted". If it is accepted they can continue, and if it is not they they will be forced to go to a page and accept them before they can continue to use the rest of my site. This is the code so far but it doesn't seem to be working. Any suggestions help.
<?php
$username=$_SESSION['username'];
$connect = mysql_connect('**', '**', '**', '**');
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('**'))
{
die('Could not select database: ' . mysql_error());
}
$toschecker = mysql_query("SELECT `tos` FROM `users` WHERE `username` = '$username'");
if (!$toschecker)
{
die('Could not query:' . mysql_error());
}
mysql_close($connect);
$unaccepted='unaccepted';
if ($toschecker === $unaccepted)
{
header('Location: accepttos.php');
}
?>
For some reason this isn't directing them to the accepttos.php page. Thanks in advance.
Change MySQL to MySQLi. Explanations are in the comments.
<?php
$username = $_SESSION['username'];
$connect = mysqli_connect('Host', 'Username', 'Password', 'Database');
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
$toschecker = mysqli_query($connect,"SELECT `tos` FROM `users` WHERE `username` = '$username'"); /* SELECT TOS COLUMN */
while ($row = mysqli_fetch_array($toschecker))
{
$tos = $row['tos']; /* STORE TO A VARIABLE THE FETCHED TOS */
}
$unaccepted = 'unaccepted';
if ($tos == $unaccepted) /* COMPARE THE TOS VARIABLE IF UNACCEPTED */
{
header('Location: accepttos.php');
}
else {
header('Location: acceptedTOS.php'); /* IF TOS IS ACCEPTED. CHANGE THE LOCATION */
}
mysqli_close($connect);
?>
$toschecker is a resource , try the following under the mysql_query line to see the result of your query, which you can use to redirect accepttos.php etc..
if( $row = mysql_fetch_assoc($toschecker)) {
var_export($row['tos']);
}
You need to access the information in the fields of your database. Here is an example.
$toschecker = mysql_query($sql);
$row = mysql_fetch_array($toschecker);
$tos = $row['tos'];
if($tos == 'accepted'){
// go to regular page
}else {
// go to accept terms page
}

Check To See A Match In MySql

I have a form which has a textbox with an attribute called ref. once this is submitted, it updates on of my fields in the database. I have this code working and fine but what i need now is for it to check if the data entered into the textbox exists in the database and if it does, then it should notify the user to choose another reference. here is my code for the php end:
$ref = mysql_real_escape_string($_REQUEST['ref']);
$id = $_GET['public'];
$con = mysql_connect("localhost", "*****", "******");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('*****', $con);
$sql = "UPDATE public SET ref = '$ref' WHERE public_id = '$id'";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
echo '<hr><h2>Reference Number Has Been Assigned Successfully</h2><hr>';
}
any ideas guys?
thanks
You can get the number of rows affected:
$rowsAffected = mysql_affected_rows($con);
if($rowsAffected) {
//something WAS changed!
}
else {
//NOTHING was changed ... :-(
}
Also I would watch out for Bobby Tables
You might want to use mysqli or PDO's prepared queries for what you want to do.
Based on OP's comment below:
...
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
$rowsAffected = mysql_affected_rows($con);
if($rowsAffected) {
echo '<hr><h2>Reference Number Has Been Assigned Successfully</h2><hr>';
}
else {
//show some error message?
}
}
In this case First you run a select command to search for the record with particular reference number. If the result is eof , then run insert command. If not EOF then send a warning to the user saying reference number exist and choose another one.

Inserting a null value into SQL if form field blank

I'm trying to add a null value into a database if the form field is left blank but not getting much luck...
Can anyone see where this is going wrong??
<?php
$con = mysql_connect("ipaddress","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if (is_null($_POST["event_sub"]) || $_POST["event_sub"]=="") {
$event_sub = NULL;
} else {
$event_sub = mysql_real_escape_string($_POST["event_sub"]);
}
$sql="INSERT INTO myTable (event_sub)
VALUES
(". $event_sub .")";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
Perhaps try this:
if (is_null($_POST["event_sub"]) || $_POST["event_sub"]=="") {
$event_sub = 'NULL';
} else {
$event_sub = mysql_real_escape_string($_POST["event_sub"]);
}
Putting the NULL in single quotes will result in the word NULL being inserted into your query rather than an actual NULL, which is nothing.
why would you need to search for the word NULL? Just leave the insert for that column blank. and make sure allow null is set in the table..

Categories