Display Results By Date - php

$sql = mysqli_query($conn, "SELECT * FROM stock WHERE Date = '$date'");
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
$d="D-";
// output data of each row
echo "<table><tr><th>ID</th><th>Date</th><th>Product Code</th><th>Description</th><th>Delivery Order</th><th>Cartons</th><th>Items</th><th>Quantity</th><th>Sent_To</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["Date"]. "</td><td>" . $row["Product_Code"]. "</td><td>" . $row["Description"]. "</td><td>".$d. $row["DEO"]. "</td><td>" . $row["Cartons"]. "</td><td>" . $row["Items"]. "</td><td>" . $row["Quantity"]. "</td><td>" . $row["Sent_To"]. "</td></tr>";
}
echo "</table>";
Hello Guys When i echo $date it prints the date correctly but when i run this query i get an error
Warning: mysqli::query() expects parameter 1 to be string, object
given in C:\xampp\htdocs\rmt\displaydate.php on line 9
Notice: Trying to get property of non-object in
C:\xampp\htdocs\rmt\displaydate.php on line 11
Please help me to fix this error. Thanks in advance.

You're calling mysqli_query followed by $conn->query on the result from the first query. You only need to call one of them, for example;
$sql = "SELECT * FROM stock WHERE Date = '$date'";
$result = $conn->query($sql);

Related

pulling data from 2 mysql databases wrong type

I'm trying to pull data from my databases into a html table, everything worked fine until I tried introducing another database to add another variable to the table.
here is my code(just for context):
<?php
require 'dbh-inc.php';
$id = $_SESSION['userId'];
if(isset($id) == false)
{
header("Location:../account.php?faliure");
exit();
}
$sql = "SELECT product, productAmount, dateOfPurchase, productStatus, userAddress FROM sales;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0){
while($row = mysqli_fetch_assoc($result)){
$sql1 = "SELECT eLink FROM products WHERE $row\['product'\];";
$result1 = mysqli_query($conn, $sql1);
$resultCheck1 = mysqli_num_rows($result1);
if($resultCheck1 == 0){
$row1['eLink'] = " ";
}
else
$row1['eLink'] = mysqli_fetch_assoc($result1);
echo "<tr><td>". $row['product'] ."</td><td>" . $row['productAmount']. "</td><td>". $row['dateOfPurchase']. "</td><td>". $row['userAddress']. "</td><td>". $row1['eLink'] . "</td><td>" .$row['productStatus']. "</td></tr>" ;
}
}
else
echo '<div>no product purcheses yet</div>';
exit();
?>
the problematic lines are:
$sql1 = "SELECT eLink FROM products WHERE $row\['product'\];";
$result1 = mysqli_query($conn, $sql1);
$resultCheck1 = mysqli_num_rows($result1);
if($resultCheck1 == 0){
$row1['eLink'] = " ";
}
else
$row1['eLink'] = mysqli_fetch_assoc($result1);
the errors I get are:
Notice: Array to string conversion in C:\xampp\htdocs\coupleslove php\includes\manage_sales-inc.php on line 20
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\coupleslove php\includes\manage_sales-inc.php on line 22
Notice: Array to string conversion in C:\xampp\htdocs\coupleslove php\includes\manage_sales-inc.php on line 20
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\coupleslove php\includes\manage_sales-inc.php on line 22
What is wrong with the code?
I understand that it has to do with inserting a wrong type to a function, although a couple lines before I do the same and everything is fine, I'm kind of confuzed with what is wrong with the code. is this a syntax problem?
thank you in advance!
I tried to explain in the comments but it is too complex.
You should always use Prepared Statements to prevent SQL injection, and use valid SQL queries. So this line:
$sql1 = "SELECT eLink FROM products WHERE $row\['product'\];";
Should be converted to (Assuming the products table has a numeric key named id)
$stmt = $conn->prepare("SELECT eLink FROM products WHERE id = ?");
$stmt->bind_param("i", $row['product']);
$stmt->execute();
$result1 = $stmt->get_result();
if($result1->num_rows === 0) {
$eLink = ' ';
} else {
$row1 = $result1->fetch_assoc();
$eLink = $row1['eLink'];
}
Then you can output the $eLink variable like this:
echo "<tr><td>". $row['product'] ."</td><td>" . $row['productAmount']. "</td><td>". $row['dateOfPurchase']. "</td><td>". $row['userAddress']. "</td><td>". $eLink . "</td><td>" .$row['productStatus']. "</td></tr>" ;

how can i get it put out something from a other database?

What is wrong about this code??
echo "<table>";
echo "<tr><td>Machine Nummer</td><td>Eigenaar</td><td>Status</td><td>Locatie</td></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['machineid'] . "</td><td> ". $eigenaar=mysql_query("SELECT userName FROM users WHERE userId =" . $row['eigenaar'] ."") ."</td><td>" . $row['status'] . "</td><td>" . $row['locatie'] . "</td></tr>";
}
echo "</table>";
$res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
$query = "SELECT * FROM machines WHERE locatie='".$_SESSION['location']."'"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
I want it to output the eigenaar name which equals to the userId but i only get the output:Resource id #9 Resource id #10 Resource id #11.
The answer is to use the INNER JOIN function.

Msql Update query inside a select query using php

I have following code. With a single select query and an update query.It is working fine when i remove the update query. When i run following complete code then nothing happens.
Please help me I want to update table with every cycle of select query. Is there any way to execute following code.
$query = "SELECT * FROM ab_rec WHERE username='$userid'" or die(mysql_error());
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
$t_name=$row['testname'];
$first_url=$row['first_url'];
$thanks_url=$row['thanks_url'];
$start_date=$row['start_date'];
$parse_first_url = parse_url($first_url); //parsing URL of first page for removing main domain name from it.
$parse_thanks_url = parse_url($thanks_url);
$final_first_url = $parse_first_url['path'] ; //Finally parsed URLs are stored into new variables
$final_thanks_url = $parse_thanks_url['path'] ;
$row['unique_visits'] = calculate_visitors($final_first_url, $start_date);
$row['conversions']= calculate_visitors($final_thanks_url, $start_date);
$row['conversion_percent'] = ($conversions/$unique_visits_first)*100;
$query1="UPDATE `ab`.`ab_rec` SET unique_visits=$row['unique_visits'], conversions=$row['conversions'] , conversion_percent=$row['conversion_percent'], WHERE testname=$row['testname'] " or die(mysql_error());
$result2=mysql_query($query1, $connection);
echo "<tr><td>" . $checkbox . "</td><td>" ."<a href='my_test.php?test_name=$t_name'>".$row['testname'] . "</a></td><td>" . $row['date_of_creation'] . "</td><td>" . $row['unique_visits'] . "</td><td>" . $row['conversions'] . "</td><td>" . $row['conversion_percent'] ."%". "</td></tr>"; //$row['index'] the index here is a field name
}
This:
$query1="UPDATE `ab`.`ab_rec` SET unique_visits=$row['unique_visits'],
conversions=$row['conversions'] , conversion_percent=$row['conversion_percent'],
WHERE testname=$row['testname'] " or die(mysql_error());
$result2=mysql_query($query1, $connection);
Should be:
$query1="UPDATE `ab`.`ab_rec` SET unique_visits=$row['unique_visits'],
conversions=$row['conversions'] , conversion_percent=$row['conversion_percent']
WHERE testname='{$row['testname']}'";
echo $query1; //POST THIS RESULT
$result2=mysql_query($query1, $connection) or die(mysql_error());

Warning on mysql_fetch_array()

Sorry to waste your time but I'm trying to store data from DB table into arrays and display in a table. I keep getting the same error. I've changed the "'s and removed variables. Still I get
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3656574/public_html/Home.php on line 41
<?php
if ($_POST['search_projects']){
$con= mysql_connect("host","username","password","a3656574_opacmin") or die ('Error: ' . mysql_error());
$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql);
while($row= mysql_fetch_array($result))
{
$Date =$row['AccessDate'];
$Key=$row['keyWord'];
$Count=$row['count'];
echo "<tr>";
echo "<td>" .$Date ."</td> ". " <td>" . $Key. " </td>" . " <td>" . $Count. " </td>";
echo "</tr>";
}
}
?>
I don't know how to fix this. Can someone please help?
Mysql connection function receive 3 arguments (mysql_server, mysql_user, mysql_password)
and you should select database using mysql_select_db(database, connection_resource_id);
Also make sure your credential
Try:
$con= mysql_connect("host","username","password");
mysql_select_db("a3656574_opacmin",$con);
0) To begin, I would urge you to start using PDO instead of mysql_connect (and friends), as the latter is being deprecated. There's tutorial to help you start in this migration here.
1) I'm not sure what it is you're expecting the 4th argument to mysql_connect() to do for you. Per the PHP documentation, this should be a boolean value:
mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false...
2) Check for error conditions before moving on to mysql_fetch_array():
$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql);
if (! $result) {
// use something like mysql_error() to find out why it failed, log it, etc.
} else {
while( ... ) { ... }
}
Firstly I would like to recommend you to start using "mysqli". You can look for this here
and then you should first check if your credentials are right. If that is right got to phpmyadmin and try your query out there and see if its working fine. Maybe you are missing something. Good luck.
Please check your database credentials and then try with:
<?php
if ($_POST['search_projects'] && !empty($_POST['search']))
{
$con= mysql_connect("host.com","opacmin","password","opacmin") or die ('Error: ' . mysql_error());
$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql);
while($row= mysql_fetch_array($result))
{
$Date =$row['AccessDate'];
$Key=$row['keyWord'];
$Count=$row['count'];
echo "<tr>";
echo "<td>" .$Date ."</td> ". " <td>" . $Key. " </td>" . " <td>" . $Count. " </td>";
echo "</tr>";
}
}
?>

Multiple While Loops within a While Loop?

Total newbie trying to learn... thanks for all the help so far!
UPDATE - updated code below - Thanks Phill
I'm now trying to get the relevant horse information from a table horses which I can join to race_form with the horse_name field. I want to then display the information for each horse in the race below the while($racecard) info. Does this make it a bit clearer?
I thought I could just do another query and then a while loop with mysql_fetch_array below the one I have already and that would display it and then move onto the next race, but that apparently doesn't work...?!
I'm wondering if you can run 2 while loops after each other inside a while loop? I am working on a horse racing formguide - I can display each race list, but underneath I want to display individual horse details on each horse in the race. Anyway if you look at this page you can see what I'm trying to do:
http://tasmanianracing.com/superform/formguide.php?meetingID=21042011LAUN&Submit=View+Form
Problem is, any time I put a second while loop after the race list, it won't show up. I'm trying to run a query to get the horse details from my horse table and then run a while for each horse in the race, but nothing shows up.
I hope this is clear enough ... my snippet of code is below - please let me know if I've missed out something important:
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $row['race_number'] . " at " . $row['start_time'] . " " . $row['class'] . " over " . $row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $row['raceID'] . "')");
while($row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $row['number'] . "</td><td>" . $row['past10'] . "</td><td>" . $row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
echo "</table>";
You'll probably need to change the names of some of the $row variables, they may interfere with each other.
For example:
while($row_races = mysql_fetch_array($totalraces)){
while($row_details = mysql_fetch_array($racedetails)){
while($row_card = mysql_fetch_array($racecard)){
I think you can get rid of one of your queries:
The first query gets the number of races by selecting a COUNT, This returns one record with the count value.
// This returns one record with the count
$total_races = "SELECT COUNT(race_number) AS totalraces
FROM race_info WHERE meetingID = ('".$safemeetingID."') ";
Next you iterate over the the same records as the rows returned for the race details are the same as the count.
// This looks to return the record(s) with the race details
$race_details = "SELECT * FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')";
I think you can just use this to get the desired results: (I agree to rename the $row variable(s) to something descriptive for each while loop)
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
NOT TESTED/PSEUDO CODE
"SELECT *
FROM horses AS h,
INNER JOIN race_info AS ri ON race_form.raceID = race_info.raceID
WHERE horse_name IN (
SELECT horse_name
FROM race_form AS srf
WHERE h.horse_name = srf.horse_name
)
AND race_form.raceID = ('" . $details_row['raceID'] . "')"
The idea is to join the two queries into one, I know this is not the correct syntax but it might give you an idea on how to go about it.
Or you can do another query while loop for the horse names
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
$horses = mysql_query("SELECT *
FROM horses
WHERE horse_name = ('" . $rc_row['horse_name'] . "')");
while($horse_row = mysql_fetch_array($horses))
{
// echo horse details here
}
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
I theory you could and in practice you can, but a while in a while in a for in a while seems a little bit over the top...
I'm sure we can help you make it more efficient if you explain what it is you're trying to do.

Categories