I'm confused right now.
I've been coding for some while now, to fix a problem, but I'm going blind on how to do, can anyone help me?
First, I have an datatable with all my customers in, then I click "USE" I should go to another page where I can make an case. This is all good, if I only should use one table, but in my project I need more then 1 table, so my question is:
$sql = "SELECT customers.cust_id,
customers.customer_name, numberplate.car
FROM customers , numberplate WHERE SESSION = numberplate.cust_id";
But I can't see how to do it, I know how to make a profilepage and so on, but this ting really tricks my brian right now. Anyone to help?
My session look like this:
if (isset($_GET['cust_id']) && $_GET['cust_id'] != "") {
$id = $_GET['cust_id'];
} else {
$cust_id = $_SESSION['cust_id'];
}
You're confusing your SQL with PHP.
$_SESSION is the variable which contains PHP's session data.
but your MYSQL statement: "WHERE SESSION = numberplate..." is referencing a column in the mysql table.
You want to use sql like:
SELECT customers.cust_id,
customers.customer_name, numberplate.car
FROM customers , numberplate WHERE numberplate.cust_id = ?";
and then bind the value of $_SESSION['cust_id'] to the database call.
But additionally, you're trying to JOIN two tables together without any details on how to do that...
so your SQL then becomes:
SELECT customers.cust_id, customers.customer_name,
numberplate.car
FROM customers,
JOIN numberplate ON customers.cust_id = numberplate.cust_id
WHERE numberplate.cust_id = ?
Related
Hey guys how's it going?
Ok so i'm having trouble writing a small script and i can't seem to understand what its not working my reasoning is:
update all the tech_kunena_messages.subject with tech_kunena_topics.subjects where tech_kunena_messages.thread and tech_kunena_topics.id are the same...
the script i've come up with is this:
$con = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_selectdb($db,$con) or die(mysql_error());
mysql_query("UPDATE tech_kunena_messages
SET tech_kunena_messages.subject=tech_kunena_topics.subject
FROM tech_kunena_messages
JOIN tech_kunena_topics ON tech_kunena_messages.thread = tech_kunena_topics.id");
echo "Done Pease check the data base";
mysql_close($con);
?>
But for some reason tech_kunena_messages.subject is not getting updated with any thing, this is like my third time writing a script and i don't have the reflex to see what is wrong for me it should work ( i also tried with INNER JOIN but nothing happend either oh the code is beeing read as when i navigate to the scripte i get the please check the database..
any ideas?
Thanks guys
Alex
Your UPDATE query should look like below
UPDATE tech_kunena_messages tkm
JOIN tech_kunena_topics tkt
ON tkm.thread = tkt.id
SET tkm.subject= tkt.subject
I'll admit, I don't know tons about MySQL queries or JSON for that matter but I would like to. Currently when I get data from my database I query one table which brings values into another query and so on. I don't think this is the best practice to go about receiving all the data I need to display. Below is an example of how my MySQL queries currently work in a PHP foreach function:
So, is there a better way of completing the query(s) I want and how would I be able to encode it as JSON?
Any help would be appreciated, thank you.
From what I understand, your code is doing something like this:
<?php
$friends = query("SELECT Friends");
while($row = fetch_object($friends)){
$friend_dets = query("SELECT Friend_dets WHERE Friend_ID = $row->Friend_ID");
$output[] = fetch_assoc($friend_dets);
}
echo json_encode($output);
?>
With this, you're making the process more complex than it needs to be. You can get all of the information you need with one query with a JOIN like this:
SELECT Name, Status, WhateverElseYouWant
FROM Friends
JOIN Profiles ON (Friends.friend-profile-id = Profiles.profile-id)
WHERE Friends.profile-id = MyCurrentProfileID
That will give you the name, status and whatever else of everyone who is friends with MyCurrentProfileID. Then, you just need to put the result in an array and json_encode it:
<?php
$friends = query($QueryFromAbove);
while($row = fetch_object($friends)){
$output[] = fetch_assoc($friend_dets);
}
echo json_encode($output);
?>
You can make 1 SQL query for this list, can you tell me whats your table Friends and Profiles info.
In Friends table you must have id to profiles to indicate is the friens of what profile.
The query may be like : select profile.* from profile, friends, where friends.id_profile = profile.id and profile.id = current-profile-id
In your graph you use current profile id in friends table and not profile this may be error
I have a PHP results page which starts off "first-pass" with ALL rows returned. It's a search listing of all pizza places in the county.
SELECT * from pizzeria;
Then the user can drill down into more detail... the page also has a CSS dropdown menu where the user can pick a specific neighborhood (which carries a URL):
href="samepage.php?neighborhood=HELLSKITCHEN"
which then changes the query after I pick up the $_GET[]
SELECT * from pizzaria WHERE nbh=(the $_GET[] variable sent in the URL);
but I'd like the page to call itself and I have header("Cache-Control:no-cache"); at the top.
I'm trying to create a first-pass or first visit flag variable with the isnull() function:
if (is_null($firstpass)) {
$query = SELECT all the records from the pizzaria table
} else {
$query = SELECT only the records WHERE I $_GET[] the value from the reloaded URL
}
It seems though that the $firstpass variable doesn't stick on reloads. Should I SESSION that variable? (though still have the problem of constantly resetting it)
Or maybe implement some other approach?
I know I can redirect to a separate second page and javascript back to this page to avoid "headers already sent", but I want to avoid the round-trip back to the client.
Is there a known best practice on reloads with new info? Kinda new to PHP here. thanks
Maybe I didn't understand well your problem but why wouldn't you do :
if (!isset($_GET['example'])) {
$query = 'SELECT * FROM pizzerias';
} else {
$query = 'SELECT * FROM pizzerias WHERE pizzeria = \'.mysql_real_escape_string($_GET['example']).\' LIMIT 1';
}
at the first pass because, it seem that the $_GET variable is set only when the user choose a pizzeria?
Here is a more targeted answer.
NOTICE: mysql_* functions are being depreciated, so use PDO instead. In my example I'm being semi-lazy and not using PDO.
//Connect to database and define table up here
...
if(!isset($_GET['neighborhood')){
$q = "SELECT * FROM pizzeria;";
}else{
$q = sprintf("SELECT * FROM pizzeria WHERE nbh=%s",mysql_real_escape_string($_GET['neighborhood']));
}
$query = mysql_query($q);
foreach($row = mysql_fetch_array($query,MYSQL_ASSOC){
//display the updated view of restaurants.
}
I would also suggest that you use jQuery for that Web 2.0 effect. It's really nice when you select from a drop-down menu and things magically move without a page reload.
I'm really struggling to get my code to work, and I can't figure out why.
I have a database in my PHPMyAdmin and it contains 2 tables:
tennisCourts
courtID
courtName
bookingFee
tenniscourts_Availability
courtID
court_dateBooked
I am writing a PHP program using PEAR repository, and I am struggling to create code that allows me to:
Display All courtNames and their corresponding bookingFee BASED ON users search date ONLY IF the courtName is not already booked by another user.
Here is my current code:-
$CHOSEN_BOOKING_DATE = $_GET['user_dateField']; //GET's input data from user form in my other html document.
$database->setFetchMode(MDB2_FETCHMODE_ASSOC);
$myQuery = "SELECT * FROM tennisCourts, tenniscourts_Availability WHERE court_dateBooked != $CHOSEN_BOOKING_DATE";
$queryResult =& $db->query($myQuery);
if (PEAR::isError($queryResult)) {
die($queryResult->getMessage());
}
echo '<table>';
while ($Col =& $queryResult->fetchRow()) {
echo '<td>'.$queryResult['courtName'].'</td>';
echo '<td>'.$queryResult['bookingFee'].'</td>';
echo '<td>'.$queryResult['court_dateBooked'].'</td>';
}
?>
The code above displays all the courtNames and BookingFee's for All court_dateBooked fields in my database. I cant get it to display the courtNames and bookingFee only if it isn't booked. If it is booked it should return "sorry no courts found on this date".
I am still new to PHP and SQL so forgive me if I have not made myself clear. I have been researching online and various sources say to use SQL UNION OR JOIN? Could someone please enlighten me on how they could be used in context to my scenario? I really appreciate any help. Thank you for checking out my question.
Try this:
$myQuery = "
SELECT c.courtName
, c.bookingFee
, a.court_dateBooked
FROM tennisCourts AS c
LEFT JOIN tenniscourts_Availability AS a ON c.id = a.courtID
WHERE a.court_dateBooked != '" . $CHOSEN_BOOKING_DATE ."'";
Make sure you sanitize and escape $CHOSEN_BOOKING_DATE properly before executing your query. Since it looks like you're using PDO you should be using prepared statements for this.
need some enlightment here, and this is my first post here.
I would like to call and use my previously created mysql view using php... is it possible?
or in another words,
i'm wondering....can we OR how do we call mysql view, that we already created using php? to reduce long query coding
standard generic codes as follow :
$sql= " SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
$db->QueryArray($sql);
while ($row = $db->Row()) {
echo $row->city_name. " - " . $row->tarrif1 . "<br />\n";
}
now for the MYSQL VIEWS :
$sql= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
Pure MYSQL command :
query: SELECT * FROM shipsummary
IN PHP :
$sql = i'm badly stuck here...please help
How do we access it using php.
thanks before
Addition 1:
ok... let me rewrite the example :
$sql1= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
$sql2= "SELECT * FROM shipsummary";
$db->QueryArray($sql2);
$sql2 can not see shipsummary VIEW, coz it's already in a different var
how to utilise and then execute $sql1 ? & $sql2?
The process is the same in PHP - a MySQL view is seen by the client (PHP) as a regular table, so querying it as
mysql_query("SELECT * FROM shipsummary");
// Or for whatever framework you're using:
$db->QueryArray("SELECT * FROM shipsummary");
should work correctly. If it does not work correctly, the MySQL user with which you are accessing the view may have broken permissions. (Seems unlikely though).
UPDATE
After you edited your question, I can see the problem quite clearly.
$sql1= " CREATE VIEW shipsumarry AS SELECT shipping.tarrif1, city.city_name
FROM shipping JOIN city
ON shipping.id_city = city.id_city";
$sql2= "SELECT * FROM shipsummary";
// Here, you need to execute $sql1 before $sql2 is useful.
$db->QueryArray($sql1);
// Now execute $sql2
$db->QueryArray($sql2);
We don't know what database class or framework you are using, but if there is a comparable method to QueryArray() that doesn't return a result set, but just executes a statement, use it to create the view instead.
Now, all that being said...
Unless the definition of the view must change every time this code executes, and unless you have a reason to then DROP VIEW shipsummary at the end of this script's execution each time, it makes far, far, far, far .... more sense to simply create the view in the database, where it will stay forever, rather than to keep re-creating it with PHP. Views, once created, stay created.
Don't think of them as a temporary query time/code saver. Create the views you will need ONCE in your database (using PHPMyAdmin or mysql CLI, or however you created your tables), and access them with PHP.
Why not just send that
SELECT * FROM shipsummary
To mysql query, it should work, unless i'm not understanding your question...