<?php
session_start();
$con=mysql_connect("localhost","root","samy");
mysql_select_db("project");
if($con)
{
echo "Connected Successfully ";
}
else
{
echo "Error" . $sql . "<br>" . mysql_connect_error();
}
$name=$_SESSION['name'];
echo $name;
$sql1 = mysql_query("select cust_id from registered_user where name ='.$name.' ");
$r = mysql_num_rows($sql1);
echo $r;
$row1 = mysql_fetch_array($sql1);
$cid = $row1['cust_id'];
echo $cid;
?>
Since num_rows is returning zero therefore $cid is also not printing.
Don't know what's the error;
You should remove the dot(.).
$sql1 = mysql_query("select cust_id from registered_user where name ='$name'");
^ ^
Also suggest to add error reporting like this
$sql1 = mysql_query("select cust_id from registered_user where name ='$name'")
or die(mysql_error());
Related
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'";
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.
im trying to query the database and apply code to each result and insert a record per result to a databse. This code works but only does it for the last result row. Ive tried foreach but cannot get it to work most likley because i dont understand how it should work. Please help and thank you
<?php
if(isset($_POST['bill1'])){
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require ('dbconnect.php');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
}
$invoicedate= $_POST['invoicedate'];
$invoiceduedate= $_POST['invoiceduedate'];
echo 'post is good';
require ('dbconnect.php');
echo 'db connect';
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customer WHERE terms = 1");
while($row = mysqli_fetch_array($result)) {
echo 'array good';
$item = $row['inputItem'];
$itemdescription = $row['description1'];
$itemprice = $row['itemprice1'];
$id = $row['id'];
$paidstatus = 5;
$totaldue = $itemprice;
require ('dbconnect.php');
echo $item;
echo $itemdescription;
echo $itemprice;
echo $id;
echo $paidstatus;
echo $invoicedate;
echo $invoiceduedate;
$resulttt = mysqli_query($con,"SELECT invoice_number FROM invoices ORDER BY invoice_number DESC LIMIT 1");
while($row = mysqli_fetch_array($resulttt)){
$addone = "1";
$invoicenewnumber = $addone + $row [invoice_number];
}
echo $invoicenewnumber;
//echo $row [invoice_number];
$invoice_number = $invoicenewnumber;
require ('dbconnect.php');
$put = mysqli_query($con,"INSERT INTO invoices (item, description, item_total, id, paidstatus, duedate, invoicedate, invoice_number, total_due)VALUES('$item', '$itemdescription', '$itemprice', '$id', '$paidstatus', '$invoiceduedate', '$invoicedate', '$invoice_number', '$totaldue')");
if (!mysqli_query($con,$put))
die('Error: ' . mysqli_error($con));
echo 'succsess';
}
?>
<?php
$resulttt = mysqli_query($con,"SELECT invoice_number FROM invoices ORDER BY invoice_number DESC LIMIT 1");
while($row = mysqli_fetch_array($resulttt))
{
$addone = "1";
$invoicenewnumber = $addone + $row [invoice_number];
$invoice_number = $invoicenewnumber;
$put = mysqli_query($con,"INSERT INTO invoices (item, description, item_total, id, paidstatus, duedate, invoicedate, invoice_number, total_due)VALUES('$item', '$itemdescription', '$itemprice', '$id', '$paidstatus', '$invoiceduedate', '$invoicedate', '$invoice_number', '$totaldue')");
}
?>
Try this insert the second query in the while loop.
And read the comment where ever you find them in the Answer
Note:You need the connection only one time in whole page.
<?php
if(isset($_POST['bill1']))
{mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require ('dbconnect.php'); You need the connection only once
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}}
$invoicedate= $_POST['invoicedate'];
$invoiceduedate= $_POST['invoiceduedate'];
echo 'post is good';
//require ('dbconnect.php'); Dont need here
// echo 'db connect'; Dont need here
//if (mysqli_connect_errno()) {
// echo "Failed to connect to MySQL: " . mysqli_connect_error();
// }
$result = mysqli_query($con,"SELECT * FROM customer WHERE terms = 1");
while($row = mysqli_fetch_array($result)) {
echo 'array good';
$item = $row['inputItem'];
$itemdescription = $row['description1'];
$itemprice = $row['itemprice1'];
$id = $row['id'];
$paidstatus = 5;
$totaldue = $itemprice;
require ('dbconnect.php');
echo $item;
echo $itemdescription;
echo $itemprice;
echo $id;
echo $paidstatus;
echo $invoicedate;
echo $invoiceduedate;
$resulttt = mysqli_query($con,"SELECT invoice_number FROM invoices ORDER BY invoice_number DESC LIMIT 1");
while($row = mysqli_fetch_array($resulttt)){
$addone=$addone+1;//If you want to add one to the invoice number you have to use this.
$invoicenewnumber = $addone + $row ['invoice_number'];//}//remove the closing while loop from here,
//Added quete to the Invoice_number
echo $invoicenewnumber;
//echo $row [invoice_number];
$invoice_number = $invoicenewnumber;
require ('dbconnect.php');
$put = mysqli_query($con,"INSERT INTO invoices (item, description, item_total, id, paidstatus, duedate, invoicedate, invoice_number, total_due)VALUES('$item', '$itemdescription', '$itemprice', '$id', '$paidstatus', '$invoiceduedate', '$invoicedate', '$invoice_number', '$totaldue')");
}
if (!mysqli_query($con,$put))
die('Error: ' . mysqli_error($con));
echo 'succsess';
}
?>
function getDepartmentAndCondition($dep, $userid, $cond) {
$result = mysql_query("SELECT * FROM department WHERE ID='$dep'");
while($row = mysql_fetch_array($result))
{
$DepConInfo['Department'] = $row['Department'];
}
$userName = mysql_query("SELECT * FROM users WHERE FacebookID = '$userid'") or die ("<hr>error in SQL query: " . mysql_error() . "<hr>");
while($row = mysql_fetch_array($username)) {
$DepConInfo['Name'] = $row['name'];
}
$result2 = mysql_query("SELECT * FROM condition WHERE ID= '$cond' ")
or die("<hr>error in SQL query: " . mysql_error() . "<hr>");
while($row2 = mysql_fetch_array($result2))
{
$DepConInfo['Condition'] = $row2['Condition'];
}
return $DepConInfo;
}
$dep, $userid, and $cond are all ints. the first one $DepConInfo['Department'] is returning the right string, but the other two fail with the error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ...
ok I rewrote the function
function getCondition($cond) {
$query = "SELECT * FROM condition WHERE ID = '$cond' ";
$sql = mysql_query($query);
if (!$sql) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row2 = mysql_fetch_array($sql))
{
$condition = $row2['Name'];
}
return $condition;
}
but I'm still getting an error:
Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition WHERE id = '1'' at line 1 Whole query: SELECT * FROM condition WHERE ID = '1'
the table "condition" has two columns "ID" and "Name".
while($row = mysql_fetch_array($username)) {
PHP is case-sensitive: you have $username with wrong caps - should be $userName
Additionally, based on your naming convention in the first and third queries
$DepConInfo['Name'] = $row['name'];
is probably incorrect and should be capitalized as $row['Name']
function getDepartment($dep) {
$sql = "SELECT * FROM department WHERE ID = '$dep'";
$result = mysql_query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
$row = mysql_fetch_row($result);
$department = $row['Department'];
}
return $department;
}
function getName($userid) {
$sql = "SELECT * FROM users WHERE FacebookID = '$userid'";
$result = mysql_query($sql);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
$row = mysql_fetch_row($result);
$user_name = $row['Name'];
}
return $username;
}
function getCondition($cond) {
$sql = "SELECT * FROM condition WHERE id = '$cond'";
$result = mysql_query($sql);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
$row = mysql_fetch_row($result);
$condition = $row['Condition'];
}
return $condition;
}
$department = getDepartment($dep);
$username = getName($userid);
$condition = getCondition($cond);
I'm writing this from my head so I did not test it, but it should work or at least get you on your way. If not let me know. Mind capitalization using caps in your dbase table and column names can make things more confusing. Use $sql to store your query, use $result to store the result. This is more descriptive. Good luck!
My first page to delete queries selected by user query.php which is working absolutely fine:
<form method=post action="delete.php">
List of queries<br/>
<?php
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("testdb") or die(mysql_error());
echo "<br />";
$query = "select * from queries ";
$result = mysql_query($query) or die(mysql_error());
$count=mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
print "<input type='checkbox' name='Query[]' value=\"".$row['queryId']."\"> ";
echo " ". $row['name']." ". $row["address"]." ". $row["contactNo"]."
". $row["query"];
echo "<br />";
}
?>
<input type="submit" value="Delete" name="Delete">
<br/>
</form>
I've tried with following codes for second page delete.php but nothing seems to work.
Code1:
<?php
if($_POST['Delete'])
{
if(count($_POST['checkbox']) > 0) {
foreach($_POST['checkbox'] as $checkbox)
{
$del_id=$checkbox;
$sql = "DELETE * FROM queries WHERE `queryId`= '$del_id'";
$result = mysql_query($sql);
mysql_error();
}
echo "Selected Rows deleted";
} else {
$NEW="Nothing to Delete";
}
}
?>
Code2:
<?php
if(($_POST['Delete']))
{
$count=array();
$count=$_POST['checkbox'];
for($i=0;$i<count($count);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM queries WHERE queryId='$del_id' ";
$result = mysql_query($sql);
}
$NEW="Selected records Deleted";
}
var_dump($_POST['checkbox']);
var_dump($count);
?>
Your checkbox names are "Query", but you're accessing it as $_POST['checkbox']. This should be $_POST['Query'] instead.
EDIT checking from your updated code:
if($_POST['Delete']) {
if(count($_POST['Query']) > 0) {
foreach($_POST['Query'] as $checkbox) {
$del_id=$checkbox;
$sql = "DELETE * FROM queries WHERE queryId= '$del_id'";
$result = mysql_query($sql);
mysql_error();
}
echo "Selected Rows deleted";
}
else {
$NEW="Nothing to Delete";
}
}
Instead of this:
$del_id=$checkbox;
do this:
// if queryId is numeric
$del_id=intval($checkbox);
This makes sure that the value you're working with is numeric, instead of potential malicious input from your user. I'm going under the assumption that queryId is numeric. If it's not, then you need to do this:
// if queryId is not numeric:
$del_id = mysql_real_escape_string($checkbox);
Your DELETE syntax is incorrect:
$sql = "DELETE * FROM queries WHERE queryId= '$del_id'";
You want just DELETE FROM. Also if the value for queryId is numeric, you don't need the quotes around it:
$sql = "DELETE FROM `queries` WHERE `queryId` = $del_id";
Finally, your MySQL error call doesn't do anything useful as is:
mysql_error();
Here's how you should do this, along with the rest of the code:
if($_POST['Delete']) {
if(count($_POST['Query']) > 0) {
foreach($_POST['Query'] as $checkbox) {
$del_id= intval($checkbox);
$sql = "DELETE FROM `queries` WHERE `queryId` = $del_id";
$result = mysql_query($sql);
if(!$result) {
echo "There was an error in the query: " . mysql_error();
}
}
echo "Selected Rows deleted";
}
else {
$NEW="Nothing to Delete";
}
}