I have this subquery:
$sql = "SELECT * FROM curriculum WHERE ci in (SELECT curriculum FROM contactados WHERE activado = 'si' AND empresa = '".$_SESSION['emp']."' )";
echo "Rows: ".#mysql_num_rows($sql);
$result = mysql_query($sql, $link) or die(mysql_error($link));
while($fila = mysql_fetch_array($result)){
echo $fila['nombres']." ".$fila['apellidos']or die("error");
}
But it doesn´t work and I don't know why
i hope this work for you...
$sql = "SELECT `a`.*, `b`.`curriculum` FROM `curriculum` AS `a`
LEFT JOIN `contactados` AS `b` ON `a`.`ci` = `b`.`curriculum`
WHERE `b`.`activado` = 'si' AND `b`.`empresa` = '".$_SESSION['emp']."'";
Related
if(isset($_GET['id']) && $_GET['id'] != null) {
$id = $_GET['id'];
$sql = "SELECT
`maps.name`,
`maps.description`,
`maps.date`,
`maps.mcversion`,
`maps.mapid`,
`maps.category`,
`maps.format`,
`users.username`,
`users.rank`,
`users.verified`,
`users.mcusername`,
COUNT(`views.mapid`) AS `views`,
COUNT(`likes.mapid`) AS `likes`,
COUNT(`downloads.mapid`) AS `downloads`,
COUNT(`subscribes.channelid`) AS `subscribers`
FROM `maps` INNER JOIN `users` ON `maps.userid` = `users.id`
INNER JOIN `views` ON `maps.mapid` = `views.mapid`
INNER JOIN `likes` ON `maps.mapid` = `likes.mapid`
INNER JOIN `downloads` ON `maps.mapid` = `downloads.mapid`
INNER JOIN `subscribe` ON `mapid.userid` = `subscribe.channelid`
WHERE `maps.mapid` = '$id'";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
echo “success”;
} else {
header("LOCATION: index.php");
}
$sql = "SELECT * FROM `maps` WHERE `id`=$id";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
viewer($id);
} else {
header("LOCATION: index.php");
}
This worked, but I need data from more tables.
$sql = "SELECT
`maps.name`,
`maps.description`,
`maps.date`,
`maps.mcversion`,
`maps.mapid`,
`maps.category`,
`maps.format`,
`users.username`,
`users.rank`,
`users.verified`,
`users.mcusername`,
COUNT(`views.mapid`) AS `views`,
COUNT(`likes.mapid`) AS `likes`,
COUNT(`downloads.mapid`) AS `downloads`,
COUNT(`subscribes.channelid`) AS `subscribers`
FROM `maps`
INNER JOIN `users` ON `maps.userid` = `users.id`
INNER JOIN `views` ON `maps.mapid` = `views.mapid`
INNER JOIN `likes` ON `maps.mapid` = `likes.mapid`
INNER JOIN `downloads` ON `maps.mapid` = `downloads.mapid`
INNER JOIN `subscribe` ON `mapid.userid` = `subscribe.channelid`
WHERE `maps.mapid` = '$id'";
Is this sql join good? Why it does not return any results?
with the normal $sql = "SELECT * FROM maps WHERE id=$id"; everything works, but i need data from the other tables too.
The solution:
$sql = "SELECT
maps.name,
maps.description,
maps.date,
maps.mcversion,
maps.mapid,
maps.category,
maps.format,
users.username,
users.rank,
users.verified,
users.mc_username,
(SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
(SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
(SELECT COUNT(*) FROM subscribe WHERE subscribe.channelid = maps.userid) AS subscribers,
(SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS viewers
FROM maps
INNER JOIN users
ON maps.userid = users.id
WHERE maps.id = '$id'";
Thanks for the help!
IF you would like to secure a complex sql statment, how would you do it?
Is it an ok version?:
if(isset($_GET['id']) && $_GET['id'] != null) {
$id = $_GET['id'];
$stmt = $mysqli->prepare('SELECT id FROM maps WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
if (mysqli_num_rows($result) == 1) {
$row = $result->fetch_assoc();
$secid = $row["id"];
} else {
echo "error2";
}
$sql = "SELECT
maps.name,
maps.description,
maps.date,
maps.mcversion,
maps.mapid,
maps.category,
maps.format,
users.username,
users.rank,
users.verified,
users.mc_username,
(SELECT COUNT(*) FROM likes WHERE likes.mapid = maps.id) AS likes,
(SELECT COUNT(*) FROM downloads WHERE downloads.mapid = maps.id) AS downloads,
(SELECT COUNT(*) FROM subscribe WHERE subscribe.channelid = maps.userid) AS subscribers,
(SELECT COUNT(*) FROM views WHERE views.mapid = maps.id) AS viewers
FROM maps
INNER JOIN users
ON maps.userid = users.id
WHERE maps.id = '$secid'";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo $row["name"];
} else {
echo "error3";
}
} else {
echo "error1";
}
database connection:
$mysqli = new mysqli('127.0.0.1', 'root', 'pass’, 'db’);
I have code the below code works fine but i want to have one sql statement instead of few in this code so when i try to change into one i get an error.
$eventID = $_GET['id'];
$sql = "SELECT * FROM te_events where eventID='$eventID'";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$eventTitle = $row['eventTitle'];
$eventDescription = $row['eventDescription'];
$eventStartDate = $row['eventStartDate'];
$eventEndDate = $row['eventEndDate'];
$eventPrice = $row['eventPrice'];
$venueID = $row['venueID'];
$catID = $row['catID'];
$sql2 = "SELECT * FROM te_venue where venueID='$venueID'";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$venueName = $row2['venueName'];
}
$sql3 = "SELECT * FROM te_category where catID='$catID'";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$catName = $row3['catDesc'];
}
}
?>
I changed the code into this but it seems it is not working.
<?php
$eventID = $_GET['id'];
$sql = "SELECT * FROM te_events where eventID='$eventID' AND where venueID='$venueID' From te_venue AND where catID='$catID' From te_category";
$queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($queryresult)) {
$eventTitle = $row['eventTitle'];
$eventDescription = $row['eventDescription'];
$eventStartDate = $row['eventStartDate'];
$eventEndDate = $row['eventEndDate'];
$eventPrice = $row['eventPrice'];
$venueID = $row['venueID'];
$catID = $row['catID'];
$catName = $row['catDesc'];
$venueName = $row['venueName'];
}
?>
And i get this error.
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 'where venueID='' From te_venue AND where catID='' From te_category' at line 1
Yes, you can do that. But in order to get the fields that you want from all three tables, you have to join them together.
Look at this article on W3S: http://www.w3schools.com/sql/sql_join.asp. It explains SQL JOIN syntax and the basic theory behind.
If you just joined venue and event Your select statement looks like:
SELECT * FROM te_event
JOIN te_venue
ON te_vendue.venueID = te_event.venueID
WHERE te_event.eventID = $eventID
The category table is similar.
Note: In general, use of SELECT * is discouraged. Your should list the fields that you want returned from the tables. ie. SELECT te_eventID, te_venueID
You could use joins as below;
SELECT * FROM te_events
JOIN te_venue ON te_events.venueID = te_venue.venueID
JOIN te_category ON te_events.catID = te_category.catID
WHERE eventID = :EventID
As mentioned you should also use PDO's:
define( "DB_DSN", "mysql:host=localhost;dbname=foo");
define( "DB_USERNAME", "root");
define( "DB_PASSWORD", "password" );
// define sql
$sSQL = "SELECT * FROM te_events
JOIN te_venue ON te_events.venueID = te_venue.venueID
JOIN te_category ON te_events.catID = te_category.catID
WHERE eventID = :EventID";
// create an instance of the connection
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
// prepare
$st = $conn->prepare( $sSQL );
// securely bind any user input in the query
$st->bindValue(":EventID", $iEventID, PDO::PARAM_INT);
// execute the connection
$st->execute()
$aResults = array();
// loop over results and store in aResults
while($row = $st->fetch()){
$aResults[] = $row;
}
// output data
foreach($aResults as $aResult){
echo "title: ".$aResult['eventTitle'];
}
You should always avoid using select *. Especially when your doing joins. Ensure you request only what you need and alias if you require duplicate column names
This is the code I have at the moment which works fine:
<?php
$sql = "SELECT * FROM te_events order by eventTitle ASC ";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{
$venueID = $row['venueID'];
$catID = $row['catID'];
$sql2 = "SELECT * FROM te_venue where venueID='$venueID'";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$venueName = $row2['venueName'];
}
$sql3 = "SELECT * FROM te_category where catID='$catID'";
$result3 = $conn->query($sql3);
while($row3 = $result3->fetch_assoc())
{
$catName = $row3['catDesc'];
}
?>
But I want to Change it into this format. I could do only till this bit couldn't go further than this I get errors.
<?php
$sql ="SELECT eventTitle, eventID, venueID, catID, eventStartDate, eventEndDate, eventPrice FROM te_events ORDER BY eventTitle ASC";
$queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));
while ($row = mysqli_fetch_array($queryresult)) {
$venueID = $row['venueID'];
$catID = $row['catID'];
$venueName = $row['venueName'];
$catName = $row['catDesc'];
?>
How can I do that then?
how can I join two tables?
You should be able to join the additional 2 tables to get the columns you need.
SELECT e.eventTitle, e.eventID, e.venueID, e.catID, e.eventStartDate, e.eventEndDate, e.eventPrice, v.venueName, c.catDesc
FROM te_events as e
join te_venue as v
on e.venueID = v.venueID
join te_category as c
on c.catID = e.catID
ORDER BY eventTitle ASC
You also should avoid putting data directly into a query. If you need to do that use parameterized queries. This is how SQL injections (or second level) occur.
I have two tables "BANDS" & "adres". In table "BANDS" there is a BOEKERID which is the same ID as the corresponding ID in "adres".
The bands is always different ofcourse and the BOEKER can be the same. Also because of adress change i have the 2 dfferent tables.
Now, i tried this to get the info:
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $BOEKERID";
$result1 = mysql_query($sql1) or die(mysql_error());
$result2 = mysql_query($sql2) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
while($row2 = mysql_fetch_array($result2))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row2['NAAM'].'</td>';
Somebody can help me ?
If i use your sql :
$sql1 = "SELECT BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>';
$sql2 = "SELECT ID, NAAM FROM `adres` WHERE ID = $row1['BOEKERID']";
$result2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_array($result2)){//change this while if $sql2 all time return only 1 result
echo '<td>'.$row2['NAAM'].'</td>';
}
echo'</tr>';
}
With join (http://sql.sh/cours/jointures/inner-join):
$sql1 = "SELECT NAAM,BANDID, NAAMBAND, CONTACTBAND, BOEKERID FROM `BANDS` INNER JOIN `adres` WHERE `BANDS`.BOEKERID = `adres`.id ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1)){
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>
</tr>';
}
Try Sql join,
SELECT
b.BANDID, b.NAAMBAND, b.CONTACTBAND, b.BOEKERID, a.id, a.naam
FROM BANDS b
LEFT JOIN adres a ON b.boekerid=a.id
ORDER BY $field $sort
Also don't try to use mysql since it's deprecated, instead try switching to MySQLi or PDO
i guess you need to do something like this:
$sql1 = "SELECT a.BANDID, a.NAAMBAND, a.CONTACTBAND, a.BOEKERID, b.NAAM FROM BANDS a, adres b WHERE a.BANDID = b.ID AND ID = $BOEKERID ORDER BY $field $sort";
$result1 = mysql_query($sql1) or die(mysql_error());
while($row1 = mysql_fetch_array($result1))
echo'<tr>
<td>'.$row1['BANDID'].'</td>
<td>'.$row1['NAAMBAND'].'</td>
<td>'.$row1['CONTACTBAND'].'</td>
<td>'.$row1['BOEKERID'].'</td>
<td>'.$row1['NAAM'].'</td>';
Please correct me if im wrong
How do i make a OR query for something like this?
//Query
$sql = "select `Friend_status` from `friendship_box` where
`system_id` = '$sid' and `friend_id` = '$friendis' OR
`system_id='$friendis' and `friend_id` = '$sid'";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
while ($row = mysql_fetch_array($query)){
$activity = htmlspecialchars($row['Friend_status']);
}
mysql_free_result($query);
select `Friend_status` from `friendship_box`
where (`system_id` = '$sid' and `friend_id` = '$friendis')
OR (`system_id`='$friendis' and `friend_id` = '$sid')
you missed the ` here:
...OR (`system_id` <--