$id=$_POST["id"];
$sqls =$handle->prepare("SELECT * FROM r WHERE ur= :u AND id='$id'");
$sqls->bindParam(':u',$_COOKIE['u']);
$sqls->execute();
$row = $sqls -> fetch();
if(!($row)){
if (!isset($_POST['submit']) && $_POST['rating'] <= 5 ) {
$sql_1 = "INSERT INTO review (b,u,r,r) VALUES(:b,:u,:r,:r)";
$query = $handle->prepare($sql_1);
$params = array(':b'=> $_POST['b'],':user'=> $_POST['u'],':r'=> $_POST[''],':ra'=> $_POST['ra']);
$query -> execute($params);
echo success;
} else {
echo nope;
}
?>
for some reason this doenst work i cant find any error is does not give any eror but just doenst do what i want
i dont want it to add an record if there has already been one in the database
Take a look in your SQL: "SELECT * FROM r WHERE ur= :u AND id='$id'"
Should "SELECT * FROM r WHERE ur= ':u' AND id=$id"
$id=intval($_POST["id"]);
$sqls =$handle->prepare("SELECT * FROM r WHERE ur= ':u' AND id=$id");
$sqls->bindParam(':u',$_COOKIE['u']);
$sqls->execute();
$row = $sqls -> fetch();
if(!($row)){
if (!isset($_POST['submit']) && $_POST['rating'] <= 5 ) {
$sql_1 = "INSERT INTO review (b,u,r,r) VALUES(':b',':u',':r',':r')";
$query = $handle->prepare($sql_1);
$params = array(':b'=> $_POST['b'],':user'=> $_POST['u'],':r'=> $_POST[''],':ra'=> $_POST['ra']);
$query -> execute($params);
echo success;
} else {
echo nope;
}
?>
Related
here is a simple mysqli query to select specific records from my mysql database:
foreach ($getData as $data) { {
$sql = "SELECT * FROM `myTable` WHERE `bookid` = '".$data['ID']."' ";
$result = $db->query( $sql );
while ($zeile = $result->fetch_object()) {
// DO SOMETHING
}
}
The question is:
How can I get (best practices) all the other records, which will not be selected with this query filter?
It can be like
foreach ($getData as $data) { {
$sql = "SELECT * FROM `myTable` WHERE `bookid` = '".$data['ID']."' ";
$result = $db->query( $sql );
while ($zeile = $result->fetch_object()) {
// DO SOMETHING
$query2 = "select * from 'myTable' WHERE 'bookid' != ".$zeile['ID']."'";
$result2 = $db -> query($query2);
// do something...
}
}
OR
foreach ($getData as $data) { {
$sql = "SELECT * FROM `myTable` WHERE `bookid` = '".$data['ID']."' ";
$result = $db->query( $sql );
while ($zeile = $result->fetch_object()) {
// DO SOMETHING
}
$query2 = "select * from 'myTable' WHERE 'bookid' != '".$data['ID']."'";
$result2 = $db -> query($query2);
while($zeile2 = $result2-> fetch_object()){
// do something...
}
}
Running successive, more or less identical, queries in a loop is fundamentally a bad way to do things. Create a list of $data['ID'] values you want to work with, then use one query to retrieve all the rows IN that list, and a second query to retrieve everything NOT IN that list:
Important: This code assumes that the values in $getData[]['ID'] can be trusted. i.e. they have been validated before entry to this code, or they come from a trusted source.
// Create a list:
$inList = '('.implode(',', array_column($getData, 'ID')).')';
$sqlIn = "SELECT * FROM `myTable` WHERE `bookid` IN $inList";
// run the query. Check for errors
if (($result = $db->query( $sqlIn )) === false) {
throw new Exception($db->error);
}
while ($zeile = $result->fetch_object()) {
// DO SOMETHING
}
// Now use the same list to exclude those rows
$sqlOut = "SELECT * FROM `myTable` WHERE `bookid` NOT IN $inList";
// run the query. Check for errors
if (($result = $db->query( $sqlOut )) === false) {
throw new Exception($db->error);
}
while ($zeile = $result->fetch_object()) {
// DO SOMETHING
}
What I want to do is a select query and then if it is true set a variable of a table called 'afiliadospadron' to 1 but if it is false (it do not return anything) just set the variable to 0. My code isn't working. Can somebody help me please?
$query= "select nrodoc from oct20 where nrodoc = 05463xx union select nrodoc from dic20 where nrodoc = 054631xx";
$query2= "update afiliadospadron set condVotar = '1' where nroDoc = '5463xxx' ";
//$query3= "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463xxx'";
if ($query) {
$query2;
} else {
echo "next time";
// $query3 = "UPDATE afiliadospadron SET condVotar = 0 WHERE nroDoc = '5463192'";;
}
$statement = $conexion->prepare($query);
$statement = $conexion->prepare($query2);
//$statement = $conexion->prepare($query3);
$statement->execute();
try it
if ($result = $mysqli -> query($query)) {
$query2;
// Free result set
$result -> free_result();
}
code from: https://www.w3schools.com/php/func_mysqli_query.asp
The answer was
$statement = $conexion->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
if ($result = $result ) {
echo "yas";
$statement = $conexion ->prepare($query2);
$statement->execute();
// Free result set
} else {
echo "Wasnt found";
$statement = $conexion ->prepare($query3);
$statement->execute();
}
I'm having a hard time getting this search results with pagination code to work. It does successfully grab the search keyword entered in the html form on another page and brings it into this search.php page. if I echo $search I see the keyword on the page. But I get no results even though I should for the query. Can anyone see what might be going on?
require "PDO_Pagination.php";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
echo $search;
$pagination->rowCount("SELECT * FROM stories WHERE stories.genre = $search");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories WHERE stories.genre = $search ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
else
{
$pagination->rowCount("SELECT * FROM stories");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
$query = "SELECT * FROM stories";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
$query .= " WHERE genre LIKE '%$search%'";
}
// No need for else statement.
$pagination->rowCount($query);
$pagination->config(3, 5);
$query .= " ORDER BY SID ASC LIMIT {$pagination->start_row}, {$pagination->max_rows}";
$stmt = $connection->prepare($query);
$stmt->execute();
$model = $stmt->fetchAll();
var_dump($model);
In your query do:
WHERE stories.genre LIKE '%string%');
instead of:
WHERE stories.genre = 'string');
Because the equals will want to literally equal the field.
I'm using to following to count the number of rows in a table:
// Count rows
$sql = "SELECT COUNT(*) FROM articles";
$result = mysqli_query($con,$sql);
$max = mysqli_fetch_row($result);
echo $max;
This echoes array. I understand why but I can't find how to get the value in this case. I've tried $max[0]. I don't understand how to reference the column in the array in this case.
try this:
$sql = "SELECT COUNT(*) as counts FROM articles";
$result = mysqli_query($con,$sql);
$max = mysqli_fetch_assoc($result);
echo $max['counts'];
some docs here
EDIT:
$sql = "SELECT COUNT(*) as counts FROM articles";
$result = mysqli_query($con,$sql);
while($max = mysqli_fetch_assoc($result))
{
echo $max['counts'];
}
You should use MySQL PDO.
Try this:
try{
$conn = new PDO("mysql:host=localhost;dbname=dbname", username, password);
$conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
$errors = "There is no connection to the Server: localhost";
}
$qry = $conn -> prepare("SELECT COUNT(*) AS counts FROM articles");
$qry -> execute();
while($row = $qry->fetch(PDO::FETCH_ASSOC)) {
$Total = $row['counts'];
}
echo $Total;
I recently was fiddling with my code and ran into an issue and i cannot figure out where it is. I added the "Update site_sync SET test = test+1" code last, which is firing perfectly fine. but everything else seems to be catching somewhere and it's not throwing errors anywhere on my site.
function skynetInfect($db)
{
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
try { $que->execute();
$que->nextRowset();
while($row = $que->fetch(PDO::FETCH_BOTH))
{
if($row[0] == 'on')
{
$que->nextRowset();
$row2 = $que->fetch(PDO::FETCH_BOTH);
$x = $row2[0];
echo $x;
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql);
$que->bindParam('id', $rand);
try{ $que->execute();}catch(PDOException $e) { echo $e->getMessage();}
}
}
}catch(PDOExceptions $e) { echo $e->getMessage();}
}
You are overwriting $que inside the inner if conditional. That happens here:
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql); // use a variable name other than $que here
Also, is there any reason you are actually using PDO::FETCH_BOTH?
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
I don't think you can execute multiple queries using PDO
$que = $db->prepare($sql);
I don't see any parameter in your Query so why the prepare?