How to create a OR statement in one query? - php

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` <--

Related

Subquery php mysql

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']."'";

My mysql update query isnt working

for some reason, I can't get this to work.
Thanks in advance, a secnod eye might help!
$sql3 = "SELECT order_id FROM orders WHERE order_code = '$order_code'";
$result3 = $conn->query($sql3) or exit("Error code ({$conn->errno}): {$conn->error}");
$row = mysqli_fetch_assoc($result3);
$order_id = $row['order_id'];
$deliv_date = date('Y-m-d');
$sql = "UPDATE orders SET deliv_date = $deliv_date
WHERE order_id = $order_id";
$result = $conn->query($sql) or exit("Error code ({$conn->errno}): {$conn->error}");
$sql1 = "INSERT INTO invoice VALUES (0,'$order_code','$deliv_date','','$order_id')";
$result1 = $conn->query($sql1) or exit("Error code ({$conn->errno}): {$conn->error}");
try this
$sql = "UPDATE `orders` SET `deliv_date` = '".$deliv_date."'
WHERE `order_id` = '".$order_id."'"

php look in database for data not working

i'm making a quiz webpage connected to a datbase, but my code isn't working.
i have this code:
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_result($result, 0);
echo $question;
it should look in my table for a question with id 1 and then echo the question, the possible answers and the correct answer.
the column names are: id, question, a, b, c, d and correct
here is how it looks like in phpmyadmin,
since im not allowed to post images, here is a link to the image.
http://i.stack.imgur.com/tNJkd.png
can someone help me???
Try This
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_result($result, 0);
echo $question['question'];
$question = mysql_result($result, 0);
echo $question['question'];
OR
$question = mysql_result($result, 0, 'question');
echo $question;
should do.
Aside of that, mysql_*-functions are deprecated
try this
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_result($result, 0, 'question');
echo $question;
Try This
$query = "SELECT `question` FROM `questions` WHERE `id` = '1'";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$question = mysql_fetch_assoc($result);
echo $question['question'];
/*
or for get the correct answer
you can just: echo $question['correct'];
//*/

process sql query results

I got a table named "Serials" with 5 comumns
Serial, Code, Name, Redeemed, Redeem_date
i am selecting some fields from that table with this query:
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$db->query();
But i dont know how to pass these values in the following variables so i can use them in if statements later
$name= //retured value from column Name
$redeemed= //retured value from column Redeemed
$redeem_date= //retured value from column Redeem_date
just like this..
// Your query here..
$query = "SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'";
$db->setQuery($query);
$results = $db->query();
//fetch data and stored into variables
while($row = fetch_array($results)){
$name = $row['Name'];
$redeemed = $row['Redeemed'];
$redeem_date = $row['Redeem_date'];
}
try something like this :
<?php
$result = $db->query("SELECT `Name`,`Redeemed`,`Redeem_date` FROM `Serials` WHERE `Serial` = '$serial' AND `Code` = '$code'");
while (list($name, $redeemed, $redeem_date) = $result->fetch(PDO::FETCH_NUM)) {
// DO SOMETHING
}
?>
while ($row = $db->fetch()) {
$name= $row['name'];
$redeemed= $row['redeemed'];
$redeem_date= $row['redeem_date'];
}
this one might fetch your results and assign to vars

MySQL run query inside a query

I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}

Categories